WooCommerce Discount based on Cart Item

A discount on the total order with condition based on our cart item

We will give a discount on the total order with conditions based on our cart item.

Here is the basic structure of our code

function.php
//discount 
add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
function custom_discount( $cart ){

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

     // Iterating through each item in cart
	$discount = 0;
	foreach( $cart->get_cart() as $cart_item ){
		// Getting an instance of the product object
		// $product = wc_get_product( $cart_item['product_id'] );
                   // $cat = $product->get_category_ids();
		
		//for item subtotal
$sub_total = $cart_item['line_total'];
		
		if ($sub_total>1000) {
			$discount += -10;
		}
		
		
	}

	## Applied discount (no products on sale) ##
	$cart->add_fee( 'Buffet over $1000 order', $discount);
		
}
Previous Code

Jquery Replace specific text in all element

TweetShareSharePin0 Shares ...

Next Code

Replace add to cart button with the product page link & Change add to cart text

`Replace add to cart button with product single li ...

Leave a Reply

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

17 + 2 =

1 Comment

skype

Need Coding Help?

Connect Skype

ACF

Elementor

JavaScript

jQuery

Others

PHP

WooCommerce

WordPress

Remove WooCommerce checkout fields

Remove WooCommerce Checkout field with simple filt ...

WooCommerce Checkout Conflict with Bootstrap 4.x

WooCommerce Checkout Conflict with Bootstrap 4.x. ...

How to Implement Google Ads Conversion Tracking in WooCommerce

Learn how to add Google Ads tracking code to your ...

Implementing WooCommerce Shop Page Quantity Input and Plus (+) Minus (-) Button with simple Code

Enhance the user experience on your WooCommerce we ...

Add an additional custom checkbox in the WooCommerce checkout

Add an additional custom checkbox after the terms ...

top