Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coupon discount on product regular price (not sale price)

In WooCommerce, I have products with a regular price of X and a sales price of Y. I would like to add a coupon with a code for a $45.00 discount to be taken from the regular price X.

I would like the coupon to disregard the sale price so I get X-$45 NOT Y-$45. But when the coupon is not applied price Y is used.

I found the following which works for percentage discounts, but I can't seem to make it work for a fixed product discount price.

add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
    if ($coupon->type == 'percent_product' || $coupon->type == 'percent') {
        global $woocommerce;
        $cart_total = 0;

        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {

         $variable_product1= new WC_Product_Variation( $cart_item["variation_id"] );

         $cart_total += $variable_product1 ->regular_price * $cart_item['quantity'];
        } 
        $discount = round( ( $cart_total / 100 ) * $coupon->amount, $woocommerce->cart->dp );
        return $discount;
    }
    return $discount;
}
like image 735
Haley M Avatar asked Apr 12 '17 18:04

Haley M


People also ask

What is the difference between a coupon and a discount?

A coupon is an access right to a discount. This right of access is controlled by a coupon code. If a customer knows a valid coupon code, he can activate the discount himself by entering the coupon code when purchasing a product.

Does a coupon take price down?

If a coupon is higher than the prevailing interest rate, the bond's price rises; if the coupon is lower, the bond's price falls.

What is fixed product discount?

Fixed product discount – A fixed total discount for selected products only. Customer receives a set amount of discount per item. For example, three (3) t-shirts @ $20 each with a coupon for $10 off applies a discount of $30.

What is meant by discount coupon?

Discount codes (also known as promo codes, coupon codes, and offer codes) are codes provided to customers and applied at checkout that reduce an order's price. They are often used in ecommerce as a tool to add value to the order, incentivize purchasing, and build loyalty for both new and returning customers.


1 Answers

This seems to check for a coupon and then change the cart price to regular price rather than discounted price (obtained from woocommerce dynamic pricing in this application) prior to applying the coupon. placed in functions.php

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 );
        }
}
like image 106
JSE Avatar answered Sep 30 '22 10:09

JSE