When developing plugins for WooCommerce, sometimes, knowing the product’s type is very important. The funny thing about WooCommerce is if you create a product from the class “WC_Product” and run
The result is always simple.
This sounds confusing, doesn’t it? I feel the same way.
If you want the get_type method to return the correct type of the product, you need to create the product with the appropriate class. For example, for the get_type function to return ‘variable’, you need to create the product with the following syntax:
$product = new WC_Product_Variable($product_id);
However, how do you know what class to use when first creating the product?
Learn how to set custom shipping cost per product in WooCommerce.
We are going to use WooCommerce shipping class to set different shipping cost per product.
What is a WooCommerce shipping class
This is the definition of shipping class on WooCommerce documentation. I think it’s clear enough:
Shipping classes can be used to group products of similar type and used by some shipping methods, such as Flat Rate Shipping, to provide different rates to different classes of product.
For example, for a specific shipping zone, when you select a shipping method, inside the shipping method, you can set many shipping classes. Each class can have different cost. This makes it very easy for us to set different shipping cost for each product.
How to set custom shipping cost per product using shipping classes
This is our setup: We have two products, a hat and a vase. A vase requires a more careful shipping method because it’s fragile while a hat can use less secure shipping method. Now, we are going to create two shipping classes:
Fragile product shipping
Normal product shipping
As you can guess, the vase will use the first class, the hat will use the second.
Add shipping classes
Let’s go to WooCommerce->Settings->Shipping class and add these two classes:
Let click on the “Add shipping class” at the bottom right. You will see there are input boxes appear for you to enter the details. Don’t worry if you don’t see the place to enter shipping cost. We’ll do it later in shipping zones.
Now click on Save Shipping classes and you are done.
Our next step is to create a zone and add shipping methods (if you haven’t got any).
Add shipping zone and shipping method
Let’s click on Shipping zones and click on Add shipping zone. You’ll see an interface appears to enter the zone details. In my example, I use Asia as my zone but you can use any other zones if you like.
Now click on Add shipping method.
We are going to use the flat rate method.
Now, click on the edit link under the flat rate method:
Here you can see that there are settings to enter Shipping class costs. In this example, I set 20 for the Fragile product shipping class and 5 for Normal product shipping.
Now, click on Save changes, click Save changes again on the add shipping zone screen and you are done with setting up shipping classes.
Set shipping class per product
Finally, we are going to set the shipping class for our products.
Let’s go to the product data area (right under the product description area) and click on shipping. When you click on the “Shipping class” drop down, you’ll see the two options we’ve just created. Select Fragile product shipping for the vase and Normal product shipping for the hat.
Now, let’s add vase and hat to cart and go to the cart page. You’ll see the shipping cost is added accordingly:
As you can see, using shipping class can let us setup some flexible shipping for our product. There is no limit of number of shipping classes you can create so you can easily have many shipping options.