Search

How to Display ‘Add to Cart’ Button only on Products on Sale in WooCommerce

    WisdmLabs
Listen to this article

This is a guest post by Waqas Nayyer. If you’d like to contribute to our blog, feel free to get in touch with us.

woocommerce-customization-logoWooCommerce is a robust e-commerce platform, with an extensive set of features for a variety of online business applications.

One of the core built-in features of WooCommerce is the ability to set a sale price in addition to the regular price for each individual product on your store. This allows storekeepers to highlight discounts and encourage consumers to make a purchase. However, not every store owner finds this feature useful because not all stores offer regular promotions.

Some clients would prefer to use the sale price field in order to highlight specific items above the rest.

In this tutorial, you will learn how a WooCommerce shop can be set up so that the “Add to Cart” button is only displayed when a sale price is specified. This should work for both registered and non-registered (guest) users.

Modified “Add to Cart” functionality for products on sale

To achieve this setup, all you would have to do is to go to Appearance -> Themes and navigate to your active theme’s functions (functions.php).

Then, you can add the below piece of code at the end of the file and hit ‘Save’

(Do note: Ideally, it would make sense to create a child theme so that the modifications do not get affected on the theme update).

function prefix_hack_for_on_sale_only( $purchasable, $product ) {
    if ( ! is_admin() && ! is_user_logged_in() && ! $product-> is_on_sale()) {
       return false;
    }
    return $purchasable;
}
    add_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1, 2);

/* Output account link for non-sale products when user is not logged in
* @return bool|string
*/
    function prefix_login_for_prices() {
        if ( ! class_exists('WooCommerce') )
            return false;
        global $product;
        $login = false;
        if ( ! is_user_logged_in() && ! $product->is_purchasable() ) 
        {
            remove_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1 );

           $login_url = wc_get_account_endpoint_url( 'dashboard' );
           if ( is_singular( 'product' ) ) {
              $login_url = add_query_arg( 'redirect_to', urlencode(get_permalink() ), $login_url );
           }
           if ( $product->is_purchasable() ) {
              $login = sprintf('<a href="%1$s">%2$s</a>';, esc_url( $login_url ), esc_html__( 'Sign in to view price', 'text-domain' ));
           }
 add_filter('woocommerce_is_purchasable','prefix_hack_for_on_sale_only', 1, 2 );
    }
    return $login;
}

The above code achieves two things:-

#1 It helps display “Add to Cart” button on products on sale

add-cart-woocommerce-sale
“Add to cart” button on products on sale

#2 It hides the “Add to Cart” button on regular products

no-add-cart-woocommerce
No option for a user to add regular products to cart

Behind the scenes

When you add the above code to your theme, you enable two things.

First of all, the code creates two categories of products:

  1. products with a sale price become “purchasable” with a visible “Add to Cart” button
  2. and products without a sale price become “view-only” where only the price of the product is displayed.

Now this tactic can be useful in differentiating products that are available for purchase online and those that can only be purchased in-store (applicable for brick-and-mortar companies) or through your business’ other distribution channels.

Some products may just be not feasible for online selling, such as large and bulky items; however, by listing them on your WooCommerce shop even if they are not “purchasable” online, your website can still act as a useful catalog for your potential buyers. A helpful tip here, would be to add an inquiry button to capture interested customers who aren’t able to purchase the product.

Now, this code can also encourage users who are currently not registered on your WooCommerce shop to create an account before viewing products. That’s because the “Add to Cart” button is displayed only to logged in users. This can not only increase the subscriber count on the website but would also help in obtaining personal information from the customers which can be further utilized in marketing campaigns, such as e-mail newsletters or telemarketing. In the age of big data, consumer information is very valuable and the ability to collect more data from the company’s prospects would dramatically affect the firm’s ability to maximize profits through its marketing efforts.

A small change can make a big difference

A simple change like this can still have big applications and that can make a great difference on your WooCommerce store. Let us know what you thought of this tip in the comment section below, and watch out this space for more such tips soon!

About the Author

Waqas Nayyer is a blogger that often writes about WooCommerce tips and tricks, WordPress theme modification tips like the one you just read! For more from him, check out themelocation.com.

Picture of WisdmLabs

WisdmLabs

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads