Menu Close

Add discount amount based on the cart total Woocommerce

0
(0)

Code Snippet for adding discount for cart total in woocommerce

add_action( 'woocommerce_cart_calculate_fees', 'code_discount_based_on_total',25, 1 );
function code_discount_based_on_total( $cart ) {

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

    $total = $cart->cart_contents_total;
    $discount = $total * 0.1;
 //10% Discount
    
    $cart->add_fee( __('discount', 'woocommerce'), -$discount );
} 

Also you can discount variations based on the cart total.

add_action( 'woocommerce_cart_calculate_fees', 'code_discount_based_on_total',25, 1 );
function code_discount_based_on_total( $cart ) {

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

    $total = $cart->cart_contents_total;

    if( $total >= 200 &amp;amp;&amp;amp; $total < 500 ){
       $discount = $total * 0.1;
 // Percentage discount (10%)
    }
    else if( $total >= 500  &amp;amp;&amp;amp; $total < 1000 ){
        $discount = $total * 0.15;
 // Percentage discount (15%)
    }
    else if( $total >= 1000 ){
        $discount = $total * 0.20;
 // Percentage discount (20%)
    }
    else{
        $discount = 0;
 // Percentage discount (0%)
    }

    $cart->add_fee( __('discount', 'woocommerce'), -$discount ); 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share

Posted in PHP, WooCommerce, WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

You can also read...