Skip to content

Instantly share code, notes, and snippets.

@jplhomer
Last active July 10, 2021 01:53
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jplhomer/6383045 to your computer and use it in GitHub Desktop.
Save jplhomer/6383045 to your computer and use it in GitHub Desktop.
Programmatically alter the WooCommerce cart discount total (and apply a new discount) before it's calculated through the woocommerce_calculate_totals hook. Add this code to your Wordpress functions.php file.
<?php
/**
* Programmatically alter the WooCommerce cart discount total (and apply a new discount) before it's calculated
* through the woocommerce_calculate_totals hook.
*
* For example, if you want to apply an arbitrary discount after a certain number of products in the cart,
* you can loop through that number and increment it as needed.
*
* In this example, I increment the discount by $8.85 after every 15 products added to the cart.
*
* @return void
*/
function mysite_box_discount( ) {
global $woocommerce;
/* Grab current total number of products */
$number_products = $woocommerce->cart->cart_contents_count;
$total_discount = 0;
$my_increment = 15; // Apply another discount every 15 products
$discount = 8.85;
if ($number_products >= $my_increment) {
/* Loop through the total number of products */
foreach ( range(0, $number_cards, $my_increment) as $val ) {
if ($val != 0) {
$total_discount += $discount;
}
}
// Alter the cart discount total
$woocommerce->cart->discount_total = $total_discount;
}
}
add_action('woocommerce_calculate_totals', 'mysite_box_discount');
@heddesheimer
Copy link

It seems this is not working. The action hook passes the $this value, which is the actual shopping cart. So instead of using $woocommerce->cart->cart_contents_count; you should declare the function like this:

function mysite_box_discount( $cart )

and use this to apply the discount:

$cart->discount_total = $total_discount;

then it works fine.

@molod
Copy link

molod commented Apr 29, 2014

Thanks for a snippet, worked like a charm

@adnanoner
Copy link

Any way to do this with the new Woocommerce 2.3 updates to the discounts class?

@JBNavadiya
Copy link

It seems this is not working on checkout page. When I am using this code it is working on cart page then click on "proceed to checkout" button and checkout page appears but 100 Rs. discount is not applied on that page. I am stuck. It would be great if anyone can help. Thanks!

add_action('woocommerce_calculate_totals', 'discount_cal');
function discount_cal(){
global $woocommerce;
$dis_val = 100;
if(is_user_logged_in()){
if($dis_val != 0){
$discount = $dis_val ;
$total_discount = 0;
$total_discount += $discount;
// Alter the cart discount total
$woocommerce->cart->discount_total = $total_discount;
}
}
}

I have also tried this (I was assumming cart session also needs to be update)
//try - session discount apply sol- for checkout
$woocommerce->cart->cart_session_data['discount_total'] = $total_discount;

@dkmartin
Copy link

Can't get this to work either. Ended up creating a negative fee in place of discount.

function sale_custom_price($cart_object) {
// Calculate discount amount and return $discount

if ($discount) {
$discount *= -1;
$cart_object->add_fee('Discount', $discount, true, '');
}
add_action( 'woocommerce_cart_calculate_fees', 'sale_custom_price');

Taken from here: http://stackoverflow.com/a/23371512/3233036

However this adds an extra line to the checkout page for Discount instead of using the already present Discount field. Any ideas?

@SHK111
Copy link

SHK111 commented Jun 28, 2016

Hey, I'm looking to apply a discount without code. An automatic discount over 100$ ...
Do you know how can I do that ?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment