Didn’t find the code snippet you looking for? Simply Subscribe to the Newsletter and comment me on my Youtube Channel what kind of function code snippet you looking for and I will notify you if I could make it available.

More Code Snippets are coming soon..

1. Currency symbols AUD to AU$

Change the currency Symbols on your woocommerce store. Simply change AUD example to your current store currency. example USD to US$ etc.

add_filter( 'woocommerce_currency_symbol', 'change_currency_symbol', 10, 2 );

function change_currency_symbol( $symbols, $currency ) {
	if ( 'AUD' === $currency ) {
		return 'AU$';
	}

        return $symbols;
}

2. Show Lowest Price with ‘From’ suffix on Shop page only

Instead of variable price on variable products on your woocommerce, you can rather show from the lowest price with Suffix “From”. example, your variable product has range of variations where lowest price variation option is $5 and highest price variation option is $20, by default, your product would show the price like “$5-$20”. But instead, with the help of this code, you can make it like “From $5”

add_filter( 'woocommerce_get_price_html', 'change_price_format_on_shop_page', 100, 2 );
function change_price_format_on_shop_page( $price, $product ) {
if ( ! is_shop() ) {
return $price;
}
if ( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices();
$min_price = min( $prices['price'] );
$price = wc_price( $min_price ) . $product->get_price_suffix();
}
return 'From ' . $price;
}

3. Remove product name from breadcrumb of product page

Sometimes breadcrumbs become very lengthy due to long product title. You can remove the product title entirely form the breadcrumbs of the product page with this code.

add_filter( 'woocommerce_get_breadcrumb', 'remove_product_name_from_breadcrumb' );

function remove_product_name_from_breadcrumb( $breadcrumb ) {
    if ( is_singular( 'product' ) ) {
        array_pop( $breadcrumb );
    }
    return $breadcrumb;
}

4. Text above phone field on checkout page

Add text something like “Please verify your Mobile Number for added security. Thank you” or “Please enter a correct Mobile number for shipping label” or something like “Valid mobile number only, No Landline number please” or whatever text you want to replace it with.

function add_email_info_text_for_non_logged_in_users() {
    if (!is_user_logged_in()) {
        add_action('woocommerce_after_checkout_billing_form', 'add_email_info_text');
    }
}

function add_email_info_text() {
    echo '<p class="info-text" style="color: red;">Please verify your Mobile Number for added security. Thank you.</p>';
}

add_action('wp', 'add_email_info_text_for_non_logged_in_users');

5. Shorten product title on shop, category and archieve pages

Simply replace the number in ($title, 0, 40, ‘…’) with any number like ($title, 0, 25, ‘…’) or ($title, 0, 30, ‘…’) or whatever max letters you want to set with..

// Function to modify product title length
function custom_modify_product_title_length($title) {
    // Check if we are on the shop, archive, category, or home page
    if (is_shop() || is_archive() || is_category() || is_front_page()) {
        // Limit the title length to 40 characters
        $title = mb_strimwidth($title, 0, 40, '...');
    }

    return $title;
}

// Hook into the_title filter to apply the custom modification
add_filter('the_title', 'custom_modify_product_title_length', 10, 2);

6. Change Add to cart text to add to bag

You can change the “Add to Cart” button text with anything you like. example “Add to Bag” or “Add to Basket” etc. I have used “Add to Bag” example in this code.

// To change add to cart text on single product page

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
    return __( 'Add To Bag', 'woocommerce' );
}

// To change add to cart text on product archives(Collection) page

add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Add To Bag', 'woocommerce' );
}

7. Automatically select the first in-stock variation by default

add_action('wp_footer', 'auto_select_first_in_stock_variation');

function auto_select_first_in_stock_variation() {
    if (!is_product()) return; // Ensure it only runs on product pages

    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            var form = $('form.variations_form');
            
            if (form.length > 0) {
                var variations = form.data('product_variations'); // Get variation data from WooCommerce
                
                if (variations && variations.length > 0) {
                    for (var i = 0; i < variations.length; i++) {
                        var variation = variations[i];

                        if (variation.is_in_stock) { // Check if the variation is in stock
                            var attributes = variation.attributes;
                            
                            for (var key in attributes) {
                                var value = attributes[key];
                                
                                if (value) {
                                    var selectElement = $('select[name="' + key + '"]');
                                    selectElement.val(value).trigger('change'); // Auto-select the attribute
                                }
                            }

                            form.trigger('check_variations'); // Ensure variation is applied
                            break; // Stop after selecting the first in-stock variation
                        }
                    }
                }
            }
        });
    </script>
    <?php
}

Didn’t find the code snippet you looking for?

Simply Subscribe to the Newsletter and comment me on my Youtube Channel what kind of function code snippet you looking for and I will notify you if I could make it available.

Subscribe to eComHardy Newsletter for Free updates

Please enter your name and email to run a free in-depth SEO report of your website and unlock the power of SEO. You will also receive exclusive tips and tricks straight to your inbox.

More Code Snippets are coming soon..

Scroll to Top