Search
Close this search box.
Search

10 Resourceful WooCommerce Snippets to Tailor Your Online Shop

Listen to this article

[su_note note_color=”#fffdb0″ text_color=”#000000″]This is a Guest Post by Edward Jones

As a certified top­notch developer at OSSMedia Ltd, Edward Jones has an impressive expertise in WordPress & provides concrete information on WordPress related tips & tricks. Having gathered a total of 5 years of experience in WordPress Development, Edward has delivered numerous projects within the allotted time­frame.[/su_note]

code-snippets-woocommerceIn the last few years, WooCommerce has emerged as a powerful WordPress e-commerce plugin that keeps on topping itself for all the good reasons. Created by WooThemes, WooCommerce is flexible enough to deliver your customers a pleasurable online shopping experience. Given its customizable nature, online players are considering this plugin as a means of expanding their business quickly and easily.

WooCommerce boasts of adequate settings and great support. However, while personalizing your store, you may need to tweak some functionality, which would call for the use of code.

Today, I have compiled a list of some WooCommerce code snippets that online retailers, like you, may find useful to perform a range of tasks in the quickest possible manner.

So, let’s look at all of them.

[su_note note_color=”#f4f4f4″ text_color=”#000000″]Where should the code be added?

To use any of the code snippets below, you can copy and paste it in functions.php of your current theme, or in a custom plugin you build.[/su_note]

[space]

#1 Add a Message Above the Login/Registration Form

For those who want to add any message on the top of the login area or registration form, simply use the below-mentioned snippet.

function wdm_login_message() {
    if ( get_option( 'woocommerce_enable_myaccount_registration' ) == 'yes' ) {
    ?>
        <div class="woocommerce-info">
        <p><?php _e( 'Your custom message goes here.' ); ?></p>
        </div>
    <?php
    }

}
add_action( 'woocommerce_before_customer_login_form', 'wdm_login_message' );

[space]

#2 Customizing the WooCommerce Breadcrumb

Breadcrumbs can be a great solution for many websites, to ease user navigation and aid search engine optimization. But if you’re looking to change the default text of an item displayed, this is how it can be done.

add_filter( 'woocommerce_breadcrumb_defaults', 'wdm_change_breadcrumb_home_text' );
function wdm_change_breadcrumb_home_text( $defaults ) {
    // change the breadcrumb home text from 'Home' to 'Store'
    $defaults['home'] = 'Store';
    return $defaults;
}

For changing the breadcrumb separator use the following code. 

add_filter( 'woocommerce_breadcrumb_defaults', 'wdm_change_breadcrumb_separator' );
function wdm_change_breadcrumb_separator( $defaults ) {
    // change the breadcrumb delimiter from '/' to '>'
    $defaults['delimiter'] = ' &gt; ';
    return $defaults;
}

[space]

#3 Remove Specific Product Tabs

To remove a specific product tab, such as the ‘Product Description’, or ‘Reviews’, use the below piece of code.

add_filter( 'woocommerce_product_tabs', 'wdm_remove_product_tabs', 99 );
function wdm_remove_product_tabs( $tabs ) {
    // remove the description tab
    unset( $tabs['description'] );
    // remove the reviews tab
    unset( $tabs['reviews'] );
    // remove the additional information tab
    unset( $tabs['additional_information'] );
    return $tabs;
}

[space]

#4 Redirect to Checkout page after Add to Cart

When your products are such that customers purchase only a single product at a time, you can redirect an ‘Add to Cart’ directly to the checkout page.

function wdm_add_to_cart_checkout_redirect() {
    wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
    die();
}
add_action( 'woocommerce_add_to_cart',  'wdm_add_to_cart_checkout_redirect', 11 );

[space]

#5 Add Payment type to the Admin Email

If you want the payment method used by the customer to make the purchase sent in the order email, the below code is what you need.

function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
    // check if the email is an order email for the admin
    if ( $is_admin_email ) {
        echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>';
    }
}
add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );

[space]

#6 Remove Specific Product Categories from Shop Page

The below code snippet will help you remove products belonging to a specific category from your shop page.

function wdm_remove_product_categories( $q ) {
    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {
        $q->set( 'tax_query', array(array(
             'taxonomy' => 'product_cat',
             'field' => 'slug',
             // don't display products from these categories on the shop page
            'terms' => array( 'red', 'old' ),
            'operator' => 'NOT IN'
        )));
    }
    // once it's done, unhook
    remove_action( 'pre_get_posts', 'wdm_remove_product_categories' );
}
add_action( 'pre_get_posts', 'wdm_remove_product_categories' );

[space]

#7 Automatically Change the Order Status

If you prefer to use manual gateways for payment, and want to mark these orders as ‘Complete’, all you need is the following code snippet.

// automatically update the order status to 'Complete'
function wdm_auto_complete_on_hold_order( $order_id ) {
    $order = new WC_Order( $order_id );
    if ( 'on-hold' === $order->status )
        $order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'wdm_auto_complete_on_hold_order' );

[space]

#8 Hide Flat Rate Shipping if Free Shipping is Available

To hide certain shipping methods like ‘Flat Rate’ shipping use the following code snippet.

//hide the flat rate shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
function hide_standard_shipping_when_free_is_available( $available_methods ) {
    if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {
        // remove standard shipping option
        unset( $available_methods['flat_rate'] );
    }
    return $available_methods;
}

To hide all other shipping methods if free shipping is available, use the below code.

add_filter( 'woocommerce_package_rates', 'wdm_hide_shipping_when_free_is_available', 10, 2 );
function wdm_hide_shipping_when_free_is_available( $rates, $package ) {
    // only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) ) {
        // unset all methods except for free_shipping
        $free_shipping = $rates['free_shipping'];
        $rates = array();
        $rates['free_shipping'] = $free_shipping;
    }
    return $rates;
}

[space]

#9 Change the Currency from ‘$’ to ‘USD’

Want to display the currency symbol differently? Use the below code.

// change the currency symbol
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
    switch( $currency ) {
        case 'USD': $currency_symbol = 'USD'; break;
    }
    return $currency_symbol;
}

[space]

#10 Exclude a Category from the WooCommerce Category Widget

Sometimes several categories might clutter the Category Widget. In this case, you might want to remove specific categories from the widget and display only certain important ones. The below code will help you do just that.

add_filter( 'woocommerce_product_categories_widget_args', 'wdm_edit_product_cat_widget_args' );
function wdm_edit_product_cat_widget_args( $cat_args ) {
    // remove specific categories using their id
    $cat_args['exclude'] = array('16');
    return $cat_args;
}

[space]

Wrapping Up

And that’s all for now- useful WooCommerce snippets to shape your online store in the most effective manner! So bookmark this post and start using them to easily customize your e-commerce store. 🙂

admin

admin

One Response

  1. Hello,
    Awesome snippets you have here, and they working like charms.

    I just have one snippet request:
    I would like to have the Woocommerce My Account page menu (together with custom menu tabs added to it) to be shrinking into a mobile menu when viewed on Mobile.

    Would you have a snippet that can do that?
    I have searched around for months now but, nothing..

    Thanks..

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads