Search code examples
phpwoocommercecartproduct-quantityfee

WooCommerce additional fees based on purchased quantity for specific products


Here is the code I currently have:

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( $cart ){

    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    $pizza_ice = 0;
    $pizza_materials = 0;
    $gravy_ice = 0;
    $gravy_materials = 0;

    $pizza_burger = 293;
    $gravy = 462;
    
    $pizza_in_cart = false;
    $gravy_in_cart = false;

    foreach( $cart->get_cart() as $item ){
        $product_in_cart = $item['product_id'];
        if ( $product_in_cart === $pizza_burger) {
            $pizza_in_cart = true;
        }
        elseif ( $product_in_cart === $gravy) {
            $gravy_in_cart = true;
        }
    }

    if ( $pizza_in_cart != false ) {
        $pizza_ice += $item['quantity'] * 27;
        $pizza_materials += $item['quantity'] * 9.99;

        $cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
        $cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);
    }
    elseif ( $gravy_in_cart != false ) {
        $gravy_ice += $item['quantity'] * 27;
        $gravy_materials += $item['quantity'] * 6.99;

        $cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
        $cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
    }
    elseif ( $pizza_in_cart != false || $gravy_in_cart != false ) {
        $pizza_ice += $item['quantity'] * 27;
        $pizza_materials += $item['quantity'] * 9.99;

        $cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
        $cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);

        $gravy_ice += $item['quantity'] * 27;
        $gravy_materials += $item['quantity'] * 6.99;

        $cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
        $cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
    }

}

What I am trying to do is:

  • If Pizza Burger product is in the cart, it should add the Dry Ice fee AND Materials fee for each quantity.

  • If Frozen Gravy product is in the cart, it should add the Dry Ice fee AND Materials fee for each quantity.

  • If both Pizza Burger and Frozen Gravy products are in the cart, it should add BOTH Dry Ice fees AND BOTH Materials fees for each quantity.

What am I doing wrong here?

When this snippet is active, It's only working for the Gravy. Changing the quantity for Pizza Burgers has no effect on the new fees.


Solution

  • There are some mistakes in your code, try the following instead:

    add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
    function add_custom_fees( $cart ){
        if (is_admin() && !defined('DOING_AJAX')) 
            return;
    
        ## -- product Ids -- ##    
        $pizza_id = 293;
        $gravy_id = 462;
        
        ## -- dry Ice & Materials fee amounts -- ##  
        $pizza_ice = $gravy_ice = 27;
        $pizza_mat = 9.99;
        $gravy_mat = 6.99;
    
        $gravy_qty = $pizza_qty = 0; // Initializing
    
        // Loop through cart items
        foreach( $cart->get_cart() as $item ){
            if ( $item['product_id'] == $pizza_id ) {
                $pizza_qty += $item['quantity'];
            } 
            elseif ( $item['product_id'] == $gravy_id ) {
                $gravy_qty += $item['quantity'];
            }
        }
    
        if ( $pizza_qty > 0 ) {
            $cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_qty * $pizza_ice );
            $cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_qty * $pizza_mat );
        }
    
        if ( $gravy_qty > 0 ) {
            $cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_qty * $gravy_ice );
            $cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_qty * $gravy_mat );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.