Disable Purchase for All But 1 WooCommerce Product

Swap out the 120 in the code with your product id. You can grab the product id from your products page in admin or hover over the edit product link in your WordPress menu and you’ll see the product id in the bottom of your browser window.

The code disables all products for sale by allowing only the product with ID 120 to be purchased. The add to cart buttons on all other shop page products is disabled and changed to read more so they can’t be added to the cart. On the single product page, products have the add to cart button removed.

Copy and paste the code to the end of your child themes functions file.

add_filter( 'woocommerce_is_purchasable', 'allow_only_specific_product_purchase', 10, 2 );
function allow_only_specific_product_purchase( $purchasable, $product ) {
// Replace 120 with the ID of the product you want to allow purchasing
$allowed_product_id = 120;
// Check if the current product is the allowed product
if ( $product->get_id() === $allowed_product_id ) {
$purchasable = true;
} else {
$purchasable = false;
}
return $purchasable;
}

The code can be extended to do anything you like.

Need More Coding Help?

Book a Consultation

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.