Discount from full price, ignore on sale Woocommerce

Discount from full price, ignore on sale Woocommerce

You can add this code in functions.php (from your wordpress theme).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {
 
    global $woocommerce;
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
 
    $coupon = False;
 
    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }
 
    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}

Notice that this might not work all the time, it depends on your theme.
It also depends on what plugins you are using.

If you notice that it doesn’t work, search for this woocommerce function with find and replace (for example with Adobe Dreamweaver) in all the folders of your website:

“woocommerce_before_calculate_totals”

For example I was using the plugin called “dynamic pricing and discounts” and because of this, my coupons code was not working properly.

If you are using dynamic pricing, add this code:

2
3
4
5
6
7
8
"$has_coupons = count(WC()->cart->applied_coupons)>0?true:false;
if (($has_coupons) && (is_cart()) or (is_checkout())){
 
        if(!is_shop() && !is_product() &&  !is_product_category() && !did_action('woocommerce_before_calculate_totals') && apply_filters('xa_give_discount_on_addon_prices',true)==$xa_add_on_true)   // added this code for compatiblity with woocommerce product addons
 
        {
		}"

instead of

3
4
5
 if(!is_shop() && !is_product() &&  !is_product_category() && !did_action('woocommerce_before_calculate_totals') && apply_filters('xa_give_discount_on_addon_prices',true)==$xa_add_on_true)   // added this code for compatiblity with woocommerce product addons
 
        {

However, to tweak the code even further, it is required that you have strong knowledge of php, wordpress and woocomerce.

This was tested on the Shopper Woocomerce theme – it worked.
Flatsome theme – Woocomerce, it did not worked. It had the dynamic pricing plugin, after disabling it, it worked.

Give it a try, if you have questions, please leave in the comments.

Add a comment: