Site icon Learn Web Development & Programming, Artificial Intelligence, WordPress, Shopify Articles

Create WooCommerce custom product type programmatically

WooCommerce custom product type

If you’ve used WooCommerce before, you should be aware that it offers four default product types: Simple product, Variable product, Group product, and External/Affiliate Product.

There’s a chance you’ll need to develop your own woocommerce custom product type if you’re working on a complex and customized solution using WooCommerce.

These woocommerce product types can be Appointment Bookings, Subscriptions, or something completely different.

You can customize the settings of a product if you have your own product type. For example, you can select default options for items like price, visibility, etc.

Suggested Read: Woocommerce hook to display offers text after shop loop item

WooCommerce: Add custom product type

If you want to learn how to create your own woocommerce custom product type, then this article is very important and helpful for you.

In this article, we will learn how to create a custom product type step by step:

Before we start, I want to inform you that we will write all steps code under your theme functions.php file. Let’s start the process to create a custom product type.

Step-1: Register a new custom product type.

In this step, we will register our own custom product type class and extends the WooCommerce default class WC_Product.

PHP Snippet: Register a New Product Type @ WooCommerce Admin

Go to your theme or child theme folder and open the functions.php file for edit. Copy and Paste the below code snippets to your theme functions.php file.

In the above code, we create a new product type as ‘my_subscription’ by extending the Woocommerce WC_Product class. Once the product type has been registered, it must be added to the product type selector so that customers can select the type of product they would like to create.

Add custom Product Type to Select Dropdown

Copy and Paste the below code snippets after the first code to your theme functions.php file.

Please refer below image for output

Step-2: Show Product Data General Tab Prices

In this step, we will code to display or hide other default attributes data panels like general, inventory, shipping, linked_product, variations, advanced

After adding the above code you can see the general attribute tab to add/edit product price. Please refer below image for output

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Step-3: Add an associated product data tab, when selected.

We are going to add a new tab to it and create fields named “Enable Subscription” and “Enter Subscription Details” that describe the product type.

To save the above fields details we will use the hook ‘ woocommerce_process_product_meta’ and add save the data in the post meta as shown in below code snippet :

The work of the admin panel has been completed.

You are now able to choose the newly registered post type, view the tab, and then enter the details in the field as shown in the screenshot below:

Step-4: Display information on the front end of a single product page.

On the single product page, we need to display the details that we added to the subscription product type. We will use “woocommerce_single_product_summary” hook to display the details on the front end. Copy and paste below code snippet:

Please refer below screenshot for the output

Step-5: Show Add to Cart Button

This is our final/last step where we will add code for add to cart button. Let’s copy and paste below code snippets.

Wow, Now we can see the subscription product that we created in the backend with the information is ready to purchase for customers. Please Refer to the below screenshot.

Also Read: How to Create a Custom Payment Gateway Plugin for Woocommerce?

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Wrapping Words

Thanks for reading 🙏, I hope you found the How to Create WooCommerce custom product type tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.

FAQs

What is the default product type in WooCommerce?

Simple product, Variable product, Group product, and External/Affiliate Product is the default product types in WooCommerce.

What is a custom product type in WooCommerce?

As you know Simple product, Variable product, Group product, and External/Affiliate Product is the default product types.
Same as custom Product types can be various things such as Appointment Bookings, Subscriptions, Tickets, or something completely different. It totally depends on you what name you want to define for the custom product type.

How do I create a custom product type for WooCommerce?

Create custom product type step by step:
1. Register a new WooCommerce custom product type.
2. Show Product Data General Tab Prices
3. Add an associated product data tab, when selected.
4. Display information on the front end of a single product page.
5. Show Add to Cart Button

Exit mobile version