0

I'm using WordPress with WooCommerce to sell physical products to customers online.

I would like to have either specific products or rather a specific category of products as "available in-store only" thus not displaying the "add to cart" button for those products, forcing the customers to get in touch with me or to physically come into my store to buy those products.

Some of these products are using variations (colors, options, etc) with different prices so I need to keep those products as "variable products".

Would love to have ideas / solutions to this as I can't find any. Thanks!

  • 1
    This plugin will do that for you but still will generate order in case the store track quantities etc - https://woocommerce.com/products/local-pickup-plus/ . The other option is to create custom stock status. You can check this answer here - https://stackoverflow.com/questions/61796640/how-to-add-custom-stock-status-to-products-in-woocommerce-4 – Snuffy Aug 18 '22 at 13:20
  • Thanks @MartinMirchev. I'll check the custom stock statuses, as I believe this plugin allows me to specify I want the customer to pick up the item in store, but what I need is to avoir any order of those specific items on the website (actually, items above a certain price). – Axel Achten Aug 18 '22 at 14:08
  • 1
    You can do it with the `woocommerce_is_purchasable` hook. Like https://stackoverflow.com/a/53147152/11656450 – Moshe Gross Aug 18 '22 at 21:49
  • Thanks @MosheGross, that's quite helpful too! – Axel Achten Aug 20 '22 at 08:22

1 Answers1

1

You can check for category slug or id, and filter woocommerce_is_purchasable like so:

function BN_restrict_store_only( $purchasable, $product ){

//The category by slug
$pickup_prod_cat_slug = 'available-in-store-only'; // slug
//The category by id
$pickup_prod_cat_ids ='681';  // the id of the product cat

 // For variations (
    if ( $product->is_type('variation') ) {
        $parent = wc_get_product( $product->get_parent_id() );
          $product_id = $parent->get_id();
        if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
             $purchasable = false;
        } 
    }
       // For simple and other product types
    else {
   $product_id = $product->get_id();
          if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
       
             $purchasable = false;
         }
    }
    return $purchasable;
}
        
add_filter( 'woocommerce_is_purchasable', 'BN_restrict_store_only', 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'BN_restrict_store_only', 10, 2 );

Works both with simple products and variations. You have to make a category with the slug "available-in-store-only" or get the id/slug of whatever product cat you want to use.

File goes in the functions.php of your child theme, or CodeSnippets.

tiny
  • 447
  • 4
  • 18