Add Cart Discount based on cart total amount – WooCommerce Snipppet

Add Cart Discount based on cart total amount – WooCommerce Snipppet

Add Cart Discount based on cart total amount in WooCommerce Snipppet without using a plugin

This woo-commerce cart page functionality can be achieve using the below.

How it works

  • In the cart page if the total price between two products is greater than 150 dollars than 15% discount on total cart of product price will be apply.
  • If total price between two product is greater than 100 dollars. and less than 150 dollars than 10% discount on total cart of product price will be apply.
  • If total price between two product is grater than 50 dollars. and less than 100 dollars than need 5% discount on total cart of product price will be apply.

Below is the snippet code. Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_cart_total', 10, 1 );
function discount_based_on_cart_total( $cart_object ) {

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

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total > 150.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 100.00 && $cart_total < 150.00 ) $percent = 10; // 10% 
    elseif ( $cart_total >= 50.00 && $cart_total < 100.00 ) $percent = 5; // 5% 

    else $percent = 0; if ( $percent != 0 ) { $discount = $cart_total * $percent / 100; 
    $cart_object->add_fee( "Discount ($percent%)", -$discount, true );
    }
}

Results on Cart Page

Before

Add Cart Discount based on cart total amount in WooCommerce Snipppet

After

discounts