Avoid add to cart for specific product categories if user is unlogged in Woocommerce

后端 未结 2 385
孤独总比滥情好
孤独总比滥情好 2020-12-21 18:35

In Woocommerce, I am trying to disable specific product categories to be added to the cart for users that aren\'t logged in. i\'m looking for a solution the last couple of d

相关标签:
2条回答
  • 2020-12-21 19:09

    The conditional tag is_product_category() only target product category archive pages. Instead you can use WordPress conditional function has_term().

    There is 2 ways to avoid specific products being added to cart for no logged user…

    1) Using Add to cart validation hook:

    // Avoid add to cart conditionally
    add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_conditionally', 20, 3 );
    function avoid_add_to_cart_conditionally( $passed, $product_id, $quantity) {
        // HERE your product categories (can be IDs, slugs or names terms)
        $terms = array( 'gekoelde-bier', 'bierkoerier');
        
        if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
            // Displaying a custom notice (optional)
            wc_add_notice( __('Only logged in users are allowed to purchase this item. Please register.'), 'error' );
            
            $passed = false;
        }
        
        return $passed;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    2) Using is_purchasable product property (It will remove add to cart button):

    add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
    function conditional_purchasable_products( $is_purchasable, $product ) {
        // HERE your product categories (can be IDs, slugs or names terms)
        $terms = array( 'gekoelde-bier', 'bierkoerier');
        
        $product_id = $product->get_id(); // The product ID
    
        if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
            $is_purchasable = false;
        }
    
        return $is_purchasable;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    Targeting the parent product category terms too.

    You will use the following custom conditional function to replace has_term() Wordpress function:

    // Custom conditional function that checks for parent product categories too
    function has_product_categories( $categories, $product_id ) {
         // Initializing
        $parent_term_ids = $categories_ids = array();
        $taxonomy        = 'product_cat';
    
        // Convert categories term names and slugs to categories term ids
        foreach ( $categories as $category ){
            if( is_numeric( $category ) ) {
                $categories_ids[] = (int) $category;
            } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
                $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
            }
        }
    
        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
            if( $term->parent > 0 ){
                $parent_term_ids[] = $term->parent; // Set the parent product category
                $parent_term_ids[] = $term->term_id; // (and the child)
            } else {
                $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
            }
        }
        return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
    }
    

    Then for both hooked functions, you will replace the following line :

    if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
        
    

    By this line:

    if( has_product_categories( $terms, $product_id ) && ! is_user_logged_in() ){
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    0 讨论(0)
  • 2020-12-21 19:23

    woocommerce_is_purchasable filter can do the job. It checks if a product is purchasable or not. If not purchasable, "add-to-cart" button is removed and can't be purchased.

    So, here you will need to check if user is logged in or not, and product belongs to certain category, before disabling purchase ability.

    This is how you can do it:

    function wpso9800_remove_cart_button( $is_purchasable, $product ) {
        //when logged in
        if ( is_user_logged_in() ) {
            return $is_purchasable;
        }
    
        //get categories
        $categories = get_the_terms( $product->id, 'product_cat');
        $my_terms_ids = array ( 1, 2 );//product category IDs
    
        if ($categories && !is_wp_error($categories)) {
            foreach ($categories as $cat) {
                if ( in_array($cat->term_id, $my_terms_ids ) ) {
                    return false;
                }
                return $is_purchasable;
            }
        }
    }
    add_filter('woocommerce_is_purchasable','wpso9800_remove_cart_button', 10, 2);
    

    **This is tested, and working.

    Note: By default, $is_purchasable is set to true. You will need to return false, when you want to turn off purchasing.

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