Woocommerce

How To Add Discount on cart page in woocomerce

27 July 2021

In woocommerce we can add the fixed discount automatically of 10% if the cart total equal 5000 or greater than 5000

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

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

    $total = $cart->cart_contents_total;

    // Percentage discount (10%)
    if( $total >= 5000 )
        $discount = $total * 0.1;
    // Fixed amount discount ($20)
    else
        // $discount = 10;

    // Add the discount
    $cart->add_fee( __('discount', 'woocommerce'), -$discount );
}

Just paste the code in your functions.php file of theme and check it is working or not.

Thanks for visting and comment below if it is useful for You!!!!

Leave a Reply

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

eighteen − 7 =

Author

Hello, I'm
Rajat Meshram

I am a WordPress Developer having 5+ years of Experience. My professional experience includes designing and implementing web pages, user interfaces and plugins for WordPress, helping clients to troubleshoot and fix their WordPress products, designing themes that are as functional as they are beautiful and working with a team of colleagues to create the best products possible.

Latest Post

Latest Tags