• Resolved rifatspir

    (@rifatspir)


    Restrict WooCommerce to allowing order from specific categories or products

    I want to restrict my customer to allow order from specific categories or some specific products. I have more than 1800+ products I want to sell few items this week.

    So instead stocking out all of them, I want to show custom message when they click on restricted product add to cart button, allow only these selected items or categories to make purchase.

    Is there any plugin or code to help with that?

    • This topic was modified 4 years, 7 months ago by rifatspir.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    > Is there any plugin or code to help with that?

    Yes, there is. You can look for WooCommerce Catalogue Visibility or alternatively try this custom code: https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/

    Kind regards,

    Thread Starter rifatspir

    (@rifatspir)

    Thanks @conschneider. Problem solved, found answer in stackoverflow.

    function sv_wc_prevent_checkout_for_category() {
    
        // set the slug of the category for which we disallow checkout
        $category = 'sibling';
    
        // get the product category
        $product_cat = get_term_by( 'slug', $category, 'product_cat' );
    
        // sanity check to prevent fatals if the term doesn't exist
        if ( is_wp_error( $product_cat ) ) {
            return;
        }
    
        $category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';
    
        // check if this category is the only thing in the cart
        if ( sv_wc_is_category_alone_in_cart( $category ) ) {
    
            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name ), 'error' );
        }
    }
    add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );
    
    /**
     * Checks if a cart contains exclusively products in a given category
     * 
     * @param string $category the slug of the product category
     * @return bool - true if the cart only contains the given category
     */
    function sv_wc_is_category_alone_in_cart( $category ) {
    
        // check each cart item for our category
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
            // if a product is not in our category, bail out since we know the category is not alone
            if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
                return false;
            }
        }
    
        // if we're here, all items in the cart are in our category
        return true;
    }
    Plugin Support con

    (@conschneider)

    Engineer

    LGTM @rifatspir
    Thanks for sharing!

    All the best.

    Use this code

        /*
        Plugin Name: Remove 'Add to cart' conditionally
        Plugin URI:  https://www.damiencarbery.com/2020/03/remove-add-to-cart-conditionally/
        Description: Conditionally remove the 'Add to cart' button in WooCommerce.
        Author: Damien Carbery
        Version: 0.2
        */
        
        
        class IsPurchasableConditionalFiltering {
        	// A reference to an instance of this class.
        	private static $instance;
        	// Store whether 'Add to cart' button should be displayed.
        	private $purchasable;
        
        
        	// Returns an instance of this class. 
        	public static function get_instance() {
        		if ( null == self::$instance ) {
        			self::$instance = new IsPurchasableConditionalFiltering;
        		}
        		return self::$instance;
        	}
        
        
        	// Initialize the plugin variables.
        	public function __construct() {
        		$this->purchasable = array();
        
        		$this->init();
        	}
        
        
        	// Set up WordPress specfic actions.
        	public function init() {
        		add_filter( 'woocommerce_is_purchasable', array( $this, 'is_purchasable_conditionals' ), 10, 2 );
        		add_filter( 'woocommerce_variation_is_purchasable', array( $this, 'variation_is_purchasable_conditionals' ), 10, 2 );
        		
        		// Remove variations dropdown and 'Add to cart' button for variable products.
        		add_action( 'woocommerce_before_single_product_summary', array( $this, 'before_single_product_summary' ) );
        	}
        	
        	
        	public function is_purchasable_conditionals( $whether_purchasable, $product ) {
        		// Return cached result.
        		if ( array_key_exists( $product->get_id(), $this->purchasable ) ) {
        			return $this->purchasable[ $product->get_id() ];
        		}
        		
        		// Only do our conditional checks if WooCommerce deems the item to be purchasable.
        		if ( $whether_purchasable ) {
        			$result = true;  // Default to allowing purchase.
        			
        			// Check our specific conditions - some examples.
        			/* // Product over a certain price.
        			if ( $product->get_price() > 2 ) {
        				$result = false;
        			}*/
        			
        			// Check if product in a certain categores.
        			if ( has_term( array( 'hoodies', 'accessories' ), 'product_cat', $product->get_id() ) ) {
        				$result = false;
        			}
        			
        			
        			$this->purchasable[ $product->get_id() ] = $result;
        		}
        		else {
        			// Store that this item cannot be purchased.
        			$this->purchasable[ $product->get_id() ] = false;
        		}
        
        		return $this->purchasable[ $product->get_id() ];
        	}
        	
        	
        	public function variation_is_purchasable_conditionals( $whether_purchasable, $product ) {
        		return $whether_purchasable;
        	}
        	
        	
        	public function before_single_product_summary() {
        		$product_id = get_the_ID();
        		if ( array_key_exists( $product_id, $this->purchasable ) && !$this->purchasable[ $product_id ] ) {
        			remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        			remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 ); 
        		}
        	}
        }
        $IsPurchasableConditionalFiltering = new IsPurchasableConditionalFiltering;

    Reference : https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/

    • This reply was modified 4 years, 2 months ago by ajanthalakmal.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restrict WooCommerce to allowing order from specific categories or products’ is closed to new replies.