【问题标题】:Check that one of multiple mandatory products categories are in Woocommerce cart检查多个强制性产品类别之一是否在 Woocommerce 购物车中
【发布时间】:2018-04-20 22:05:55
【问题描述】:

这是一个新问题,要求与以下已给出的答案相同:
Checking that one of multiple mandatory products categories are in cart

但它似乎对我不起作用,我在这一行得到一个错误:

if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( 
$categories_needed[0] || is_product_category( $categories_needed[1] ) ) 
){ 

我收到错误消息,提示有 unexpected {

感谢任何帮助。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    只有两个小错误:

    1) 在return $categories 末尾缺少;,因此在此更新后的代码中:

    // Function that define the mandatory product category
    function your_mandatory_category_slug(){
        // DEFINE HERE the 2 SLUGs of the needed product categories
        $categories = array('cxsuite-download-option','cxsuite-hosted-option');
    
        return $categories; // <== HERE
    }
    

    2) is_product_category( $categories_needed[0] 中缺少关闭 ) 所以在这个更新的代码中:

    // Function that display a message if there is not in cart a mandatory product category
    function mandatory_category_display_message() {
        $categories_needed = your_mandatory_category_slug();
    
        // check that cart is not empty (for cart and product category archives)
        if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $categories_needed[0] ) || is_product_category( $categories_needed[1] ) ) ){
            $category_name = array();
            $category_url = array();
    
            // iterating both categories
            foreach($categories_needed as $key => $category_needed){
                $category_obj = get_term_by( 'slug', $category_needed, 'product_cat' );
                if ( is_wp_error( $category_obj ) )
                    return;
    
                $category_name[$key] = $category_obj->name;
                $category_url[$key] = get_term_link( $category_needed, 'product_cat' );
            }
            // Display message when one of the product categories is not in cart items
            if ( !has_mandatory_category() ) {
                // render a notice to explain why checkout is blocked
                wc_add_notice( sprintf( __( '<strong>Reminder:</strong> You have to add in your cart, a product from "%1$s" or from "%3$s" category, to be allowed to check out. Please return <a href="%2$s"> here to "%1$s"</a> or <a href="%4$s"> here to "%3$s"</a> product pages', 'your_theme_domain'), $category_name[0], $category_url[0], $category_name[1], $category_url[1] ), 'error' );
            }
        }
    }
    add_action( 'woocommerce_before_main_content', 'mandatory_category_display_message', 30 ); // for product mandatory category archives pages
    add_action( 'woocommerce_check_cart_items', 'mandatory_category_display_message' ); // for cart page
    

    现在它应该可以正常工作了……

    【讨论】:

    • 感谢您的时间和精力!实现它,它的工作原理!
    猜你喜欢
    • 2017-03-18
    • 2017-03-26
    • 2018-12-10
    • 2018-10-13
    • 2014-04-29
    • 2019-06-15
    • 2017-05-06
    • 1970-01-01
    • 2017-07-13
    相关资源
    最近更新 更多