【问题标题】:Mandatory coupon for specific product categories avoiding checkout in Woocommerce特定产品类别的强制性优惠券,避免在 Woocommerce 中结帐
【发布时间】:2017-06-06 14:03:40
【问题描述】:

我希望使用 woocommerce 智能优惠券创建“邀请码”。为此,我希望特定产品在购物车/结帐时需要优惠券代码。我已经看到了这样做的方法,但是它们会影响所有产品,而不仅仅是我想要的产品。

我正在尝试将返回的变量传递给另一个函数,但是我不知道该怎么做。

这是最新版本:

            //Check to see if user has product with a specific product category in cart    
    function check_product_in_cart() {

        global $woocommerce;

        //assigns a default negative value
        //  categories targeted 87, 18, 19

        $product_in_cart = false;

        // start of the loop that fetches the cart items

        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );

            // second level loop search, in case some items have several categories
            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
                if (( $_categoryid === 87 ) || ( $_categoryid === 18 ) || ( $_categoryid === 19 )) {
                    //category is in cart!
                    $product_in_cart = true;
                }
            }
        }

        return $product_in_cart;
   }  

// Force Coupon codes for Woocommerce
 add_action('woocommerce_check_cart_items', 'force_coupon_code');

    function force_coupon_code( $product_in_cart )
    {
        global $woocommerce;
        if(is_cart() || is_checkout()){
            $my_coupon = $woocommerce->cart->applied_coupons;
            echo $woocommerce->cart->get_applied_coupons;
            if(empty($my_coupon))
            {
                wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'A coupon code is mandatory for this product.', 'woocommerce' ), 'error' );
            }
        }
    }

第一部分应该退回产品 $product_in_cart,第二部分强制使用优惠券。这是我正在使用的代码的来源。 Mandatory Coupon code 和 Checking products in cart based on category。我读到将变量放在新函数 args 中可能会实现这个like so,但它绝不工作。

【问题讨论】:

    标签: php wordpress woocommerce categories coupon


    【解决方案1】:

    更新于 2018 年 4 月 1 日

    您使用的代码已过时且复杂。这可以简单地在一个挂钩函数中完成,使用 Wordpress has_term() 条件函数来检测购物车项目是否属于特定产品类别。

    因此您的代码将更加紧凑和高效:

    // Force Coupon codes for Woocommerce
    add_action('woocommerce_check_cart_items', 'mandatory_coupon_code');
    function mandatory_coupon_code()
    {
            // set Here your categories IDs, slugs or names
            $categories = array(18,19,87);
            $found = false;
    
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) 
                if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
                    // Product category found in cart items
                    $found = true;
                    // Exit from loop
                    break;
                }
            }
            $coupons = WC()->cart->get_applied_coupons();
    
            // The Notice is displayed for that product categories when no mandatory coupon has been entered
            if( count( $coupons ) > 0 && $found )
                wc_add_notice( __( 'A coupon code is mandatory for this product.', 'woocommerce' ), 'error' );
        }
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。


    版本适用于特定优惠券代码且无产品类别(适用于所有产品):

    add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
    function mandatory_coupon_code() {
        // HERE set your coupon code
        $mandatory_coupon = 'spring10';
    
        $applied_coupons = WC()->cart->get_applied_coupons();
    
        // If coupon is found we exit
        if( in_array( $mandatory_coupon, $applied_coupons ) ) return;
    
        // Not found: display an error notice
        wc_add_notice( __( 'Please enter coupon code to checkout.', 'woocommerce' ), 'error' );
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。

    【讨论】:

    • 漂亮!很有魅力。我还在学习,所以除了更好的代码之外,if(empty($my_coupon) &amp;&amp; $has_cat) 这行将它们联系在一起。从理论上讲,我可以在语句中添加更多的 args 并进一步缩小范围,例如if(empty($my_coupon) &amp;&amp; $has_cat) &amp;&amp; $color_blue &amp;&amp; etc...?
    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 2021-01-03
    • 2019-01-12
    • 1970-01-01
    • 2021-04-13
    • 2019-01-14
    • 2016-02-25
    • 2014-09-08
    相关资源
    最近更新 更多