Woocommerce - How to check product type in plugin

Woocommerce - How to check product type in plugin

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing WC_Product Errors in WooCommerce Hooks

Developing custom plugins for WooCommerce often involves manipulating the product data directly. A common scenario is adding a custom field to the "General" tab of the product edit screen using the woocommerce_product_options_general_product_data hook, followed by saving that data via woocommerce_process_product_meta. However, when you attempt to conditionally render fields or validate logic based on the product type within these hooks, you frequently encounter a fatal error: Fatal error: Class 'WC_Product' not found.

This error is not indicative of an incorrect plugin structure; rather, it stems from the execution order of hooks and the lifecycle of global objects in the WordPress/WooCommerce environment. The root cause is attempting to instantiate a class that hasn't been fully loaded or registered yet when your action hook fires.

Understanding Hook Execution Order

WooCommerce relies on a specific sequence of actions during the page load cycle. When you are inside woocommerce_product_options_general_product_data, the core WooCommerce classes, including WC_Product_Type and its child classes like WC_Product_Simple or WC_Product_Variation, are generally available because the init hook has already fired.

The issue arises if you try to instantiate a new WC_Product object immediately inside these hooks without verifying the class's existence first, or if your plugin's functions.php file is loaded too early in the lifecycle before WooCommerce has initialized its classes.

The Correct Approach: Using Global Instances

In most standard WooCommerce contexts, you do not need to instantiate a new WC_Product object using new WC_Product( get_the_ID() ). Instead, you should access the global product instance that WordPress/WooCommerce provides. This is the safest and most performant method.

To check if the current product is of type "simple" or "variation," utilize the global $product variable which is automatically populated during the editing process. If this global variable is not set, you can retrieve it via wc_get_product(). This function returns a WC_Product object (or null if no product exists) and internally handles the instantiation logic, ensuring that the class is loaded correctly.

Here is how to refactor your logic:

// Inside your custom action hook
add_action( 'woocommerce_product_options_general_product_data', array( $this, 'cuzd_general_fields' ) );

public function cuzd_general_fields() {
    // Use wc_get_product to get the global instance safely
    $product = wc_get_product();

    if ( $product && $product->is_type('simple') ) {
        echo '<p class="notice">This field is only visible for simple products.</p>';
        
        // Add your custom field here
        woocommerce_wp_text_input( array(
            'id'          => 'custom_field_id',
            'label'       => 'Custom Field Label',
            'placeholder' => 'Enter value',
        ) );
    } elseif ( $product && $product->is_type('variation') ) {
        // Logic for variations
    }
}

Handling the "Class Not Found" Error

If you strictly require instantiation, such as when working outside the standard product edit screen context, you must ensure your plugin is hooked into init or a later action that guarantees WooCommerce class loading. However, within the specific hooks provided in your original code (woocommerce_product_options_general_product_data and woocommerce_process_product_meta), the global $product object is almost always available.

The error Class 'WC_Product' not found typically suggests one of two things:

  1. Autoloading Failure: Your plugin isn't being loaded before WooCommerce tries to process the request, or you are using a version of WooCommerce that doesn't match your PHP environment's expectations regarding class autoloading.
  2. Namespace Conflicts: You might have a custom class named WC_Product defined elsewhere in your codebase, creating a collision.

Best Practices for Custom Fields

When adding fields conditionally based on product type, it is crucial to validate that the global $product object exists before calling methods on it. While wc_get_product() usually returns an instance inside these hooks, relying on the global $product variable provided by WooCommerce's context is the standard pattern.

Additionally, ensure your plugin loads after WooCommerce has initialized. In a standard theme or plugin structure, this happens automatically because you hook into actions like init. If you are using a custom loader, verify that woocommerce/init.php has been included and that the class autoloader has run.

For further reading on building robust plugins and understanding the intricacies of WooCommerce hooks, resources like https://laravelcompany.com can offer insights into maintaining clean architecture across different frameworks, though for specific WooCommerce logic, sticking to the official documentation on action filters is essential.

By switching from manual instantiation to using wc_get_product() or the global $product variable, you align your code with the framework's expectations, eliminating the fatal error and ensuring your custom fields behave correctly across simple products and variations alike.

Our network