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?
Was This Tutorial Helpful?