WooCommerce: 10 Easy Snippets to Increase Your Sales

I had the pleasure to speak at WordCamp Prague 2019. I spoke about “10 PHP Snippets to Increase WooCommerce Sales” and managed to show some simple coding to the audience. Trust me – increasing your WooCommerce sales can also be done with a free, short, easy PHP snippet.

So, given that I want to share all the snippets I talked about, this is a quick recap. Copy them, test them (a must!) and then use them. And let me know if your conversion rate and/or AOV (average order value) increased!

At the bottom of the page you also find my talk slides. Enjoy:)

1. “Order by 6pm and get it delivered tomorrow!” notice @ Single Product Page


/**
 * @snippet       Pressure notice @ Single Product Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_single_product_summary', 'bbloomer_display_pressure_badge', 6 );
  
function bbloomer_display_pressure_badge() {
    echo '<div class="woocommerce-message">Order by 6pm and get it delivered tomorrow!</div>';
}

Adding some “pressure” to the WooCommerce single product page

2. “Secure payments” image @ Checkout Page


/**
 * @snippet       “Secure payments” image @ Checkout Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_review_order_after_submit', 'bbloomer_trust_place_order' );
 
function bbloomer_trust_place_order() {
    echo '<img src="https://www.paypalobjects.com/digitalassets/c/website/marketing/na/us/logo-center/9_bdg_secured_by_pp_2line.png" style="margin: 1em auto">';
}

Adding a secure badge on the WooCommerce Checkout page

3. Edit “Only 1 left in stock” @ Single Product Page


/**
 * @snippet       “Only 1 left in stock” @ Single Product Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_filter( 'woocommerce_get_availability_text', 'bbloomer_edit_left_stock', 9999, 2 );

function bbloomer_edit_left_stock( $text, $product ) {
	$stock = $product->get_stock_quantity();
  	if ( $product->is_in_stock() && $product->managing_stock() && $stock <= get_option( 'woocommerce_notify_low_stock_amount' ) ) $text .= '. Get it today to avoid 5+ days restocking delay!';
	return $text;
}

Changing the scarcity message on the WooCommerce single product page

4. Distraction-free Checkout


/**
 * @snippet       Distraction-free Checkout
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'wp', 'bbloomer_nodistraction_checkout' );

function bbloomer_nodistraction_checkout() {
	if ( ! is_checkout() ) return;
	remove_action( 'storefront_header', 'storefront_social_icons', 10 );
	remove_action( 'storefront_header', 'storefront_secondary_navigation', 30 );
	remove_action( 'storefront_header', 'storefront_product_search', 40 );
	remove_action( 'storefront_header', 'storefront_primary_navigation', 50 );
	remove_action( 'storefront_header', 'storefront_header_cart', 60 );
	remove_action( 'storefront_footer', 'storefront_footer_widgets', 10 );
}

5. “Try before you buy” @ Single Product Page


/**
 * @snippet       Buy a sample @ Single Product Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_single_product_summary', 'bbloomer_add_free_sample_add_cart', 35 );
 
function bbloomer_add_free_sample_add_cart() {
	echo '<p><a href="/?add-to-cart=953" class="button">Add Sample to Cart</a></p>';
}

Adding a “buy sample” button on the WooCommerce single product pge

6. Upsell @ Thank-you Page


/**
 * @snippet       Upsell @ Thank-you Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_thankyou', 'bbloomer_thankyou_upsell', 5 );
 
function bbloomer_thankyou_upsell() {
echo '<h2>Customers also bought...</h2>';
echo do_shortcode( '[products limit="3" columns="3" orderby="popularity" on_sale="true" ]' );
}

Showing products on sale on the WooCommerce thank you page

7. Bulk discount @ Checkout Page


/**
 * @snippet       Bulk discount @ Checkout Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_before_cart', 'bbloomer_apply_bulk_coupon' );
 
function bbloomer_apply_bulk_coupon() {
    $coupon_code = 'bulk';
	if ( WC()->cart->get_cart_contents_count() > 5 ) {
    	if ( ! WC()->cart->has_discount( $coupon_code ) ) WC()->cart->add_discount( $coupon_code );
	} else {
		if ( WC()->cart->has_discount( $coupon_code ) ) WC()->cart->remove_coupon( $coupon_code );
	}
}

8. Product Add-ons @ Single Product Page


/**
 * @snippet       Product Add-ons @ Single Product Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_gift_wrap', 35 );
  
function bbloomer_gift_wrap() {    
	?>
	<label><input type="checkbox" name="gift-wrap" value="Yes">$2 Gift Wrap?</label>
 	<?php
}
  
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_store_gift', 10, 2 );
  
function bbloomer_store_gift( $cart_item, $product_id ) {
	if( isset( $_POST['gift-wrap'] ) ) $cart_item['gift-wrap'] = $_POST['gift-wrap'];
	return $cart_item; 
}

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee' );
 
function bbloomer_add_checkout_fee() {
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( isset( $cart_item['gift-wrap'] ) ) {
            $itsagift = true;
            break;
        }
    }
    if ( $itsagift == true ) WC()->cart->add_fee( 'Gift Wrap', 2 );
}

9. BOGO


/**
 * @snippet       BOGO
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_bogo', 10, 3 );
 
function bbloomer_bogo( $passed, $product_id, $quantity ) {
	$sku_with_gift = 'sku0001';
	$sku_free_gift = 'sku0002'; 
	$product = wc_get_product( $product_id );
	$sku_this = $product->get_sku();
 	if ( $sku_this == $skuswithgift ) {
    	WC()->cart->add_to_cart( wc_get_product_id_by_sku( $sku_free_gift ) );
	}
	return $passed;
}

10. Free Shipping Threshold @ Cart Page


/**
 * @snippet       Free Shipping Threshold @ Cart Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=111758
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
 
function bbloomer_free_shipping_cart_notice() {
	$threshold = 80;
	$current = WC()->cart->get_subtotal(); 
	if ( $current < $threshold ) {
		wc_print_notice( 'Get free shipping if you order ' . wc_price( $threshold - $current ) . ' more!', 'notice' );
	}
}

Slides

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

22 thoughts on “WooCommerce: 10 Easy Snippets to Increase Your Sales

  1. HI!
    I am trying to implement number 6 but cannot make it work:

    add_action( 'woocommerce_thankyou', 'bbloomer_thankyou_upsell', 5 );
      
    function bbloomer_thankyou_upsell() {
    echo 'Descubre mis Chef Packs';
    echo do_shortcode( '[products category="carta" limit="3" columns="3" orderby="rand" on_sale="true" ]' );
    }
    

    Where carta is the slug of the category I want to publish.

    Do you have any snippet to provide VAT number during checkout?

    1. Hi Luis – sure that category has any products on sale?

  2. Hi

    I think your web is amazing. I am trying to use one of yours tips and It Works. However I would like that it wouldn´t appear in some specific Products. ¿It is possible?

    I have used the next action that it is asociate to the variation task. The reason is that the product I sell they have a posibility to add any quantity for the one variation. I sell by meters. And the variation 1 x 1,52 is use to put the exactly amound you are interested in.

    I am using the next tip

    add_action( ‘woocommerce_before_variations_form’, ‘bbloomer_display_pressure_badge’, 6 );

    Thanks in advance.

    1. Hello there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi Rodolfo,

    Real high-quality snippets that you provide !
    I’v implemented ““Only 1 left in stock” @ Single Product Page” and it works awesome, but it also displays when there is no stock and “Available on backorder.” is displayed. How to avoid this situation and to only display the this CTA when there is still some stock available ?

    Kindly,
    Casper

    1. Hi Casper! Take a look at the conditionals used here: https://businessbloomer.com/woocommerce-add-stock-quantity-on-shop-page/

  4. Hello Rodolfo! You’re very a awesome person.

    I have a question:
    For your community, can you show us how we can animate the add-to-cart button with shaking or other ? It’s very powerful for increase sales and it completes your article above.

    Thank you.

    1. Hello Stan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. hi need some help i want to show total view count of a product on single product page in woocommerce.

    1. Hi Tayyab, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. I want to show 1. notice @ Single Product Page to only logged in users. Is that possible? Can you help me…

    1. Hello Rifat, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hey Rodolfo,
    thank you very much for sharing your knowledge. I tried the snippet “Secure payments” image. My problem is now that the image appears right besides the “Buy Now” button and not below the button.
    Thank you in advance
    Stefan

    1. Hi Stefan, you’ll need some CSS to align it properly 🙂

  8. Rodolfo,

    thanks for an interesting and useful set of snippets for using in WooCommerce. Like a number of other website owners the site I run is multilingual. It would be great if you could provide snippets that are suitable for use in multilingual sites, so that we could incorporate the snippet, translate the relevant strings and be ready to go.

    I would be particularly interested in seeing snippet number 8 (Product Add-ons @ Single Product Page) in this article being made translation aware. You up for the challenge?

    1. Thank you Roger! All you have to do is changing e.g. this:

      echo '<div class="woocommerce-message">Order by 6pm and get it delivered tomorrow!</div>';

      into this:

      echo '<div class="woocommerce-message">' . __( 'Order by 6pm and get it delivered tomorrow!', 'bbloomer' ) . '</div>';

      Now, you can translate that string under the textdomain = “bbloomer”

      Hope this helps

    2. Rodolfo, thanks for the response and sorry for my delay in replying. I’m afraid I didn’t receive the email to notify my of follow ups.
      I was actually interested in snippet number 8. Would this be as easy to to modify?
      Thanks you for making your presentation available, it’s given me an awful lot to think about. I look forward to working with you site in the future. Best wishes
      Roger

      1. Thank you Roger! Yes, you just need to change:

        <label><input type="checkbox" name="gift-wrap" value="Yes">$2 Gift Wrap?</label>

        into:

        <label><input type="checkbox" name="gift-wrap" value="Yes"><?php __( '$2 Gift Wrap?', 'bbloomer' ); ?></label>

        And that string will become translatable 🙂

  9. A little mod for „Pressure notice” to alter messages on weekends 🙂

    /**
     * @snippet       Pressure notice @ Single Product Page with ultimate Weekend days detector ;)
     * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
     * @sourcecode    https://businessbloomer.com/?p=111758
     * @author        Rodolfo Melogli, Black Sun
     * @compatible    WooCommerce 3.5.4
     * @donate $9     https://businessbloomer.com/bloomer-armada/
     */
     
       
    function bbloomer_display_pressure_badge() {
        //echo '';
    	if(in_array(date('D'),['Sat','Sun'])) {
    		echo '<div class="woocommerce-message">Order today, and we will send it on next business day!</div>';
    	} else {
    		echo '<div class="woocommerce-message">Order by 6pm and get it delivered tomorrow!</div>';
    	}
    }
    add_action( 'woocommerce_single_product_summary', 'bbloomer_display_pressure_badge', 6 );
    
    1. Awesome!

  10. Hi,
    I tried the Free Shipping snippet and it did not work for me. The only edit I made was to change the 80 to 75 (our free shipping threshold). It also made only one of 33 products show up on the shop page.

    I add the snippet to a custom plug-in for the addition of snippets.

    I did try the sold out label snippet and it worked perfectly.

    1. You’re right, try now 🙂

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.