I am adding the variable product in woocommerce pragmatically but the product created in the admin panel is always a simple product. I am using the following code to set the product type variable.

$producttype = wp_set_object_terms($post_id, 'variable', 'product_type');

When I print the output in $producttype it says "Invalid taxonomy".

What is the problem behind this ?

2 Answers 2


Not sure exactly where you're adding that code, but there is a generally accepted way of doing this, which is outlined pretty well here: How to add a new product type on woocommerce product types?

There are 2 ways of doing this in the Wordpress/Woocommerce "recommended" types of approaches.

1) Copying the files that need these new classes etc into your themes folder into an identical folder structured /woocommerce folder. Technically of course you'll have to re-copy that folder over if you ever update your theme.

2) You can also just do it all in your theme's functions.php file, which is what I did.


This is exactly what my code starts out like (and you can see where product_type is declared as ta_fundraising_campaign below):

 * Fundraising custom product_type for WooCommerce.

// add a product type
add_filter('product_type_selector', 'ta_add_custom_product_type');
function ta_add_custom_product_type($types)
    $types['ta_fundraising_campaign'] = __('Fundraising Campaign');
    return $types;

// create product class on plugins_loaded hook
add_action('plugins_loaded', 'ta_create_custom_product_type');
function ta_create_custom_product_type()
     // declare the product class
     class WC_Product_FundRaisingCampaign
        extends WC_Product
        public function __construct($product)
           $product->product_type = 'ta_fundraising_campaign';


Now here's the tricky part - because there is practically NO support out there on the internet for adding custom product_types for variable product_types.

If, you're trying to make your own variable custom product_type, then you'll probably want to add back some of the "Settings" ie. Data Panels that allow you to choose different options like Attributes, Inventory and Variations.

****** This is how you add those: *******

add_filter('woocommerce_product_data_tabs', function($tabs) {

        array_push($tabs['inventory']['class'], 'show_if_variable show_if_membership show_if_ta_fundraising_campaign');
        array_push($tabs['variations']['class'], 'show_if_membership show_if_ta_fundraising_campaign');

        return $tabs;

    }, 10, 1);  

And then, for some reason the "Used for variations" checkbox may not show under the Attributes tab. So you may want to copy the file that it's in over to your theme folder and then add a show_if to the class, like this:

This is the file where the checkbox is (copy it into a woocommerce folder in your theme's folder with the exact same structure ie. /yourtheme/woocommerce/includes/admin/meta-boxes/views/html-product-attribute.php):


Then towards the bottom of that file you'll see the checkbox (below). But, notice how the div class has 2 show_if classes....the 2nd one is the one I added to show it on my custom product_type. Hope all this helps some others because it took me forever to figure all this out!!

<div class="enable_variation show_if_variable show_if_ta_fundraising_campaign">
                            <label><input type="checkbox" class="checkbox" <?php checked( $attribute['is_variation'], 1 ); ?> name="attribute_variation[<?php echo $i; ?>]" value="1" /> <?php _e( 'Used for variations', 'woocommerce' ); ?></label>

You problem is that your taxonomy is not registered. Take a look at the output of get_taxonomies() at the end of your page to see which taxonomies are available:

add_action( 'shutdown', function() {
    printf( '<pre>%s</pre>', var_export( get_taxonomies(), TRUE ) );
}, PHP_MAX_INT -1 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.