2

I need to remove coupon discount for backordered (and out of stock) items in cart, but without removing that discount for other eligible items. E.g. 2 items in cart, Item1 is in stock and gets coupon discount 20%, Item2 is out of stock, backorders allowed, but discount is 0. Can someone suggest any solution, plugin or function?

Demuri Celidze
  • 579
  • 2
  • 7
  • 25
  • 1
    That is not possible or very complicate… even Woocommerce feature for on sale products works only globally… So you should try to rethink it differently. – LoicTheAztec Apr 14 '19 at 00:07
  • Thanks @LoicTheAztec. Seems like you right coz' I'm searching for solution about a week and didn't find any. Do you think the only solution is to disallow backorders? – Demuri Celidze Apr 14 '19 at 08:57
  • You could also try to use: 1) a negative fee based on a coupon without discount, so you could make any discount calculations, by item. 2) change the cart item price to a discounted one, when a coupon coupon (without discount is applied) and when the item is not on backorders. – LoicTheAztec Apr 14 '19 at 14:28
  • It's a bit more complicated. There are item which are still in stock AND available on backorder. I need them to be ordered with discount only while they're in stock. Seems like disabling backorders for the sale time would be the only reasonable way. – Demuri Celidze Apr 14 '19 at 17:05
  • 1
    I know but there is a dedicated WC_Product conditional function that you can use n cart items (and quantity) like: `if( $product->is_on_backorder( $cart_item['quantity']) ) { /* Product is on backorders */ } else { /* Product is not on backorders */ }` … So in this case it handle the quantity and make it works. – LoicTheAztec Apr 14 '19 at 17:26
  • 1
    Shame on me, I'm not a programmer. I can read and sometimes customize the code, but not able to write my own (except html or css). – Demuri Celidze Apr 14 '19 at 20:12

1 Answers1

0
// If WooCommerce Cart items are on backorder do not apply coupon
function filter_woocommerce_coupon_get_discount_amount($discount, $price_to_discount, $cart_item, $single, $coupon) {    
    // On backorder
    if ($cart_item['data']->is_on_backorder()) {
        $discount = 0;
    }

    return $discount;
}
add_filter('woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5);
tomfrio
  • 993
  • 7
  • 16
Inigo
  • 11
  • 2