ProgramMatek: Exploring Woocommerce Product Types

Welcome to a comprehensive tutorial on Woocommerce product types. In this article, we will delve into the intricacies of understanding product types, creating custom ones, and hiding default types.

Understanding Product Types in Woocommerce

Woocommerce provides four default product types, and you also have the option to create custom ones. But how do you easily check the type of a product in your code? There are two ways to achieve this:

  1. Using the get_type() method of the WC_Product class.
  2. Using the is_type() method of the same class.

To get the product type by ID, you can use either of the following code snippets:

$product = wc_get_product($product_id);
$type = $product->get_type();
$product = wc_get_product($product_id);
if ($product->is_type('simple')) {
    // Product is of type Simple
}

Feel free to check if a product is Simple, Grouped, or Affiliate/External using these methods.

Checking for Downloadable and Virtual Products

While “Downloadable” and “Virtual” are not technically product types, you can still check for them using specific methods. Use the is_downloadable() method to determine if a product is downloadable, and the is_virtual() method for virtual products.

Customizing Product Types

Suppose you want to remove default product types to improve your store’s user experience. Understand that product_type is a custom taxonomy with a hidden user interface. Each product type, such as “Simple,” “Grouped,” or “Variable,” is a taxonomy term.

You might think that removing a specific term from the database would suffice, but here’s the catch: product types are hardcoded in the WooCommerce files. To remove default product types, you need to hook into the product_type_selector.

Let’s say you want to remove the “Grouped” and “External” product types. By implementing the following code, those types will be eliminated:

function remove_default_product_types($types) {
    unset($types['grouped']);
    unset($types['external']);
    return $types;
}
add_filter('product_type_selector', 'remove_default_product_types');

What happens next? The removed product types won’t magically transform into “Simple” or any other type. However, you won’t be able to edit the removed product types, and you’ll need to re-save each product as a different type. Additionally, the removed product types won’t be available in the dropdown menu of the Product Data metabox or the “Filter by product type” option on the All Products page.

See also  WooCommerce Local Delivery

By the way, if you’re eager to remove the “Downloadable” and “Virtual” options from the filter, check out our tutorial on how to do that.

Creating Custom Product Types for WooCommerce

Why would you need to create a custom product type in the first place? To enhance the user experience, of course! Let’s consider the example of the WooCommerce Subscriptions plugin, where a subscription product is a custom product type. This approach offers several benefits:

  1. Configure the product settings and tabs available in the Product Data metabox.
  2. Utilize additional filters on the All Products page.
  3. Query products using parameters with wc_get_products() and WC_Product_Query.
  4. Implement and customize methods specific to your custom product type.
  5. Override default WC_Product methods to meet your requirements.

Registering a Custom Product Type as an Extension

Each default WooCommerce product type has its own PHP class that extends the WC_Product default product class. To create a custom product type, you need to create another PHP class that follows the same pattern.

Here’s an example of creating a custom product type called “Gift” with the class name WC_Product_Gift:

class WC_Product_Gift extends WC_Product {
    public function get_type() {
        return 'gift';
    }
}

To add your custom type to the list, you can use the product_type_selector filter hook in a similar manner as removing the default types.

Customizing Product Settings and Tabs

When you switch between different product types, you’ll notice that the product settings and data tabs change accordingly. But how can you control and modify these settings? By using the show_if_{type} and hide_if_{type} CSS classes.

For instance, let’s explore how to hide the “Attributes” tab and bring back the “General” tab with pricing information for our custom product type.

See also  Boost Your Sales with Woocommerce Subscriptions Plugins

To hide the “Attributes” tab, you can use the woocommerce_product_data_tabs filter hook. However, displaying the “General” tab requires a slightly different approach. This tab is only visible if it contains HTML fields specific to a particular product type. Here are two options to consider:

  1. Add another section with fields using the woocommerce_product_options_general_product_data filter hook and the show_if_gift CSS class.
  2. Use JavaScript to apply the show_if_gift CSS class to the pricing fields.

Customizing Methods

You can redefine various methods for your custom product type. For example, you can calculate the price of your custom product type differently. Let’s take a look at an example where we override the get_type() method:

class WC_Product_Gift extends WC_Product {
    public function get_type() {
        return 'gift';
    }

    public function get_price() {
        // Custom logic to calculate the price
    }
}

ProgramMatek is here to help you make the most of your Woocommerce product types. Check out our website for more tips and tutorials!

WooCommerce custom product type