如果WooCommerce购物车项目有待补货,请不要使用优惠券 [英] If WooCommerce Cart items are on backorder do not apply coupon

查看:60
本文介绍了如果WooCommerce购物车项目有待补货,请不要使用优惠券的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这就是我所拥有的:

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 ; 
}

我不明白为什么woocommerce并未内置此选项。我们想吹销库存中的商品,但也要对我们的产品进行补货,但是,我们不想给任何补货打折。

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.

任何帮助都会赞赏。

推荐答案

您的代码中有 $ vaild = false;的错字错误 c>(错误的变量名称应为 $ valid )和一个扩展名 } ,因为您已注释了if语句,导致错误

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.

下面,我将您的代码升级到了更实际的版本(替换了 global $ woocommerce 和<$ c由 WC()-> cart 的$ c> $ woocommerce-> cart ):

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 ;
}

代码包含在您活动的子主题的function.php文件中(

现在此代码应该可以正常运行

这篇关于如果WooCommerce购物车项目有待补货,请不要使用优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆