Get Product (or Variation) by SKU

In this tutorial I will show you how to get a product or a product variation object in WooCommerce if you only know its SKU. It is easy-peasy when we get a product object by ID, we just have to use wc_get_product() function, but we can not pass SKU to that function.

So I am going to show you three different ways how you can obtain a product object from SKU, we are going to do it both WooCommerce-way (with WP_Product_Query and with wc_get_product_id_by_sku() function) and WordPress-way (with WP_Query).

Create Products Programmatically

In this tutorial I am going to guide you through the process of creating products in WooCommerce in code.

It is the most correct way of creating products in latest WooCommerce versions. So, please forget about wp_insert_post() and update_post_meta() functions. Yes, I perfectly remember that products are WordPress custom post types and product prices are post meta but it doesn’t allow us to use those functions anyway, because there is a little more than that and it could cause bugs.

In this tutorial we are going to use CRUD objects that were introduced in WooCommerce 3.0 (CRUD is an abbreviation of Create, Read, Update, Delete). The CRUD objects we are interested in right now are:

  • WC_Product_Simple – for simple products,
  • WC_Product_External – for external products,
  • WC_Product_Grouped – for grouped products,
  • WC_Product_Variable – variable ones.

We are also going to use plenty of methods of these objects, that allow us to configure our product data.

Change Product Tab Titles and Headings

In this tutorial I will show you how to rename WooCommerce product tabs. Each tab has a title which is the link text you click in order to change the tab and also each tab has its <h2> heading which is displayed just before the tab content. By default appropriate titles and headings are the same. In this tutorial I will show you how to rename all of them.

How to Remove Add to Cart Button

Before answering this question, let’s answer another question “why should we remove add to cart or read more buttons anyway?”

There are could be different situations:

  • Maybe it is just a part of an idea of your website design – so you would like to hide add to cart buttons only on shop page.
  • Maybe you would like to disable add to cart buttons everywhere – both in archive and single product pages, in that case you’re turning your shop into a catalog.
  • Or probably you may want to remove add to cart button for specific products only.

Check if Product Belongs to a Product Category or Tag

Are you trying to make it work with in_category() function but nothing happens? Well, in this tutorial you will find out why.

Actually it is a very common mistake and I also did it before.

First things first, let’s begin with the fact that products in WooCommerce are a custom post type product and product categories are a custom taxonomy product_cat. When you work with custom taxonomies, do you use functions for categories/tags or for taxonomies?

Comparing to is_product_category(), WooCommerce doesn’t have its own conditional tags here, so we have to use default WordPress conditional tags like has_term().

Remove Product Prices

In this tutorial I will show a correct way of removing product prices from single product pages and shop page (product archives).

There are plenty of tutorials out there that recommend you to use woocommerce_get_price_html hook which is totally wrong, because that hook is intended to edit the HTML of the price element. We are not going to hide prices with CSS as well.

WooCommerce connects product prices to hooks:

So all we need to do is to disconnect price displaying functions from these hooks!

Random Product Sorting with Pagination

There are already a couple tutorials on this website related to product sorting, you can read for example, how to change a default sorting order, how to completely remove it or how to add a custom sorting option.

Today we are going to go even further and add an option to sort products in random order and retain it during pagination.

First things first, let’s add a new sorting option. There is another tutorial about that, so you can read more if you want. Right now we are going to do it with woocommerce_catalog_orderby filter hook.