Halloween Sale 15% off Ends in See the plans
PHP

"Available In Store Only" Products by Category

Lets customers browse products from chosen categories online but removes the Add to Cart button and shows an "available in store only" message instead.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_store_only = array(
    'categories' => array( 'furniture' ),                                      // Category slugs.
    'message'    => 'Available in our showroom only. Call us to reserve one.', // Shown instead of Add to Cart.
);

$wpide_store_only_match = function ( $product ) use ( $wpide_store_only ) {
    $id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();

    return has_term( $wpide_store_only['categories'], 'product_cat', $id );
};

add_filter( 'woocommerce_is_purchasable', function ( $purchasable, $product ) use ( $wpide_store_only_match ) {
    return $purchasable && ! $wpide_store_only_match( $product );
}, 10, 2 );

add_action( 'woocommerce_single_product_summary', function () use ( $wpide_store_only, $wpide_store_only_match ) {
    global $product;

    if ( $product instanceof WC_Product && $wpide_store_only_match( $product ) ) {
        printf( '<p class="wpide-store-only">%s</p>', esc_html( $wpide_store_only['message'] ) );
    }
}, 31 );

add_filter( 'woocommerce_product_add_to_cart_text', function ( $text, $product ) use ( $wpide_store_only_match ) {
    return $wpide_store_only_match( $product ) ? 'View details' : $text;
}, 10, 2 );

Notes

Products stay visible with their price; add your opening hours or address to the message.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.