Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    woocommerce-marketplace-mod

    Create a Custom Product Type in Woocommerce

    By default, Woocommerce provides us with four types of products namely Simple product, Variable product, Group product and External/Affiliate Product.

    What if you need to add a new product type according to your need. Having your own product type, allows you to have control on product settings. For example, you could set default values like price, visibility, etc.

    In this article, we will learn how to

    • Register a new product type.
    • Add a corresponding tab to it when selected.
    • Add fields to the created tab.
    • And finally show those details on the front at product page.

    So, lets start by firstly registering a product type. In my case, I am creating a product type ‘demo’.

    Below code snippets shows you how to register a new product type which executes at ‘init’ hook at the function file of the plugin.


    In above code, we create a new product type as ‘demo’ by extending the Woocommerce WC_Product class.

    Than, after registering the product type we need to add the product type to the product type selector for choosing what kind of product the user want to make. For that we add our new product type with default product type provided by Woocommerce as shown in below code.


    After adding option to select the new product type we need to add a new tab associated with it and  we will add a field ‘ Demo Product Spec ‘ for describing the product type. This can be done by


    And for adding  field to it we need to add the execute following code on ‘woocommerce_product_data_panels’ hook.


    The last step to do in admin side process is to save the details of the field ‘ Demo Product Spec ‘ .

    To save those details we will use the hook ‘ woocommerce_process_product_meta’ and add save the data in the postmeta as shown in below code snippet :


    Now the admin side work is done. Now you will be able to select the new post type registered, see the new tab associated with it and finally add the details in the field as shown in below screenshot:

    At last, we need to show the information that we added to the demo product type at the product page.

    For that we’ll execute the below code on  hook  ‘ woocommerce_single_product_summary ‘  and add the details to the product page in the front as :


     

    After doing all this we’ll now see the demo product that we created in the front-end with the information added to its custom field as shown in below :

     

     

    Hope this blog will help you to develop custom functionality in your plugin in a better way. Try this and if you have any query then just comment below.

    Thanks for reading 🙂

     

    . . .

    Comments (4)

    Add Your Comment

  • marie
    awesome post, one question, what is in demo, you dont want shipping tab to appear how would you hide it
    • Nikhil Chaudhary
      The shipping tab automatically hides if you make the product virtual.
  • sandeep
    Very nice article with code
    • Nikhil Chaudhary
      Thanks for your feedback.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close