Checking that one of multiple mandatory products categories are in cart

前端 未结 1 1558
难免孤独
难免孤独 2020-12-21 11:33

I have to check if one of my two mandatory products categories is in cart.

I have customized the code from this answer:
Allow checkout only when a product of a m

相关标签:
1条回答
  • 2020-12-21 11:49

    Updated (on April 2018)

    You can only use partially the code you have picked up from my answer, for 2 or more categories, using an array of product categories this way:

    // 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
    

    But you will have to choose for the last function, between one of the 2 product categories for the redirection…

    // 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');
    

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

    This code is based on this functional answer:
    Allow checkout only when a product of a mandatory category is in cart

    It is untested, but it should work…

    0 讨论(0)
提交回复
热议问题