【问题标题】:Checking that one of multiple mandatory products categories are in cart检查多个强制性产品类别之一是否在购物车中
【发布时间】:2017-03-18 19:28:02
【问题描述】:

我必须检查我的两个强制性产品类别之一是否在购物车中。

我已经根据这个答案自定义了代码:
Allow checkout only when a product of a mandatory category is in cart

但是使用我的自定义代码时,它总是只检查其中一个,我得到了一些错误。我的代码不起作用。

如何使它适用于 2 个产品类别而不是一个?

代码如下:

 // Function that define the mandatory product category
         function your_mandatory_category_slug(){
        // DEFINE HERE the SLUG of the needed product category
            $category = 'cxsuite-download-option';
            return $category;
         }

         function your_mandatory_category_slug_h(){

             // DEFINE HERE the SLUG of the needed product category
            $category_h = 'cxsuite-hosted-option';
            return $category_h;
         }
    // Conditional function that returns true if the mandatory product category is in cart
    function has_mandatory_category(){
        $category_needed = your_mandatory_category_slug();
       $category_needed_h = your_mandatory_category_slug_h();
        $has_cat = false;

        // Iterrating each item in cart and detecting…
        foreach ( WC()->cart->get_cart() as $item ) {

            // Detects if the needed product category is in cart items
            if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
                $has_cat = true;
                break;
            }
            elseif ( has_term($category_needed_h, 'product_cat', $item['product_id'] ) ) {
                $has_cat = true;
                break;
            }
        }
        return $has_cat;
     }
// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
        $category_needed = your_mandatory_category_slug();
       $category_needed_h = your_mandatory_category_slug_h();


    // check that cart is not empty (for cart and product category archives)
    if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) || is_product_category( $category_needed_h ) ) ){
        $category_needed_single=null;
        if( $category_needed=='cxsuite-download-option' ){
            $category_needed_single='cxsuite-download-option';
        }{
            $category_needed_single=$category_needed_h;
        }
        $category_obj = get_term_by( 'slug', $category_needed_single, 'product_cat' );

        if ( is_wp_error( $category_obj ) ) return;


        // Display message when product category is not in cart items
        if ( !has_mandatory_category() ) {
            $category_name = $category_obj->name;
            $category_url = get_term_link( $category_needed_single, 'product_cat' );

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( __( '<strong>Reminder:</strong> You have chosen Addon that can only be purchased together with a %1$s. Please add a %1$s (full or trial) before checking out. Please return <a href="%2$s"> here to "%1$s" product page</a>', 'your_theme_domain'), $category_name, $category_url ), '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 cat page


// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {

    // If cart is not empty on checkout page
    if( !WC()->cart->is_empty() && is_checkout() ){
        $category_needed = your_mandatory_category_slug();
        $category_needed_h = your_mandatory_category_slug_h();
        $category_needed_single=null;
        if(is_product_category( $category_needed )){
            $category_needed_single=$category_needed;
        }else{
            $category_needed_single=$category_needed_h;
        }

        // If missing product category => redirect to the products category page
        if ( !has_mandatory_category() )
            wp_redirect( get_term_link( $category_needed_single, 'product_cat' ) );
    }

}
add_action('template_redirect', 'mandatory_category_checkout_redirect');

【问题讨论】:

    标签: php wordpress woocommerce categories cart


    【解决方案1】:

    更新 (2018 年 4 月)

    您只能部分使用从我的答案中获取的代码,用于 2 个或更多类别,以这种方式使用产品类别数组:

    // 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;
    }
    
    // Conditional function that returns true if the mandatory products categories are in cart
    function has_mandatory_category(){
        $categories_needed = your_mandatory_category_slug();
        $has_cat = false;
    
        // Iterrating each item in cart and detecting…
        foreach ( WC()->cart->get_cart() as $item ) {
    
            // iterating both categories
            foreach ( $categories_needed as $category_needed ) {
    
                // Detects if one of the needed product categories is in cart items
                if ( has_term($category_needed, 'product_cat', $item['product_id'] ) )
                    $has_cat = true;
            }
            if($has_cat) 
                break;
        }
        return $has_cat;
    }
    
    // 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
    

    但您必须选择最后一个功能,在 2 个产品类别之一之间进行重定向……

    // Function that redirect from checkout to mandatory product category archives pages
    function mandatory_category_checkout_redirect() {
    
        // If cart is not empty on checkout page
        if( !WC()->cart->is_empty() && is_checkout() ){
            $categories_needed = your_mandatory_category_slug();
    
            // If missing product category => redirect to the products category page
            if ( !has_mandatory_category() )
                wp_redirect( get_term_link( $categories_needed[0], 'product_cat' ) ); // or $categories_needed[1]
        }
    }
    add_action('template_redirect', 'mandatory_category_checkout_redirect');
    

    这在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码基于此功能性答案:
    Allow checkout only when a product of a mandatory category is in cart

    它未经测试,但它应该可以工作......

    【讨论】:

    • 嗨,谢谢它做得很好,但在最后一个功能中,我更改为重定向回商店页面,因为客户如果他不能选择另一种矿石,我认为更好的解决方案是去商店页面。再次感谢。
    • @LoicTheAztec 我阅读了您的其他评论以提出一个新问题。后来我发现你已经在这里发布了超过 1 个类别的答案。这个不适合我。我在这一行收到一个错误:if( !WC()-&gt;cart-&gt;is_empty() &amp;&amp; ( is_cart() || is_product_category( $categories_needed[0] || is_product_category( $categories_needed[1] ) ) ){ 我收到一个错误,即存在意外的“{”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2017-03-26
    • 2011-04-09
    • 1970-01-01
    • 2018-10-13
    • 2017-09-27
    • 2021-05-15
    相关资源
    最近更新 更多