WOOCOMMERCE
Essential WooCommerce Code Snippets and Customizations

Why Use WooCommerce Code Snippets?
WooCommerce is highly customizable, allowing store owners and developers to extend its functionality beyond the default settings. While many features can be added through plugins, code snippets often provide a lightweight and efficient way to customize your online store without installing additional software.
Whether you want to modify the checkout experience, customize shipping behavior, improve product pages, or automate common tasks, WooCommerce provides an extensive collection of hooks and filters that make these customizations possible.
When implemented correctly, code snippets can improve your store’s functionality, enhance the customer experience, and reduce reliance on multiple plugins that may impact website performance. They also give developers greater flexibility to tailor WooCommerce to specific business requirements.
Before You Begin
Before adding WooCommerce code snippets to your website, make sure you understand where the code should be placed and how to test changes safely. Even a small error in a PHP snippet can affect your website if it’s not implemented correctly.
Before You Start
Make sure you have:
- Administrator access to your WordPress Dashboard.
- A recent backup of your website.
- A staging website for testing (recommended).
- Access to your active child theme or the Code Snippets plugin.
- The latest versions of WordPress and WooCommerce installed.
Where to Add Code Snippets
For most customizations, you should add PHP snippets using one of these methods:
- A child theme’s functions.php file.
- A reputable code snippets plugin.
- A custom functionality plugin.
Avoid modifying WooCommerce core files, as your changes will be overwritten during future updates.
Test Before Using on a Live Website
Always test new snippets on a staging website before deploying them to your live store.
After adding a snippet:
- Verify there are no PHP errors.
- Test the affected functionality.
- Confirm your checkout process still works correctly.
- Review both desktop and mobile versions.
Keep Your Website Updated
Many WooCommerce snippets rely on hooks and filters that may change over time.
Before adding any snippet:
- Update WordPress.
- Update WooCommerce.
- Update your theme.
- Review the snippet’s compatibility with your current WooCommerce version.
Tip: Whenever possible, use a child theme or a code snippets plugin instead of editing your theme’s core files. This helps preserve your customizations during future theme and WooCommerce updates.
Snippet 1: Hide Other Shipping Methods When Free Shipping Is Available
When free shipping is available, many online stores prefer to hide all other shipping options to simplify the checkout process and encourage customers to select the free shipping method.
Note: Recent versions of WooCommerce include a built-in option to hide other shipping rates when free shipping is available. Check your WooCommerce shipping settings before adding a custom code snippet.
When to Use This Snippet
This customization is useful when you want to:
- Display only the Free Shipping option.
- Simplify the checkout experience.
- Prevent customers from selecting unnecessary paid shipping methods.
- Reduce confusion during checkout.
Built-in WooCommerce Option
Before adding custom code, navigate to:
WooCommerce → Settings → Shipping → Shipping Settings
If your WooCommerce version supports it, enable:
Hide shipping rates when free shipping is available
Using the built-in setting is recommended because it remains compatible with future WooCommerce updates.
Custom PHP Snippet (Optional)
If you need custom behavior beyond the built-in option, you can use a PHP snippet.
add_filter( 'woocommerce_package_rates', 'tpw_hide_shipping_when_free_is_available', 100 );
function tpw_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
Where to Add the Snippet
Add the code to one of the following:
- Your child theme’s functions.php file.
- A trusted Code Snippets plugin.
- A custom functionality plugin.
Avoid editing WooCommerce core files.
Test the Results
After adding the snippet:
- Add products to your cart.
- Meet the free shipping requirements.
- Verify that only the Free Shipping option appears.
- Complete a test checkout to ensure shipping charges are calculated correctly.
Tip: Always use WooCommerce’s built-in shipping settings when they meet your needs. Reserve custom code snippets for situations where additional functionality or specialized behavior is required.
Snippet 2: Define a Minimum Order Amount
Setting a minimum order amount can help increase average order value, reduce small transactions, and ensure each order remains profitable. This customization allows you to require customers to reach a specified cart total before they can complete checkout.
When to Use This Snippet
This customization is useful when you want to:
- Require a minimum purchase amount before checkout.
- Encourage customers to add more products to their cart.
- Reduce low-value orders that may not be cost-effective.
- Improve overall store profitability.
How It Works
The snippet checks the customer’s cart subtotal before checkout. If the subtotal is below the minimum amount you define, WooCommerce displays a notice and prevents the customer from completing the checkout process until the minimum requirement is met.
Customize the Minimum Order Amount
Before using the snippet, choose the minimum order value that best fits your business.
Examples include:
- $25 minimum order
- $50 minimum order
- $100 minimum order
- Any custom amount based on your store requirements
Custom PHP Snippet
Where to add this snippet: Add it to your child theme’s functions.php file, a trusted Code Snippets plugin, or a custom functionality plugin. Do not edit WooCommerce core files.
add_action( 'woocommerce_checkout_process', 'tpw_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'tpw_minimum_order_amount' );
function tpw_minimum_order_amount() {
$minimum = 50;
if ( WC()->cart->subtotal < $minimum ) {
if ( is_cart() ) {
wc_print_notice(
sprintf(
'Your current order total is %s. You must have an order with a minimum of %s to place your order.',
wc_price( WC()->cart->subtotal ),
wc_price( $minimum )
),
'error'
);
} else {
wc_add_notice(
sprintf(
'Your current order total is %s. You must have an order with a minimum of %s to place your order.',
wc_price( WC()->cart->subtotal ),
wc_price( $minimum )
),
'error'
);
}
}
}
Snippet 3: Apply a Cart Discount Based on the Cart Total
Offering automatic discounts based on the cart total is an effective way to encourage customers to spend more while rewarding larger purchases. Instead of requiring a coupon code, this customization automatically applies a discount when predefined spending thresholds are met.
When to Use This Snippet
This customization is useful when you want to:
- Reward customers for larger purchases.
- Increase your average order value.
- Automatically apply discounts without coupon codes.
- Create promotional pricing based on cart totals.
How It Works
The snippet checks the customer’s cart subtotal before checkout. If the subtotal falls within the discount range you define, WooCommerce automatically applies a discount to the order total.
You can customize:
- Minimum cart amount
- Maximum cart amount (optional)
- Discount amount
- Discount percentage
- Discount label displayed to the customer
Example Discount Structure
You might configure discounts such as:
- Spend $100 and receive $10 off
- Spend $250 and receive 5% off
- Spend $500 and receive 10% off
- Apply multiple discount tiers based on the cart total
Automatic discounts provide a seamless shopping experience because customers don’t need to remember or enter a coupon code.
Custom PHP Snippet
Where to add this snippet: Add it to your child theme’s functions.php file, a trusted Code Snippets plugin, or a custom functionality plugin. Do not edit WooCommerce core files.
add_action( 'woocommerce_cart_calculate_fees', 'tpw_apply_cart_discount' );
function tpw_apply_cart_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$minimum_amount = 100;
$discount = 10;
if ( $cart->subtotal >= $minimum_amount ) {
$cart->add_fee( 'Automatic Discount', -$discount );
}
}
How to Customize the Snippet
Modify these variables to fit your promotion:
$minimum_amount = 100; $discount = 10;
Examples:
- $minimum_amount = 50;
- $minimum_amount = 250;
- $discount = 20;
- $discount = 50;
You can also replace the fixed discount with a percentage if your promotion requires it.
Test the Results
After adding the snippet:
- Add products below the minimum amount.
- Verify no discount is applied.
- Increase the cart subtotal above the minimum amount.
- Confirm the Automatic Discount appears in the cart totals.
- Complete a test checkout to ensure the discount is reflected in the final order total.
Tip: If you plan to run frequent promotions or create multiple discount rules, consider using a dedicated WooCommerce dynamic pricing extension instead of custom code. For simple automatic discounts, this snippet provides a lightweight and effective solution.
Snippet 4: Modify the AJAX Variation Threshold
WooCommerce automatically switches to AJAX loading when a variable product contains more than a certain number of variations. This helps improve performance on large product catalogs by loading variation data only when customers make a selection.
In some cases, however, you may want to increase or decrease this threshold to better match your store’s needs.
When to Use This Snippet
This customization is useful when you want to:
- Display more product variations without AJAX loading.
- Improve the customer experience for variable products.
- Fine-tune WooCommerce performance.
- Optimize large product catalogs.
How It Works
By default, WooCommerce uses an AJAX variation threshold to determine when variation data should be loaded dynamically.
This snippet changes that limit by using WooCommerce’s built-in woocommerce_ajax_variation_threshold filter.
Note: Increasing the threshold causes WooCommerce to load more variation data on the initial page load, which may affect performance on products with a large number of variations.
Custom PHP Snippet
Where to add this snippet: Add it to your child theme’s functions.php file, a trusted Code Snippets plugin, or a custom functionality plugin. Do not edit WooCommerce core files.
add_filter( 'woocommerce_ajax_variation_threshold', 'tpw_ajax_variation_threshold', 10, 2 );
function tpw_ajax_variation_threshold( $threshold, $product ) {
return 100;
}
How to Customize the Snippet
Change the value returned by the function to match your preferred threshold.
Examples:
- return 30; → WooCommerce default behavior
- return 50; → Load up to 50 variations normally
- return 100; → Load up to 100 variations normally
- return 200; → Load up to 200 variations before switching to AJAX
Choose a value that balances usability and website performance.
Test the Results
After adding the snippet:
- Open a variable product with many variations.
- Verify that product options load correctly.
- Test product selection on desktop and mobile devices.
- Monitor page load speed and overall performance.
Tip: Only increase the AJAX variation threshold when necessary. Stores with products containing hundreds of variations may experience slower page loads if too much variation data is loaded at once.
Best Practices for Using WooCommerce Code Snippets
WooCommerce code snippets provide a simple way to customize your online store, but they should be implemented carefully. Following these best practices helps ensure your customizations remain secure, compatible, and easy to maintain as WordPress and WooCommerce continue to evolve.
Always Create a Backup
Before adding or modifying any code, create a complete backup of your website, including both your files and database. This allows you to quickly restore your store if a snippet causes unexpected issues.
Use a Child Theme or Code Snippets Plugin
Avoid editing your theme’s functions.php file directly unless you’re using a child theme.
Instead, consider using:
- A child theme
- The Code Snippets plugin
- A custom functionality plugin
These methods make your customizations easier to manage and preserve during theme updates.
Test on a Staging Website
Whenever possible, test new snippets on a staging website before deploying them to your live store.
Verify that:
- The snippet performs as expected.
- No PHP errors occur.
- Checkout continues to function correctly.
- Mobile and desktop experiences remain unaffected.
Keep WooCommerce Updated
WooCommerce regularly introduces new features and updates existing hooks and filters.
After updating:
- Review your custom snippets.
- Test key store functionality.
- Remove snippets that are no longer needed because WooCommerce now provides a built-in solution.
Use Only Trusted Snippets
Not every code snippet found online is compatible with the latest versions of WordPress and WooCommerce.
Before implementing any snippet:
- Verify that it supports your WooCommerce version.
- Understand what the code does.
- Test it thoroughly before using it on a production website.
Best Practice: Keep a record of every custom WooCommerce snippet you add to your website. Documenting your customizations makes future maintenance, troubleshooting, and upgrades much easier, especially as your online store grows.


