2

So far this is what I've got:

add_filter('woocommerce_coupon_is_valid','coupon_always_valid',99,2);
function coupon_always_valid($valid, $coupon){
    global $woocommerce;
    $valid = true;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
       // if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product
            // get the stock quantity - returns the available amount number
            $stock_info = $values['data']->get_stock_quantity();

            if($stock_info < 1){
                $vaild = false;

                break;
            }
        }
    // give error message...

    return $valid ; 
}

I don't understand why this option is not built into woocommerce to begin with. We want to blow out what we have in inventory but also take backorders on our products but, we do not want to give a discount to any backorders.

Any help would be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Shinosky
  • 89
  • 1
  • 10

1 Answers1

2

There is a typo error in your code for $vaild = false; (wrong variable name should be $valid) and an exta } as you have commented an if statement, causing the error.

Also below, I have upgraded your code to a more actual version (replacing global $woocommerce and $woocommerce->cart by WC()->cart):

add_filter( 'woocommerce_coupon_is_valid', 'coupon_always_valid', 99, 2 );
function coupon_always_valid( $valid, $coupon ){

    $valid = true;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product
        // get the stock quantity - returns the available amount number
        $stock_info = $cart_item['data']->get_stock_quantity();

        if( $stock_info < 1 ){
            $valid = false; ## <== HERE a typo error
            break;
        }
        // } ## <== HERE commented
    }
    return $valid ;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Now this code should work without errors

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Awesome! That works. Thanks! Now I'm going to try to extend on it by: Updating the error explaining why the coupon code doesn't work AND Allowing the coupon to work on products that are not backordered. Right now - the coupon code will not be applied if one product is back ordered but others are not. – Shinosky Aug 14 '17 at 19:41
  • 1
    @Shinosky have you done it? i have the same issue. i also want the coupon to be applied only for non backorder products. if you done this please share the code. thank you. – Dhan Jul 05 '18 at 05:34
  • Code is great but it removes coupon from cart if there's some ineligible item — even if there are more items that are eligible for coupon. – Demuri Celidze Apr 13 '19 at 17:22
  • @DemuriCelidze This code is made on the question requirements and answer that requirements… For your use you will need to adapt it. – LoicTheAztec Apr 13 '19 at 17:25
  • @LoicTheAztec I'd appreciate if you can help with customization. I'm completely lost trying to find the way how to set coupon for in-stock items without disabling it if backorder items are in cart too. – Demuri Celidze Apr 13 '19 at 17:34