Плата за Woocommerce не умножается на несколько элементов корзины?

#php #wordpress #woocommerce

Вопрос:

Этот скрипт добавляет плату, если товар относится к определенной категории.
Скрипт работает, но только для одного элемента корзины.
Но если в корзине будет 2 или более элементов, он не будет умножаться.
Что я делаю не так?

 add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
function custom_pcat_fee( $cart ) {
    if ( is_admin() amp;amp; ! defined( 'DOING_AJAX' ) )
        return;

    // iPhone CAT
    $iphone_categories = array('74');
    $iphone_fee_amount = 0;
    
    // iPad CAT
    $ipad_categories = array('75');
    $ipad_fee_amount = 0;

    
    // Loop through cart items 
    foreach( $cart->get_cart() as $cart_item ){
        $quantiy = $cart_item['quantity']; //get quantity from cart
        if( has_term( $iphone_categories, 'product_cat', $cart_item['product_id']) )
            $iphone_fee_amount = 4.70 * $quantiy;
        if( has_term( $ipad_categories, 'product_cat', $cart_item['product_id']) )
            $ipad_fee_amount = 2.60 * $quantiy;
    }

    // Adding the fee for iPhone
    if ( $iphone_fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Thuiskopieheffing iPhone", "woocommerce" ), $iphone_fee_amount, true );
    }
    // Adding the fee for iPad
    if ( $ipad_fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Thuiskopieheffing iPad", "woocommerce" ), $ipad_fee_amount, true );
    }
}```
 

Ответ №1:

Я думаю, вам не хватает базового суммирования в каждом. вы переопределяете $iphone_fee_amount , и $ipad_fee_amount вам нужно добавлять каждое значение к этой переменной. проверьте код ниже.

 function custom_pcat_fee( $cart ) {
    if ( is_admin() amp;amp; ! defined( 'DOING_AJAX' ) )
        return;

    // iPhone CAT
    $iphone_categories = array('74');
    $iphone_fee_amount = 0;
    
    // iPad CAT
    $ipad_categories = array('75');
    $ipad_fee_amount = 0;

    
    // Loop through cart items 
    foreach( $cart->get_cart() as $cart_item ){
        $quantiy = $cart_item['quantity']; //get quantity from cart
        if( has_term( $iphone_categories, 'product_cat', $cart_item['product_id']) )
            $iphone_fee_amount = $iphone_fee_amount   ( 4.70 * $quantiy );
        if( has_term( $ipad_categories, 'product_cat', $cart_item['product_id']) )
            $ipad_fee_amount = $iphone_fee_amount   ( 2.60 * $quantiy );
    }

    // Adding the fee for iPhone
    if ( $iphone_fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Thuiskopieheffing iPhone", "woocommerce" ), $iphone_fee_amount, true );
    }
    // Adding the fee for iPad
    if ( $ipad_fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Thuiskopieheffing iPad", "woocommerce" ), $ipad_fee_amount, true );
    }
}