php – Foreach loop check indiviual items in woocommerce cart loop

Question

Can someone help me with a foreach loop? This is my code:

add_action( ‘woocommerce_check_cart_items’, ‘add_custom_fees’ );

function add_custom_fees(){
    $found      = false;
    $categories = array('1', '2');
    foreach( $cart_items as $cart_item_key => $cart_item ) {
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
        $found = true;
        $sum_price_arrays = $cart_item['data']->get_price() * $cart_item['quantity'];
        break;
        }
    }
    if ( $found = true ) {
    wc_add_notice( sprintf( __( "Test %s" ), wc_price($sum_price_arrays ) ), 'notice' );
    }
}

Product 1 ($5) x quantity (2) = $10.

Product 2 ($5) x quantity (2) = $10.

If both of the products A & B is found, the $sum_price_arrays should be $20 but it shows only $10?

0
Thijs Driesen 8 months 2021-12-05T12:45:51-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse