Guide to Setting Up a Promo Code in WooCommerce

chaoscss

New member
Hello everyone,

I need your help after two days of unsuccessful research.
I created a promo code for 50 euros off the cart and another for -50%. My problem is that the -50% discount is not being applied to the remaining amount after deducting the 50 euros.

For example, for a cart of 150 euros:
First discount: -50 euros = 100 euros
Then, I would like to apply -50% on this amount, but it doesn’t work.

I asked ChatGPT to create a code (attached), but it doesn’t work. Does anyone have a recommendation? (I’m not a programmer.)

Thank you in advance and have a great evening!



// Apply the coupon 'CODETEST' only to the remaining amount after other discounts
add_action('woocommerce_cart_calculate_fees', 'apply_discount_after_previous_coupon', 20, 1);

function apply_discount_after_previous_coupon($cart) {

// Do nothing in the admin area or during AJAX calls (outside the shop)
if (is_admin() || defined('DOING_AJAX') && DOING_AJAX) {
return;
}

// Check if the coupon 'CODETEST' is applied
if (!in_array('CODETEST', $cart->get_applied_coupons(), true)) {
return; // Do nothing if the coupon is not used
}

// Get the cart's gross subtotal (before taxes and discounts)
$subtotal = $cart->get_subtotal();

// Stop if the subtotal is 0 or invalid
if ($subtotal <= 0) {
return;
}

// Calculate the total discounts applied by other coupons
$total_discounts = 0;

foreach ($cart->get_coupons() as $code => $coupon) {
if ($code !== 'CODETEST') { // Exclude the 'CODETEST' coupon
$total_discounts += $cart->get_coupon_discount_amount($code, false); // Raw discount
}
}

// Calculate the remaining amount after applying other discounts
$remaining_total = $subtotal - $total_discounts;

// Check if the remaining amount is valid
if ($remaining_total <= 0) {
return; // Do nothing if the remaining amount is zero or negative
}

// Calculate the discount for the 'CODETEST' coupon (50% of the remaining amount)
$discount_amount = $remaining_total * 0.50;

// Apply the discount as a negative fee
$cart->add_fee(__('50% Discount (after other coupons)', 'woocommerce'), -$discount_amount);
}
 
Hello everyone,

I need your help after two days of unsuccessful research.
I created a promo code for 50 euros off the cart and another for -50%. My problem is that the -50% discount is not being applied to the remaining amount after deducting the 50 euros.

For example, for a cart of 150 euros:
First discount: -50 euros = 100 euros
Then, I would like to apply -50% on this amount, but it doesn’t work.

I asked ChatGPT to create a code (attached), but it doesn’t work. Does anyone have a recommendation? (I’m not a programmer.)

Thank you in advance and have a great evening!
ChatGPT is useless :D
Try this:
Code:
// Apply the coupon 'CODETEST' only to the remaining amount after other discounts
add_action('woocommerce_cart_calculate_fees', 'apply_discount_after_previous_coupon', 20, 1);

function apply_discount_after_previous_coupon($cart) {
    // Do nothing in the admin area or during AJAX calls (outside the shop)
    if (is_admin() || defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    // Check if the coupon 'CODETEST' is applied
    if (!in_array('CODETEST', $cart->get_applied_coupons(), true)) {
        return; // Do nothing if the coupon is not used
    }

    // Get the cart's gross subtotal (before taxes and discounts)
    $subtotal = $cart->get_subtotal();

    // Stop if the subtotal is 0 or invalid
    if ($subtotal <= 0) {
        return;
    }

    // Calculate the total discounts applied by other coupons
    $total_discounts = 0;

    foreach ($cart->get_coupons() as $code => $coupon) {
        if ($code !== 'CODETEST') { // Exclude the 'CODETEST' coupon
            $total_discounts += $cart->get_coupon_discount_amount($code, false); // Raw discount
        }
    }

    // Calculate the remaining amount after applying other discounts
    $remaining_total = $subtotal - $total_discounts;

    // Check if the remaining amount is valid
    if ($remaining_total <= 0) {
        return; // Do nothing if the remaining amount is zero or negative
    }

    // Calculate the discount for the 'CODETEST' coupon (50% of the remaining amount)
    $discount_amount = $remaining_total * 0.50;

    // Apply the discount as a negative fee
    $cart->add_fee(__('50% Discount (after other coupons)', 'woocommerce'), -$discount_amount);
}
Hope it helps.
 
Hello,

Thank you so much for taking the time to respond to me.

Unfortunately, the issue persists:

  • The initial cart amount is €133.80.
  • I apply the first code for -€50, which brings the total to €83.80.
  • Then I apply a -50% code, but the final amount calculated is €16.90 instead of €41.90.
It seems that the 50% discount is being calculated on the initial amount (€133.80) instead of the remaining total.

Do you have a solution to this issue? I’m sorry, but I don’t know how to code.

Thank you for your help.
 
Hello,

Thank you so much for taking the time to respond to me.

Unfortunately, the issue persists:

  • The initial cart amount is €133.80.
  • I apply the first code for -€50, which brings the total to €83.80.
  • Then I apply a -50% code, but the final amount calculated is €16.90 instead of €41.90.
It seems that the 50% discount is being calculated on the initial amount (€133.80) instead of the remaining total.

Do you have a solution to this issue? I’m sorry, but I don’t know how to code.

Thank you for your help.
You need to ensure that the discount is calculated based on the correct remaining total after all other discounts have been applied.
I have quickly made some changes. Untested, but give it a shot.
Code:
// Apply the coupon 'CODETEST' only to the remaining amount after other discounts
add_action('woocommerce_cart_calculate_fees', 'apply_discount_after_previous_coupon', 20, 1);

function apply_discount_after_previous_coupon($cart) {
    // Exit if in admin area or during AJAX calls
    if (is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
        return;
    }

    // Check if the coupon 'CODETEST' is applied
    if (!in_array('CODETEST', $cart->get_applied_coupons(), true)) {
        return; // Exit if the coupon is not used
    }

    // Get the cart's gross subtotal (before taxes and discounts)
    $subtotal = $cart->get_subtotal();

    // Exit if the subtotal is 0 or invalid
    if ($subtotal <= 0) {
        return;
    }

    // Calculate the total discounts applied by other coupons
    $total_discounts = 0;
    foreach ($cart->get_coupons() as $coupon) {
        if ($coupon->get_code() !== 'CODETEST') {
            $total_discounts += $coupon->get_discount_amount();
        }
    }

    // Calculate the remaining amount after applying other discounts
    $remaining_total = $subtotal - $total_discounts;

    // Exit if the remaining amount is zero or negative
    if ($remaining_total <= 0) {
        return;
    }

    // Calculate the discount for the 'CODETEST' coupon (50% of the remaining amount)
    $discount_amount = $remaining_total * 0.50;

    // Apply the discount as a negative fee
    $cart->add_fee(__('50% Discount (after other coupons)', 'woocommerce'), -$discount_amount);
}
 
"You need to ensure that the discount is calculated based on the correct remaining total after all other discounts have been applied"

That is exactly what I need. Unfortunately, WooCommerce does not provide this rule in the system, and after asking on the WooCommerce forum, they confirmed that there is no plugin capable of doing this. The only possible solution is with PHP code.

Unfortunately, the code you provided does the same thing; we are still at a total of €16.90
 
I don’t know if this might help you, but the person who replied to me on the WooCommerce forum shared this link. I’m passing it on to you, just in case.

 
Oh. Sorry. But I did say untested. :P
Ok, I have applied some changes now:
It directly retrieves the total amount after all discounts have been applied using $cart-get_total('edit'). This ensures that the discount is calculated on the final amount that the customer is expected to pay.
The discount should now applied as a negative fee directly on the grand total.
Code:
// Apply the coupon 'CODETEST' only to the grand total after other discounts
add_action('woocommerce_cart_calculate_fees', 'apply_discount_after_previous_coupon', 20, 1);

function apply_discount_after_previous_coupon($cart) {
    // Exit if in admin area or during AJAX calls
    if (is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
        return;
    }

    // Check if the coupon 'CODETEST' is applied
    if (!in_array('CODETEST', $cart->get_applied_coupons(), true)) {
        return; // Exit if the coupon is not used
    }

    // Get the cart's total after applying other discounts
    $total = $cart->get_total('edit');

    // Exit if the total is 0 or invalid
    if ($total <= 0) {
        return;
    }

    // Calculate the discount for the 'CODETEST' coupon (50% of the total)
    $discount_amount = $total * 0.50;

    // Apply the discount as a negative fee
    $cart->add_fee(__('50% Discount (on grand total)', 'woocommerce'), -$discount_amount);
}
Again, untested. See how it goes.
 
Dunno then. Something's amiss. Can't understand why Woocommerce can't do this already. It's been around for thousands of years lol. I don't use woocommerce myself. Hate it. Hence why I can't test it.
Hopefully someone will help.
 
Back
Top