How do I apply Cart Discount Before Tax in woocommerce

I have being trying to apply cart discount before tax usgin my custom Woocommerce Plugin. But it is not getting applied using the action hook woocommerce_cart_discounts_before_tax in my custom plugin file.

Here is the code from my custom plugin file.

Read More
public function custom_discount(){
                global $woocommerce;
                $abc = woocommerce_price(5);
                echo $abc;
}

and the action hook

if(!empty($_POST['apply_discount_woo'])){
                    add_action( 'woocommerce_cart_discounts_before_tax', array(&$this,'custom_discount'));
                }

I get the following screen

enter image description here

The screenshot shows $5 above the subtotal but doesn’t get applied to the cart and shows the wrong total.

Related posts

Leave a Reply

1 comment

  1. I think you need to return the value, not echo:

    public function custom_discount(){
        global $woocommerce;
        $abc = woocommerce_price(5);
        return $abc;
    }
    

    However, i think this will also overwrite any other discounts that have been applied.