2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Trisha
  • 539
  • 3
  • 10
  • 30

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • That's definitely better, but something is still off. It's changing the fee amounts for both Pizza Burger and Gravy if the Gravy quantity is 2 (https://ibb.co/JC8wzTG), but if it's changed on Pizza Burger it does nothing (https://ibb.co/Vx1G2Lb). PS - Thanks for the quick response! --- Wait, I think that was just the file not updated upon save. Retrying and will update! – Trisha Feb 11 '21 at 22:25
  • It's missing a ; at the end of `$pizza_ice = $gravy_ice = 27`, fixed that, but it's still giving issues. Gravy x2 and Pizza x1 (https://ibb.co/XZvcxfd) -- Gravy x1 and Pizza x2 (https://ibb.co/WK82VKh) -- looks like it's not applying to the quantities correctly? – Trisha Feb 11 '21 at 22:33
  • Not sure what happened a I'm not seeing it that way in your code now but the add_fee() shipping materials for gravy was calling pizza_qty * gravy_mat - changed it to gravy_qty and it works now. Thanks again! – Trisha Feb 11 '21 at 23:00
  • @Trisha Yes I have just added missing `;` in `$pizza_ice = $gravy_ice = 27`… For `'$pizza_qty * $gravy_mat'` it was already updated… Happy that it works finally. – LoicTheAztec Feb 12 '21 at 15:17