Best WooCommerce Code Snippets for WordPress

Best WooCommerce Code Snippets for WordPress

Are you looking for the best WooCommerce code snippets for WordPress? If you are, keep reading this article! Here, we will show you the best snippets to help you improve your online store!

While you handle a WooCommerce store, customizing it would be ideal. This will help you stand out from the crowd.

When it comes to WooCommerce store customization, there are multiple ways. Some people use custom plugins, while others rely on PHP/CSS snippets.

Do you want the best WooCommerce code snippets for WordPress?

Keep reading this article if you need the best options to enhance the website’s features. This will be the only article you need to check regarding the topic.

But first, let’s see why customizing your WooCommerce store is mandatory.

Why You Should Customize Your WooCommerce Store

WooCommerce code snippets

You should customize your WooCommerce store to stand out from the crowd. There are a lot of WooCommerce stores available. The main point is they all have the same/similar features.

The only difference would be the website’s design since people use different WooCommerce themes. Hence, tweaking your WooCommerce store will make you unique.

You do not need to be an expert in PHP or any other development language to tweak a WooCommerce store.

These days, there are multiple plugins available for the same task. Since some plugins can slow your website down, we will choose the traditional way – dealing with custom snippets.

Now you know why customizing the WooCommerce store is a good idea. Next, let’s see how you can customize your online store.

Best WooCommerce Code Snippets for WordPress

Here, we will show you the top code snippets you can add to your WooCommerce store. They are:

  • Add WooCommerce support to a third-party theme
  • Add a secure payment image to the checkout page
  • Exclude a category from the WooCommerce widget
  • Add a custom currency to the store
  • Remove product meta from single product pages
  • Remove the product tab from the product pages
  • Add a new country to the store
  • Remove WooCommerce breadcrumbs
  • Add a custom shop title
  • Remove the company name from the WooCommerce checkout page
  • Redirect users to checkout after they add the product to the cart
  • Remove custom product categories from the shop page
  • Remove zero decimals from the product page
  • Translate any WooCommerce strings
  • Rename a product tab
  • Set a minimum order amount

Each option comes with a dedicated purpose. Let’s have a closer look at each option.

1. Enable WooCommerce Support on a Third-Party Theme

When you have created a custom theme and need to include WooCommerce support, use this code:

function yaycommerce_add_woocommerce_support() {
	add_theme_support( 'woocommerce' );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
} 
add_action( 'after_setup_theme', 'yaycommerce_add_woocommerce_support' );

Add this code to the theme’s functions.php file, and you are ready.

2. Add a Secure Payments Image to the Checkout Page

If you need to add a secure payments image to your checkout page, use this code in your functiosn.php file:

add_action( 'woocommerce_review_order_after_submit', 'yaycommerce_trust_place_order' );
 
function yaycommerce_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">';
}

In this case, we request an image file from a third-party source. So, if you are into speed optimization, adding this domain name to the prefetching list would be ideal.

3. Exclude a Specific Category from WooComerce Category Widget

When you need to exclude a category from the WooCommerce category widget, use this snippet:

add_filter( 'woocommerce_product_categories_widget_args', 'yay_product_cat_widget_args' );

function yay_product_cat_widget_args( $cat_args ) {
	
	$cat_args['exclude'] = array('18');
	
	return $cat_args;
}

You need to replace the category ID with your custom one. For example, we have 18 as the category ID in this case. In your case, it might be different. So, find the correct category ID and update the code accordingly.

4. Add a Custom Currency to the Store

If you need to add a custom currency to the online store without using a dedicated plugin, use this snippet:

add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ ); 
function add_my_currency( $currencies ) { 
 $currencies[‘ABC’] = __( ‘Currency name’, ‘woocommerce’ ); 
 return $currencies;
}
add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2); 
function add_my_currency_symbol( $currency_symbol, $currency ) 
{ 
 switch( $currency ) { 
 case ‘ABC’: 
 $currency_symbol = ‘$’; 
 break; 
 } 
 return $currency_symbol;
}

Once you have modified the currency name and symbol, paste the snippet in the functions.php file and save it.

5. Remove Product Meta from Single Product Pages

Do you need to remove product meta from single product pages completely? If you are looking for the right snippet, here it is:

remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_meta’, 40 );

6. Remove the Product Tab from Single Product Pages

By using the snippet we added below, you can remove the product tabs from single product pages:

add_filter( ‘woocommerce_product_tabs’, ‘yayremove_product_tabs’, 98 );
function yayremove_product_tabs( $tabs ) {
 // Remove the additional information tab
 unset( $tabs[‘additional_information’] ); 
 return $tabs;
}

7. Add a New Country to the Website

Sometimes, you need to add a country to the WooCommerce store. If you are in a similar situation, use this code in your functions.php file.

function yay_add_my_country( $country ) {
 $country[“AEDUB”] = ‘Dubai’;
 return $country;
}
add_filter( ‘woocommerce_countries’, ‘yay_add_my_country’, 10, 1 );

Before updating the file, update the country name. Once you are done, go ahead and edit the functions.php file.

8. Remove WooCommerce Breadcrumbs

Not a fan of WooCommerce breadcrumbs? If you run a content blog, breadcrumbs will be useful in multiple situations. But for WooCommerce stores, breadcrumbs won’t be necessary. Here is a code to remove WooCommerce breadcrumbs from your store.

add_action( ‘init’, ‘yayremove_wc_breadcrumbs’ );
function yayremove_wc_breadcrumbs() {
   remove_action( ‘woocommerce_before_main_content’, ‘woocommerce_breadcrumb’, 20, 0 );
}

Update the functions.php file after pasting this code.

9. Add a Custom Shop Title

By default, WooCommerce will display shows as the page title of your products page. But with a little bit of code, you can modify it. Here is the code you can use to customize the shop page title:

add_filter( ‘woocommerce_page_title’, ‘yayshop_page_title’);
function yayshop_page_title($title ) {
 if(is_shop()) {
 return “My New Shop”;
 }
 return $title;
}

Replace My New Shop with your preferred title, and you are good to go.

10. Remove the Company Name from WooCommerce Checkout

If you need to remove the company name field from the WooCommerce checkout page, use this snippet:

add_filter( ‘woocommerce_checkout_fields’ , ‘yayremove_company_name’ ); function yayremove_company_name( $fields ) { 
   unset($fields[‘billing’][‘billing_company’]); 
   return $fields;
}

When you target individual customers, a company name field is not mandatory. Instead of marking it as optional, you can remove the field from the checkout page using the code.

11. Redirect Users to Checkout After Adding the Product to the Cart

We can enable the automatic redirection feature instead of customers manually going to the checkout. In this case, once they have added a product to the cart, WooCommerce will automatically redirect them to the checkout page. This way, the store owner can boost conversion rates. Here is the snippet you can use to enable the feature:

add_action( ‘add_to_cart_redirect’, ‘yayadd_to_cart_checkout_redirect’, 16 );
function yayadd_to_cart_checkout_redirect() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
}

12. Remove Product Categories from the Shop Page

Hiding custom product categories from the shop is possible. By default, there is no way to do this. But using this code, you can achieve the task:

add_action( ‘pre_get_posts’, ‘yayremove_categories_shop’ );
function yayremove_categories_shop( $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 in these categories on the shop page
 ‘terms’ => array( ‘oneplus’, ‘apple’, ‘samsung’, ‘mi’ ),
‘operator’ => ‘NOT IN’
)));
 }
remove_action( ‘pre_get_posts’, ‘yayremove_categories_shop’ );
}

You need to replace the category slugs in the code. After modifying the code, update the functions.php file and save every update.

13. Remove Zero Decimals in Product Pricing

Removing zero decimals from your product’s price is a good option you can try. Here is the code you need to use for the task:

add_filter( ‘woocommerce_price_trim_zeros’, ‘__return_true’ );

Update the functions.php file after pasting the code.

14. Translate any WooCommerce Strings

If you use the snippet below in your functions.php, you can enable a feature to translate every string quickly.

add_filter('gettext',  'translate_text');
add_filter('ngettext',  'translate_text');
 
function translate_text($translated) {
     $translated = str_ireplace('Choose and option',  'Select',  $translated);
     return $translated;
}

15. Rename a Product Tab

Instead of removing the product tab, let’s learn how to rename it. Here is the code snippet you need to use to rename the product tab:

add_filter( 'woocommerce_product_tabs', 'yaywoo_rename_tab', 98);
function yaywoo_rename_tab($tabs) {

 $tabs['description']['title'] = 'More info';

 return $tabs;
}

Modify the code and add the preferred title in the snippet before updating the functions.php file.

16. Set Minimum Order Amount

If you are selling products and need to set a minimum order amount for the purchase, follow this option. You can set a minimum purchase order amount using the snippet we mentioned below.

add_action( 'woocommerce_checkout_process', 'yaywc_minimum_order_amount' );
function yaywc_minimum_order_amount() {
	global $woocommerce;
	$minimum = 5;
	if ( $woocommerce->cart->get_cart_total(); < $minimum ) {
           $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
	}
}

In this case, we have selected five as the minimum order count. Based on your requirements, you can modify the amount.

That’s it!

These are the best code snippets you can use in a WooCommerce store. Remember that you use all these codes in the functions.php of your theme.

To keep the modifications live, we recommend creating a child theme and making all the modifications there. This way, you will not lose any customizations after a major theme update.

You can use the Child Theme Wizard plugin to create your child theme.

Conclusion

Starting a WooCommerce store is so simple. But generating sales and ensuring the website stands out is more challenging than you think. While handling an online store, you know the competition is not tiny.

Several individuals and businesses are selling the same products, and it takes work to stand against them. You can use these code snippets in this article to make your store unique.

As you can see, you do not need to be super technical to modify the WooCommerce store. We have already shared the snippet; you only need to modify it according to your preferences and update it on the functions.php file.

The changes will be visible in the front end almost instantly. We recommend cleaning your browser and WordPress cache if you can’t spot any differences.

What other ways do you recommend to improve an online store?

Let us know in the comments!

Sreehari P Raju
Sreehari P Raju

Sreehari P Raju is a freelance WordPress content writer. He started using WordPress in 2015 and loves writing tutorials, product reviews, and listicles. While not working, he loves playing Minecraft or eating KFC.

Related Posts
Leave a Reply

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