The WooCommerce product catalog mode allows you to display your products without the ability to purchase them. This can be useful in scenarios where you want to showcase your products but don’t want to enable the shopping functionality.
To achieve this, you can use the wpsnippets_disable_add_to_cart()
function, which removes the “Add to Cart” button and disables the purchase functionality for all products on your WooCommerce site.
function wpsnippets_disable_add_to_cart() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'init', 'wpsnippets_disable_add_to_cart' );
By adding this code snippet to your theme’s functions.php
file or a custom plugin, the “Add to Cart” button will no longer be displayed on both the product listing page and the single product page.
This code snippet is useful when you want to showcase your products without allowing customers to make purchases directly from your website. It can be handy for businesses that prefer to handle orders through other channels, such as phone or email.
Examples
Example #1: Setting WooCommerce to Product Catalog Mode
This use case demonstrates how to set WooCommerce to product catalog mode, which allows you to disable the shopping cart functionality and only showcase your products without the ability to purchase them.
function wpsnippets_disable_cart() {
add_filter( 'woocommerce_add_to_cart_validation', '__return_false', 999 );
remove_action( 'woocommerce_before_cart', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'init', 'wpsnippets_disable_cart' );
This code snippet disables the add to cart functionality and removes related actions and hooks associated with the cart, checkout, cross-sells, and add to cart buttons. It should be added to your theme’s functions.php
file or a custom plugin.
Example #2: Hiding Price and Add to Cart Button
This use case demonstrates how to hide the price and add to cart button for all products in WooCommerce.
function wpsnippets_hide_price_add_to_cart() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'init', 'wpsnippets_hide_price_add_to_cart' );
This code snippet removes the actions responsible for displaying the add to cart button and price on both the shop page and single product page. It should be added to your theme’s functions.php
file or a custom plugin.
Example #3: Custom “Enquire Now” Button
This use case demonstrates how to replace the add to cart button with a custom “Enquire Now” button for specific products in WooCommerce.
function wpsnippets_custom_enquire_button() {
global $product;
if ( $product->is_type( 'simple' ) && $product->get_price() > 0 ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'wpsnippets_custom_enquire_button_output', 30 );
}
}
function wpsnippets_custom_enquire_button_output() {
echo '<a href="mailto:sales@example.com" class="button">Enquire Now</a>';
}
add_action( 'woocommerce_single_product_summary', 'wpsnippets_custom_enquire_button' );
This code snippet checks if the product is a simple product with a price greater than zero, then it removes the default add to cart button and adds a custom “Enquire Now” button in its place. The button links to a specified email address. It should be added to your theme’s functions.php
file or a custom plugin.