Ad

How To Sum The Additional Fees Of Added Product ID's In WooCommerce Cart

- 1 answer

I'm using How to add additional fees to different products in WooCommerce cart answer code (part 2). This code is to add additional fees to the products that are described with their IDs.

Of that existing answer I have replaced:

// Settings
$settings = array(
    array(
        'product_id' => 30,
        'amount'     => 5,
        'name'       => __( 'Additional service fee', 'woocommerce' ),
    ),
    array(
        'product_id' => 813,
        'amount'     => 10,
        'name'       => __( 'Packing fee', 'woocommerce' ),
    ),
    array(
        'product_id' => 815,
        'amount'     => 15,
        'name'       => __( 'Another fee', 'woocommerce' ),
    ),
);

With

// Settings
$settings = array(
    array(
        'product_id' => __(123, 456, 789),
        'amount'     => 10,
        'name'       => __( 'Additional service fee', 'woocommerce' ),
    ),
    array(
        'product_id' => __(987, 654, 321),
        'amount'     => 20,
        'name'       => __( 'Additiona packing fee', 'woocommerce' ),
    ),
    array(
        'product_id' => __(445, 556, 555),
        'amount'     => 30,
        'name'       => __( 'Additinal specific fee', 'woocommerce' ),
    ),
);

At the moment, if I add two products that are in the same "additional fee" category, for example, "'Additional service fee'" only shows me the amount once and does not count it.

My question is how to change the code so that by adding more than one product that is in the same "additional fee" category - to consider the sum of the fee for the category.

Ad

Answer

The __() WordPress function retrieve the translation and therefore has nothing to do with adding multiple productIDs. Instead, you can add multiple product IDs as an array.

Note: The total_amount setting serves as a counter to keep track of the total amount and should therefore not be changed!

Note: I've added extra code that also takes product quantity into account. If not applicable. Just delete $quantity = $cart_item['quantity']; and change $setting['amount'] * $quantity; to $setting['amount'];

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Settings (multiple settings arrays can be added/removed if desired)
    $settings = array(
        array(
            'product_id'    => array( 30, 813, 815 ),
            'amount'        => 5,
            'name'          => __( 'Additional service fee', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 817, 819, 820 ),
            'amount'        => 25,
            'name'          => __( 'Packing fee', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 825 ),
            'amount'        => 100,
            'name'          => __( 'Another fee', 'woocommerce' ),
            'total_amount'  => 0,
        ),
    );
    
    // Loop through cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {      
        // Get product id
        $product_id = $cart_item['product_id'];
        
        // Get quantity
        $quantity = $cart_item['quantity'];
        
        // Loop trough settings array (determine total amount)
        foreach ( $settings as $key => $setting ) {
            // Search for the product ID
            if ( in_array( $product_id, $settings[$key]['product_id'] ) ) {
                // Addition
                $settings[$key]['total_amount'] += $setting['amount'] * $quantity;
            }
        }       
    }
    
    // Loop trough settings array (output)
    foreach ( $settings as $setting ) {
        // Greater than 0
        if ( $setting['total_amount'] > 0 ) {
            // Add fee
            $cart->add_fee( $setting['name'], $setting['total_amount'], false );    
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
Ad
source: stackoverflow.com
Ad