skip to Main Content

I am adding a custom fee to the cart by hooking into the woocommerce_cart_calculate_fees action like so:

add_action('woocommerce_cart_calculate_fees', function add_loyalty_discounts(WC_Cart $cart)
    {
   $cart->add_fee('Loyalty Discount',   -intval($_GET['discount']));
});

The fee appears on the cart page but not persist onto the checkout and order pages.

I’ve tried using $cart->set_session() but this had no effect.

2

Answers


  1. I tested and it works fine with me on both cart and checkout page

    function add_loyalty_discounts(WC_Cart $cart) {
            $cart->add_fee('Loyalty Discount', -intval(100), true);
    }
    
    add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
    
    
    Login or Signup to reply.
  2. There are some mistakes, and missing things in your code, try the following:

    add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
    function add_loyalty_discounts($cart) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
            
        $cart->add_fee('Loyalty Discount', -intval(100), true);
    }
    
    

    Code goes in function.php file of your child theme (or in a plugin). It should work now.

    Related:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search