December 28, 2024
1 min read

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);
		
}

Leave a Reply

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

1 Comment

Cookie Popup with jQuery

Browser cookies popup accept, store cookies in browser with jQuery

Jquery Replace specific text in all element

Scenario Imagine you have a set of elements with the class specific_class, and they all contain the phrase “old text.” You want to replace this phrase with “New text” programmatically. The Solution Here’s a simple jQuery script to achieve this: How It Works Things to Keep in Mind Extending the Script jQuery provides a quick […]

JS set interval for an event until element show

Sometimes we need to active an event when a specific element loads on-page or part of an element change.

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.