Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to Hide the ‘Add to Cart’ Button in WooCommerce the RIGHT way!

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 10, 2024
5 minutes read
How to Hide the ‘Add to Cart’ Button in WooCommerce the RIGHT way!

Running a WooCommerce online store? Obviously, you want that your customers can easily add your products to their shopping cart, isn’t it? Then why would you want to hide the ‘Add to Cart’ button in WooCommerce? Let us first understand why this need might arise and then learn how to go about it.

There are plenty of instances when you would want to hide the WooCommerce ‘Add to Cart’ button. Here are a few of them:

  • You are using WooCommerce simply as an online catalog of products, not for making sales.
  • The displayed product is not yet available in the market.
  • The displayed product is presently out of stock.
  • The product is only available for sale for select customers (for example, those registered on the WooCommerce store).
  • The WooCommerce ‘Add to Cart’ button is not working due to some technical problem.

Now that we have listed the reasons, let’s discuss why a simple Google search will not provide the answer to the question – “How do you hide the ‘Add to Cart’ button on WooCommerce?”

Search Results can guide you – but not always in the right direction

Try searching for the keywords, “remove add to cart button WooCommerce” on Google – and you will find a couple of credible search results. Most of these search results will guide you to remove the following functions using the “Remove Action” function:

  • woocommerce_template_loop_add_to_cart
  • woocommerce_template_single_add_to_cart

However, doing this will disable the ‘Add to Cart’ button on your WooCommerce website entirely (on all pages). The first action above effectively disables the “Add to Cart” button on a particular product page, while the second action implements the same outcome on the shop page.

Another major problem with this action is that besides hiding the “Add to Cart” button, it can also remove some WooCommerce template files, which is unnecessary. We would recommend that before reusing this “Remove Action” code, first understand what each line of code does – and then go ahead if required.

Instead of using this function, there are far simpler and safer ways on WooCommerce to hide the ‘Add to Cart’ button. Let’s discuss them in the following sections.

Hiding the ‘Add to Cart’ button in WooCommerce – the RIGHT way!!

Here is how you can hide the ‘Add to Cart’ button the right way for each of the following cases:

  • To remove ‘Add to Cart’ for a single unpurchaseable product For the selected product, open the “simple.php” file (from the single-product/add-to-cart folder) and set the “is_purchaseable” Boolean variable to “False”. This action will disable the “Cart” button and replace it with the “Read more…” button
    
    if ( ! $product->is_purchasable() ) {
        return;
    }
    
    // For a specific product, override the is_purchasable to false
    if ( $product->get_id() == 1234 ) { // Replace 1234 with your product ID
        $product->set_is_purchasable(false);
    }
    

    .

  • To disable the ‘Add to Cart’ button for specific products , you can choose to hide the “Add to Cart” button for specific products (or product pages).
    • To do this, you can simply remove the price figure for the product, which will also remove the “Add to Cart” button.
    • Another option is to set the product stock level to zero, thus automatically disabling the Cart button.
    • Alternatively, you can open the “functions.php” file (from the “theme” folder) and add a filter to disable the “purchaseable” attribute for a specific product ID.
      
      add_filter( 'woocommerce_is_purchasable', 'custom_disable_add_to_cart_button', 10, 2 );
      
      function custom_disable_add_to_cart_button( $is_purchasable, $product ) {
          // Replace '1234' with the ID of the product you want to disable the 'Add to Cart' button for
          if ( $product->get_id() == 1234 ) {
              return false;
          }
          return $is_purchasable;
      }
      
      
  • To hide ‘Add to Cart’ for users who have not signed in , you can choose to hide the “Add to Cart” button for users who have not logged in (or registered) on your WooCommerce store. This is typically used when you want to run a special offer (or offer product discounts) for only your registered (and signed in) users.To do this, you can open the “functions.php” file (from the “theme” folder) and set the “is_purchaseable” variable to “True” only for users who have logged in.
    add_filter( 'woocommerce_is_purchasable', 'hide_add_to_cart_for_guests', 10, 2 );
    
    function hide_add_to_cart_for_guests( $is_purchasable, $product ) {
        // Check if the user is not logged in
        if ( ! is_user_logged_in() ) {
            // Set the product as unpurchasable for guests
            return false;
        }
    
        // Allow the "Add to Cart" button for logged-in users
        return $is_purchasable;
    }
    
  • To remove ‘Add to Cart’ for users based on their user role, you can choose to completely remove the “Add to Cart” button based on the user role. For example, you can remove the “Cart” button for all users with administrative rights. To do this, from the “functions.php” file (in the “theme” folder), you can use the “get_user_role” function to retrieve the user role and set the “is_purchaseable” variable to “False” for users with administrator role.
    
    add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_for_specific_roles', 10, 2 );
    
    function remove_add_to_cart_for_specific_roles( $is_purchasable, $product ) {
        // Get current user role
        if ( is_user_logged_in() ) {
            $user = wp_get_current_user();
            $user_roles = $user->roles;
    
            // Check if the user has the 'administrator' role
            if ( in_array( 'administrator', $user_roles ) ) {
                return false; // Disable 'Add to Cart' for administrators
            }
        }
    
        return $is_purchasable; // Allow 'Add to Cart' for other roles
    }
    
    
  • To disable ‘Add to Cart’ for selected product categories. , another option is to disable the “Add to Cart” button for select product categories (for example, laptops).To do this, you can open the “functions.php” file (from the “theme” folder) and check the product category using the “is_product_category” function. For the selected product category, you can use the “remove_action” function to configure disabling the “Cart” button.
    add_action( 'wp', 'disable_add_to_cart_for_selected_categories' );
    
    function disable_add_to_cart_for_selected_categories() {
        if ( is_product_category( 'laptops' ) ) { // Replace 'laptops' with your category slug
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    }
    
  • To disable ‘Add to Cart’ for a selected date range., you can also choose to disable the “Add to Cart” button temporarily for a new product for a selected date range. For example, you may be having a new product launch on a certain date. In this case, you can disable the “Cart” button until the date of the product launch and enable it on the launch date.To do this, you can open the “functions.php” file (from the “theme” folder) and use the “$current_date” and “$release_date” variables to obtain the current and release dates, respectively. You can then set the “is_purchaseable” variable to “False” for the following conditions:
    • The current date is less than the release date.
    • A product with specific product ID.

    This action will also replace the “Cart” button with the “Read more…” button till the product release date.

    
    add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_for_date_range', 10, 2 );
    
    function disable_add_to_cart_for_date_range( $is_purchasable, $product ) {
        // Set the release date (YYYY-MM-DD format)
        $release_date = '2024-12-01';
        $current_date = date( 'Y-m-d' );
    
        // Check if the current date is before the release date and if the product ID matches
        if ( $current_date < $release_date && $product->get_id() == 1234 ) { // Replace 1234 with your product ID
            return false; // Disable "Add to Cart"
        }
    
        return $is_purchasable; // Allow "Add to Cart" for other conditions
    }
    
    

Conclusion

As discussed, there are multiple cases where you need to hide, remove, or disable the “Add to Cart” button on your WooCommerce store. To create a positive shopping experience for your customers, you can further customize your “Add to Cart” button in many ways.

With its expertise in WooCommerce development, WePlugins can provide a wide variety of services to help you build a great website. Our dedicated and highly experienced WordPress team has been doing coding and plugin development since the year 2012. Here is an informative blog on how to implement WooCommerce shipping based on zip code.

We can help in developing an entire WordPress website or even performing major bug fixes. Register today on the WePlugins website or drop us a message with your name and email address.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.