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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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

This will add a nice gift-wrap option for an extra $2.

/**
 * @snippet       Product Add-ons @ Single Product Page
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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

Buy One, Get One free.

/**
 * @snippet       BOGO
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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

Show the $ needed to reach the free shipping threshold.

/**
 * @snippet       Free Shipping Threshold @ Cart Page
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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

Video recording

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

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

  1. hi. the message to say get free shipping over x amount is not working correctly if the sub total has UK VAT added to it

    gives an incorrect amount needed to get free shipping

    so if free shipping is ยฃ30 and you have ยฃ20 in basket it says you need to spend ยฃ8.33 and not ยฃ10

    1. I see. What are your tax settings please?

  2. Hello! Really well done!
    I had the pleasure of “meeting” your site while I was looking on the web, and I discovered a WORLD!
    I need help, I tested the “snipet 7 – Bulk discount @ Checkout Page”, it works perfectly if I am “logged in, while in the cart view of an” unregistered “visitor the discount does not apply.
    How can I make sure that the visitor who puts a product in the cart can also receive the coupon automatically?

    Thank you so much for your work!

    1. Thanks Francesco! Snippet 7 works for all users, logged in or not

      1. Hi, is it possible to give a discount on the last product that was added to the cart instead of a general discount?

        I added one product and when I added the second one I want to get a “15%” discount on the second product only.

        if ( WC()->cart->get_cart_contents_count() > 2 ) {

        1. Hello Ran, 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. I loved the snippets, they were incredible and very helpful!
    I just wanted to get a free shipping message for products at a certain cost, if I have a category, how would I do it?

    1. Hi Christiano, 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!

  4. Your snippets work just perfectly on my site. With the gift wrap, it’s price doesn’t increase with the quantity. Is there a problem with the way I implemented the code or it doesn’t function that way.

    1. In this case it doesn’t increase with the quantity. But you could try https://www.businessbloomer.com/woocommerce-sync-product-quantities-cart/

  5. Good evening friend, thanks for all your help! I have a question: how can I use your codes, specifically number one so that it is personalized for each product? what I want is to have it for all products but change it in some specific products if necessary

    1. Hola Jose! I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy ๐Ÿ™‚

  6. Free shipping snippet was exactly what I’ve been searching for!! Very very useful thank you! ๐Ÿ˜€

    1. Awesome

  7. 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?

  8. 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!

  9. 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/

  10. 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!

  11. 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!

  12. 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!

  13. 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 ๐Ÿ™‚

  14. 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 ๐Ÿ™‚

  15. A little mod for โ€žPressure noticeโ€ to alter messages on weekends ๐Ÿ™‚

    /**
     * @snippet       Pressure notice @ Single Product Page with ultimate Weekend days detector ;)
     * @how-to        businessbloomer.com/woocommerce-customization
     * @sourcecode    https://businessbloomer.com/?p=111758
     * @author        Rodolfo Melogli, Business Bloomer, Black Sun
     * @compatible    WooCommerce 3.5.4
     * @community     https://businessbloomer.com/club/
     */
     
       
    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!

  16. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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