30+ Useful Custom WooCommerce Functions for Your WordPress Site

You can completely customize the look or function of your sales page by editing the Woocommerce plugin directly, but writing custom code is always better. In this guide, I list the most commonly used ready-to-use custom WooCommerce functions that you can use to greatly increase the functionality of your online store to make your website stand out and bring give you a competitive advantage compared to other websites in the same industry and with the same function.

WooCommerce is an open-source plugin on WordPress that supports the design of e-commerce websites or sales websites. Its task is to turn your website into a fast and efficient e-commerce site.
Currently, about 13 million people are using WooCommerce for their website and about 37% of online shops operate on this plugin. So what makes WooCommerce so popular?

Useful WooCommerce functions

Here are some of the main reasons.

  • WooCommerce is a free plugin.
  • The installation and use method are relatively simple and easy. You don’t have to be a professional technician to do it.
  • Provides a diverse library of WordPress templates, making the process of building an online shop homepage quick with just a few steps.
  • Regularly updated with new features and supported by WordPress developers themselves.
  • High security, helping to protect data, safe transaction information, prevent any illegal intrusion of third parties.
  • WooCommerce allows users to configure with a very large number of built-in features or extensions.
  • You can customize your shop, product, and cart pages easily through WooCommerce functions

To use these functions, you should create a child theme. After you have successfully set up your child theme, paste any of the code below into your child theme’s functions.php file. You can place these code snippets at the bottom of the functions.php file, just before the ?> tag. If you don’t see the ?> tag then just put it at the last line of your functions.php

Recommended:
WordPress .htaccess File: How to Use and Edit It
How to get post data in WordPress?
woocommerce_form_field() user guide and 10 sample examples
5 examples of getting Post by ID & 11 Ways to Get Post ID in WordPress

Table of Contents

Where is the functions.php file in Woocommerce?

Woocommerce does not have a functions.php file, it is a WordPress plugin. There may be some Woocommerce themes that may have a functions.php file but it’s not mandatory and when you insert the code into the functions.php file in the Woocommerce theme it won’t run either.

So, when I say “add this code to the functions.php file”, you must understand that is the active theme’s functions.php file. This functions.php file is usually located at wp-content/themes/your activated theme/functions.php

Here are the most commonly used code snippets that will help you customize Woocommerce functions and forms. These code snippets will help you to remove the default WooCommerce functionality or add new functionality to your WordPress Shopping page.

Renaming WooCommerce Product Tabs

If you want to change the title of the product tabs on the single product page, you can use the code below. The code below changes all 3 default tabs of Woocommerce from Description to Product Information, Additional Information to Extra Information, and Reviews to Product Reviews. If you don’t want to rename any tab, just remove that line in the code.

Removing Tabs

Use the code below to remove any tab you want. The example below removes all 3 tabs on the single product page.
Note that when you want to keep a tab, delete the corresponding line to keep that tab.

Re-ordering Tabs

Use the following code to change the tab order. By default of WooCommerce, the order of tabs is Description, Reviews, Additional Information, respectively. The example below will move the Reviews tab first, then the Additional Information tab, and finally the Description tab.

Add a custom tab

Use the following code to add a custom global product tab. This snippet will insert an extra tab on all products. This means that all products have an extra tab like that and the content is the same for all products.

In case you want product A to display different content, product B to display different content, you can use the code below. In the example below, I take product A with id 11, product B has id 12. You can apply the same to other products or get by category.

Move WooCommerce product additional information to Sidebar

The WooCommerce product additional information tab is where the Product attributes are displayed.

Actually, there is no DIRECT way to move the additional information tab to the sidebar, but we can do it INDIRECTLY by removing the additional information tab on a single product display page, then creating a shortcode to display information there on the sidebar.

The first is the code to remove the additional information tab on the single product display page:

Next, use this code to create a shortcode that displays all his product attributes.

SHORTCODE USAGE

Insert this shortcode anywhere on your Woocommerce page with a defined product id:

Or in php:

In a single product page (where the “additional information” product tab is removed):

Or in PHP:

You will get something like this:

WooCommerce product additional information shortcode

Replace WooCommerce variable products price range with the min price

The code below will replace the variable price range with “Price from” with the lowest price:

Replace the Variable Price range with the chosen variation price in WooCommerce 4+

The code below will update the displayed price based on the variations the customer selects from the list of product variations:

Add Prefix / Suffix to WooCommerce Product Prices

Add Prefix / Suffix to Product Prices

Add Prefix / Suffix to Product Prices

By default, WooCommerce Product Prices only shows the price in numbers and currency codes. Example $8. If you want to add any text to this price, you can use the code below. This code will convert $8 to Price: $8

In case you want to add to the back of the price, for example, $8 – The price includes VAT, you can refer to this code:

Result:

Add Prefix / Suffix to WooCommerce Product Prices

Add Prefix / Suffix to WooCommerce Product Prices

Hide Prices on the Shop & Category Pages

Remove Prices from the Shop Page Only

Or

If we want all archive pages like the shop page, category page, tag page to hide price, then we will return empty price if it is not a single product page. This code ensures that prices are still displayed on single product pages, shopping cart pages, and checkout pages

Hide prices on WooCommerce categories archive pages

Similar to the code above, this code checks if it is a WooCommerce category archive page, it will not display the price.

Hide Prices in WooCommerce for Specific Categories

Let’s say you only want to hide prices for a few categories, you can use:

Hide Prices in WooCommerce for Specific Products

Let’s say you want to hide prices for specific items with IDs 20,50,80. With the following code, you can hide the prices of products with ID 20,50,80 and show the prices for all remaining products on your Woocommerce page.

Remove the “Default Sorting” Dropdown

Remove or Rename SALE! Badge

When a product has a sale, Woocommerce will display a badge “Sale!” right under the product (or it could be in the product image, depending on the theme you’re using). I will guide you to Remove or Rename this SALE! Badges

Remove Sale! Label From Products – WooCommerce Shop & Single Product Pages

Rename / Translate Sale! Label

Replace “Choose an option” Text with Custom Text

This code is applied on a product page with product variations. This will make it clearer what product options your customers can choose from:

Change “Total” text on WooCommerce Order confirmation template

Hide the “Add to Cart” button in WooCommerce

How to Hide the “Add to Cart” button in WooCommerce the RIGHT way? If you search for the keyword “remove add to cart button Woocommerce”, you will get tons of tutorials. However, after trying the methods they guide for a while, none of them work, even causing website errors. Then I had to dig through the Woocommerce source code, and I found this code in the file single-product/add-to-cart/simple.php

On the 22nd line, you will see

So, to hide the “Add to Cart” button, simply set is_purchasable to false.
Then the Right solution is often Officially documented
Add the following code to your functions.php file, and tara, the “Add to Cart” button is gone. Even if the “Add to Cart” button is inserted manually, it won’t work. Clicking on it will show a message “Sorry, this product is currently not available for purchase.

Woocommerce hide add to cart button for specific products
Now, suppose you want to hide add to cart button for specific products in Woocommerce, you can use Conditional Tags. Here is checking the products with certain ids that we want to hide the “Add to Cart” button.

Alternatively, you can also completely hide the “Add to Cart” button using CSS:

Adding Contact Form 7 to WooCommerce Product

If your product has variations or can be ordered according to the customer’s wishes, customizing the product according to the customer’s wishes, you will definitely need an Enquiry form.
These Woocommerce Functions below will insert a tab into a single Woocommerce product page and display the Contact Form 7 form.

Step 1: Add a filter to create a product tab

First, we will use the filter function to add a tab containing our contact form 7 to “woocommerce_product_tabs

as a result, it will create a new tab with the name “Enquiry” at the end. You can rename this Enquiry to whatever title you want.

Step 2: Define a callback function

Once we have created the tab, we will have to define the callback function(contact_form7_tab) that will echo the contact form.

Here, id="1230” is the id of contact form 7. You must replace this id with your id for the new form to appear. You can get the id of the form as shown in the image below

Contact Form 7 ID and shortcode

Contact Form 7 ID and shortcode

Contact Form 7 + WooCommerce – Save form data in orders

By default, Contact Form 7 will only send mail and not save data to the Database. So what if we do the product Enquiry as above, and want that form to act like a Woocommerce form? Suppose, we want when we use contact form 7 to inquiry about the product, it will be saved in the orders list of Woocommerce, is it ok?
The answer is yes, we can do it. See the code below that will do exactly that.

As you can see, that code will Save form data in orders. When using, please change the form information corresponding to the data on your contact form 7. If you have any problems, you can comment below or contact us for free help.

Add Custom Product Fields in WooCommerce

This is a tutorial on how you could add custom product fields in WooCommerce.
The code below will display 3 more fields of numeric, text, and textarea data in the General section of Product Data. When entering data and updating, the data of those 3 fields will be saved in the product’s post meta.

Now, if you want to show these fields somewhere, you just need to add the code below there.

Note: To edit the Woocommerce theme, we recommend you to work with the WooCommerce custom templates. Instructions are here

If outside the loop, you must declare the product ID of the product, if you leave get_the_ID() as it is, it will get the ID of the product you are viewing.
The code will look like this:

Change Woocommerce “Thank you” message

There are many ways to change the message thanking customers for placing an order. But the best way to do this would be to filter the WC thank you message. The code below will change the default Woocommerce thank you message to

“Thank you for shopping with us on [TITLE OF WEBSITE], [NAME OF THE CUSTOMER]! We will contact you soon to confirm the order. Please pay attention to your phone number: [CUSTOMER PHONE NUMBER] to get our confirmation phone call.”

Remove billing Address at WooCommerce Checkout Page

Not only can you remove the billing address field, but you can also completely remove any field that you don’t need from the Woocommerce checkout page. The code below will remove the billing fields: postal code, company, last name, address 2 from the checkout page

Remove Shipping address at WooCommerce Checkout Page

Similar to removing the billing fields, you can also remove the fields in the shipping area. The function below will also remove the shipping fields postal code, company, last name, address2 from the checkout page

Reorder Checkout Fields in WooCommerce

Not only can you remove fields from billing and shipping, but you can also change the order of these fields. By default, Woocommerce sorts First Name -> Last Name -> Company -> Country …
The code snippet below will change the order of the billing fields. The lower the priority, the higher the order is. The example below is the email field that will be first.

Add Attributes to Short Description in WooCommerce

This Woocommerce function snippet will help to display a list of all the attributes with their value just before the short description of the single product page.

Or the function below will show attributes right after the short description

Hide SKUs on WooCommerce Product Page

There are quite a few ways to disable SKUs on product pages. You can hide the display, delete the display outside the frontend but keep it in the admin, or completely remove the SKU function both in the frontend and in the admin.
SKUs are located in the General Tab of the product data when creating or editing a product. By default, the SKU is displayed on the product page directly in front of the product meta.

Remove WooCommerce SKUs Completely

If you don’t need to use SKUs at all, you can disable them entirely using this snippet in your theme’s functions.php file:

Remove WooCommerce SKUs only on Product Pages

In case you just want to hide or delete SKU information outside the frontend on product pages but still keep it in the management section, we can use 2 ways. The first way is to use CSS to hide the SKU block. The second way is to use a piece of code inserted into the functions.php file in your child theme (or theme if you are not using a child theme).

Disable SKUs completely on product pages using CSS

Disable SKUs by code snippet in functions.php

Disable/hide Woocommerce single product page

For some reason you don’t want product A with ID 234 to be invisible or inaccessible, you can use the code below to insert it into the functions.php file in your child theme. The function of this code is that when accessing the single product page of the product with ID 234, it will be redirected to the homepage.

Disable/hide Woocommerce category page

If you want to hide category pages. for example, you don’t need these because you are using category shortcode to display products on other pages. And you want when clicking on any category it will go to the Shop page, below is the code that does that function.

Get custom product attributes in Woocommerce

This code shows a single attribute of the product. You can rely on it to choose to display several attributes of a product or create shortcodes to display elsewhere as you like.

Display Contact Button if Woocommerce Product Price is zero

The function below will show the contact us button and a link to the /contact-us/ page if the product price is empty or = 0. Other products with a price will still display the price and the Add to cart button as usual.

Add custom fields to WooCommerce registration form

The complete code below will Add Woocommerce registration form custom field using woocommerce_forms_field filter hook and save data with nonce verification.

WooCommerce show prices after login

You won’t need to use the Show Prices After Login Plugin for WooCommerce. The code below will make WooCommerce show prices after login

The code above will completely block the price view or show the Add to Cart button for customers who are not logged in. Even if you use the add-to-cart=ID URL, the system will notify you to log in or register an account before adding to the cart or viewing the price.

 

Content Protection by DMCA.com
Share.
About Author

Leave A Reply