How to Create a Custom Product Type in WooCommerce

a person using her laptop while holding a credit card
Photo by Pavel Danilyuk on Pexels.com

With the constant growth of online stores based on WooCommerce, the need for custom product types has increased. In this article, I will show you how to develop your own custom product type in WooCommerce so that you can offer additional and unique functionalities to your customers.

Why do you need a custom product type?

Custom product types allow online stores to offer solutions more tailored to their specific products and services. For example, you might want to create a “Subscription” product type that is handled differently from regular products.

image 91

Steps to Create a Custom Product Type in WooCommerce:

  1. Register the Product Type: Explain how to use the add_action('init', 'register_my_subscription_product_type'); to register the new type.
  2. Add the Product Type to the Selector: Show how to add the product type to the product type dropdown menu using the filter product_type_selector.
  3. Customize the Visibility of Tabs in the Product Data Box: Explain how to show or hide specific tabs based on the selected product type.
  4. Add Custom Data to the Product: Describe how to add custom fields to the product, such as a checkbox to enable subscription or a text field to add subscription details.
  5. Save and Display Custom Data: Explain how to save this custom data and how to display it on the product page.
  6. Display the “Add to Cart” Button: Detail how to display the “Add to Cart” button for the custom product type.
<?php
/**
 * Plugin Name: Custom Product Type
 * Plugin URI:
 * Description:
 * Version:
 * Author:
 * Author URI:
 * License:
 * License URI:
 */

/**
 * Register a custom product type on initialization.
 */
add_action('init', 'register_my_subscription_product_type');

/**
 * Class WC_Product_my_subscription
 * 
 * Define a custom product type.
 */
function register_my_subscription_product_type()
{
    class WC_Product_my_subscription extends WC_Product
    {
        /**
         * WC_Product_my_subscription constructor.
         *
         * @param mixed $product
         */
        public function __construct($product)
        {
            $this->product_type = 'my_subscription';
            parent::__construct($product);
        }
    }
}

/**
 * Add the custom product type to product type selector.
 *
 * @param array $types Existing product types.
 * @return array Modified product types.
 */
function add_my_subscription_product_type($types)
{
    $types['my_subscription'] = __('My Subscription');
    return $types;
}
add_filter('product_type_selector', 'add_my_subscription_product_type');

/**
 * Customize visibility of tabs in product data metabox.
 *
 * @param array $tabs Existing product data tabs.
 * @return array Modified product data tabs.
 */
function wcs_hide_attributes_data_panel($tabs)
{
    $tabs['general']['class'][] = 'show_if_my_subscription';
    $tabs['attribute']['class'][] = 'hide_if_my_subscription';
    $tabs['shipping']['class'][] = 'hide_if_my_subscription';
    $tabs['inventory']['class'][] = 'show_if_my_subscription';
    return $tabs;
}

/**
 * Display pricing fields for the custom product type.
 */
function my_subscription_custom_js()
{
    if ('product' != get_post_type()) :
        return;
    endif;
    ?>
    <script type='text/javascript'>
        jQuery(document).ready(function() {
            jQuery('.options_group.pricing').addClass('show_if_my_subscription').show();
            jQuery('.general_options').show();
        });
    </script>
<?php
}
add_action('admin_footer', 'my_subscription_custom_js');

/**
 * Add custom product data tab.
 *
 * @param array $tabs Existing product data tabs.
 * @return array Modified product data tabs.
 */
function my_subscription_product_tab($tabs)
{
    $tabs['my_subscription'] = array(
        'label' => __('My Subscription Tab', 'dm_product'),
        'target' => 'my_subscription_product_options',
        'class' => 'show_if_my_subscription_product',
    );
    return $tabs;
}
add_filter('woocommerce_product_data_tabs', 'my_subscription_product_tab');

/**
 * Content for custom product data tab.
 */
function QL_custom_product_options_product_tab_content()
{
    ?><div id='my_subscription_product_options' class='panel woocommerce_options_panel'>
        <div class='options_group'><?php
                woocommerce_wp_checkbox(array(
                    'id' => '_enable_custom_product',
                    'label' => __('Enable Subscription'),
                ));

                woocommerce_wp_text_input(
                    array(
                        'id' => 'my_subscription_product_info',
                        'label' => __('Enter Subscription Details', 'dm_product'),
                        'placeholder' => '',
                        'desc_tip' => 'true',
                        'description' => __('Enter subscription details.', 'dm_product'),
                        'type' => 'text',
                    )
                );

                ?>
        </div>
    </div>
<?php
}
add_action('woocommerce_product_data_panels', 'QL_custom_product_options_product_tab_content');

/**
 * Save custom product data.
 *
 * @param int $post_id Product ID.
 */
function save_my_subscription_product_settings($post_id)
{
    $engrave_text_option = isset($_POST['_enable_custom_product']) ? 'yes' : 'no';
    update_post_meta($post_id, '_enable_custom_product', esc_attr($engrave_text_option));

    if (!empty($_POST['my_subscription_product_info'])) {
        update_post_meta($post_id, 'my_subscription_product_info', esc_attr($_POST['my_subscription_product_info']));
    }
}
add_action('woocommerce_process_product_meta', 'save_my_subscription_product_settings');

/**
 * Display custom product data in frontend.
 */
function my_subscription_product_front()
{
    global $product;
    if ('my_subscription' == $product->get_type()) {
        echo "<strong>Subscription Type: </strong>" . esc_html(get_post_meta($product->get_id(), 'my_subscription_product_info', true));
    }
}
add_action('woocommerce_single_product_summary', 'my_subscription_product_front');

/**
 * Display add to cart button for the custom product type.
 */
add_action("woocommerce_my_subscription_add_to_cart", function () {
    do_action('woocommerce_simple_add_to_cart');
});

Conclusion

With the ability to create custom product types in WooCommerce, the possibilities are virtually limitless. You can tailor your online store to perfectly fit the needs of your customers and products.

Are you ready to take your WooCommerce store to the next level? Start implementing custom product types today. If you need additional help or custom development, feel free to contact us.

Do you need help with a web project?

Do you need help with a web project? Don't hesitate to contact me. I develop complete and customized solutions with WordPress and PHP, using modern tools and processes, HTML, CSS, SCSS, PHP, JavaScript, Bootstrap, and more. Ready? Send me a message and let's talk about your web project!