Remove add to cart button in WooCommerce while disabling or keeping purchase functionality
remove add-to cart button woocommerce

Remove add to cart button in WooCommerce while disabling/keeping purchase functionality

The are millions of articles that showing you the “right” way of removing the add to cart button, while some of them are good, I keep seeing people asking for more specific solutions.

In this article, I will share with you all the possible ways of how to remove the add to cart button in WooCommerce while keeping/disabling the option to purchase a product.

Remove add to cart button from all pages and disabled purchase functionality

add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS

That is the most correct method on how to fully disable purchase functionality website wide and removing any add to cart button from any page.

Any theme, script or plugin that uses the purchase functionality of WooCommerce, directly or indirectly, will stop working.

Hide add to cart button from shop page/archive while keeping purchase functionality

function react2wp_is_shop_remove_add_to_cart_button() {
   if ( is_shop() ) {
      add_filter( 'woocommerce_is_purchasable', '__return_false' );
   }
}

add_action( 'wp_head', 'react2wp_is_shop_remove_add_to_cart_button' );

Sometimes, you want to remove the add to cart button from the shop page(WooCommerce archive) while still keeping the purchase functionality on other areas of the website.

Remove add to cart button from category page while keeping purchase functionality

function react2wp_is_shop_remove_add_to_cart_button() {
   if ( is_product_category() ) {
      add_filter( 'woocommerce_is_purchasable', '__return_false' );
   }
}

add_action( 'wp_head', 'react2wp_is_shop_remove_add_to_cart_button' );

The same as removing the add to cart button from the shop page with a slight change to remove it from the category page instead. It is possible to use is_shop and is_product_category together, to do that, just change the if to if ( is_shop() || is_product_category() ).

If you want, you can hide the button on the specific category page(while keeping functionality)

function react2wp_is_shop_remove_add_to_cart_button() {
   if ( is_product_category( 'category_slug' ) ) {
      add_filter( 'woocommerce_is_purchasable', '__return_false' );
   }
}

add_action( 'wp_head', 'react2wp_is_shop_remove_add_to_cart_button' );

Remove add to cart button from single product page while keeping purchase functionality

function react2wp_is_shop_remove_add_to_cart_button() {
   if ( is_product() ) {
      add_filter( 'woocommerce_is_purchasable', '__return_false' );
   }
}

add_action( 'wp_head', 'react2wp_is_shop_remove_add_to_cart_button' );

Removing add to cart button while still keeping purchase functionality has really rare use-cases. You would want to use this code only if you would like to allow purchases through other areas of the website(like a plugin) but not through the purchase page directly.

To summarize

There are many ways of removing/hiding the add to cart button with or without disabling purchase functionality, pick the one that you need or maybe merge two solutions into one(like using is_shop with is_product_category in the same function).

If you find that something is not working the way I wrote or just having troubles, leave a comment and I’ll try to reply as soon as possible.

  • Function: add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
  • Description:
    Hook a function or method to a specific filter action.
  • Args:
    • (string) $tag The name of the filter to hook the $function_to_add callback to.
    • (callable) $function_to_add The callback to be run when the filter is applied.
    • (int) $priority Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
    • (int) $accepted_args The number of arguments the function accepts.
  • Return:
    • (true)
  • Function: add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
  • Description:
    Hooks a function on to a specific action.
  • Args:
    • (string) $tag The name of the action to which the $function_to_add is hooked.
    • (callable) $function_to_add The name of the function you wish to be called.
    • (int) $priority Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
    • (int) $accepted_args The number of arguments the function accepts.
  • Return:
    • (true) Will always return true.
  • Function: remove_action( string $tag, callable $function_to_remove, int $priority = 10 )
  • Description:
    Removes a function from a specified action hook.
  • Args:
    • (string) $tag The action hook to which the function to be removed is hooked.
    • (callable) $function_to_remove The name of the function which should be removed.
    • (int) $priority The priority of the function.
  • Return:
    • (bool) Whether the function is removed.
  • Function: is_product
  • Description:
    Is_product - Returns true when viewing a single product.
  • Return:
    • boolean bool
  • Function: is_product_category
  • Description:
    Is_product_category - Returns true when viewing a product category.
  • Args:
    • string $term = '' (default: '') The term slug your checking for. Leave blank to return true on any.
  • Return:
    • boolean bool
  • Function: is_shop
  • Description:
    Is_shop - Returns true when viewing the product type archive (shop).
  • Return:
    • boolean bool

More about: Best WordPress web hosting 2019  

Leave a Reply

Your email address will not be published. Required fields are marked *