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>';
}
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">';
}
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;
}
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>';
}
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"]' );
}
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' );
}
}
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
I see. What are your tax settings please?
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!
Thanks Francesco! Snippet 7 works for all users, logged in or not
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 ) {
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!
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?
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!
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.
In this case it doesn’t increase with the quantity. But you could try https://www.businessbloomer.com/woocommerce-sync-product-quantities-cart/
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
Hola Jose! I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy ๐
Free shipping snippet was exactly what I’ve been searching for!! Very very useful thank you! ๐
Awesome
HI!
I am trying to implement number 6 but cannot make it work:
Where carta is the slug of the category I want to publish.
Do you have any snippet to provide VAT number during checkout?
Hi Luis – sure that category has any products on sale?
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.
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!
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
Hi Casper! Take a look at the conditionals used here: https://businessbloomer.com/woocommerce-add-stock-quantity-on-shop-page/
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.
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!
hi need some help i want to show total view count of a product on single product page in woocommerce.
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!
I want to show 1. notice @ Single Product Page to only logged in users. Is that possible? Can you help me…
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!
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
Hi Stefan, you’ll need some CSS to align it properly ๐
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?
Thank you Roger! All you have to do is changing e.g. this:
into this:
Now, you can translate that string under the textdomain = “bbloomer”
Hope this helps
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
Thank you Roger! Yes, you just need to change:
into:
And that string will become translatable ๐
A little mod for โPressure noticeโ to alter messages on weekends ๐
Awesome!
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.
You’re right, try now ๐