• Resolved kdpatel

    (@kdpatel)


        $pid = 237; //variable Product ID
        $prod_test =  new WC_Product($pid);
        echo $prod_test->get_type(); 
    //echo "simple"

    Same with the Grouped products. I am using “Woo Product Bundles” plugin.

        $pid = 229; //grouped Product ID
        $prod_test =  new WC_Product($pid);
        echo $prod_test->get_type();
    //echo "simple"

    I installed fresh wordpress and I have no other plugin than woocomerce and it says “simple” instead of “variable”

Viewing 1 replies (of 1 total)
  • This one is a bit tricky, and it’s probably one of the reasons why the comment above the WC_Product class’s constructor says to not instantiate the class directly. The various product types are sub_classes of WC_Product and the type is set in each of them.

    So say you have a grouped product. Instead of calling new WC_Product( $pid ) you would want to use new WC_Product_Grouped( $pid ) instead.

    The issue comes from how the WC_Data_Store is loaded inWC_Product‘s constructor.

    $this->data_store = WC_Data_Store::load( 'product-' . $this->get_type() );

    If we’ve instantiated the main WC_Product class, then get_type will just return simple, even if the ID matches a variable or grouped product.

    public function get_type() {
    	return isset( $this->product_type ) ? $this->product_type : 'simple';
    }

    But if we instantiate one of the correct child classes, then get_type() will return the correct one. But having to know the type before calling may not be ideal.

    So if possible, you probably want to use something like wc_get_product( $pid ) to load the product instead.

Viewing 1 replies (of 1 total)
  • The topic ‘Variable/Grouped product shown as simple product’ is closed to new replies.