Ultimate guide: Best WooCommerce Snippets to Tailor Your Online Shop

Are looking for the best WooCommerce Snippets to Tailor Your Online Shop? Over the previous some years, WooCommerce is a powerful WordPress e-commerce plugin. It has some good reasons that are why it’s always on the top. 

Designed by WooThemes, WooCommerce is flexible enough to give your customers a pleasant online shopping experience. Looking at its adaptive nature, online players are seeing this plugin as a means of expanding their business quickly and easily.

WooCommerce claims enough settings and great support. However, while personalizing your store, you may need to zoom in some functionality, which will call for the use of the code. Today, In this post, We have compiled a list of 10 WooCommerce code snippets that can be useful to meet many tasks like online retailers like you, possibly in a highly feasible way.

So, let’s take a look!

Read More: Ultimate Guide: How to Display Code Snippets in WordPress Post

1. Add a Message to the Login/Registration Form

Those who want to add any message at the top of the login area or registration form, simply use the snippet as shown below:

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’ );

2. Customizing the WooCommerce Breadcrumb

Breadcrumb may be a great solution for several sites, to reduce user navigation and simply search engine optimization. But how can this be done if you want to change the default text of an item?

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;

}

To change 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;

}

3. Remove Specific Product Tabs

To remove a specific product tab, like as ‘product description’, or ‘review’, use the code of code given below.

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;

}

4. Redirect to Checkout page after Add to Cart

When your products are such that customers buy the only single product at a time, you can redirect directly to ‘Add to Cart’ on 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 );

6. Add Payment type to the Admin Email

The code snippet shown below will help you remove products from a specific category from your store 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’ );

7. Automatically Change the Order Status

If you prefer to use the manual gateway for payment, and you want to mark these orders as ‘complete’, then you only need the following code snippets.

// 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’ );

8. Hide Flat Rate Shipping if Free Shipping is Available

Use the following code snippet to hide some shipping methods like ‘flat rate’ shipping.

//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;

}

Use the code shown below, to hide all other shipping methods when free shipping is available.

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;

}

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

Want to display currency symbol differently? Use the following 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;

}

10. Exclude a Category from the WooCommerce Category Widget

Sometimes, many categories can disorder the category widget. In this situation, you want to remove specific categories from the widget and can only show certain people the code below will help you do this.

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;

}

Wrapping Up

Here, in this, we have discussed 10 Resourceful WooCommerce Snippets for your online store.

Now, all of these are useful WooCommerce snippets for shaping your online store in very useful ways! So bookmark this post and start using them to easily customize your WooCommerce storeIf you facing any hurdle, let me know in the comments.

If you’re looking for someone who can help you with WooCommerce then you can dial our WordPress technical support number +1-855-945-3219. We will be glad to help you.

Some of Our Popular Blogs:

Complete Guide: How to easily Hide WooCommerce Coupon Fields?

Complete Guide: How to accept multiple currencies in WooCommerce?



Leave a Reply

Call Now