我已经尝试过以下解决方案:
$the_product = new WC_Product(get_the_ID());
$the_product->get_type(); // Return always "simple"
因此,我遵循了代码的文档
Class WC_Product()
:
/**
* Get the product if ID is passed, otherwise the product is new and empty.
* This class should NOT be instantiated, but the wc_get_product() function
* should be used. It is possible, but the wc_get_product() is preferred.
*
* @param int|WC_Product|object $product Product to init.
*/
现在,我可以通过以下方式获得正确的产品类型:
$the_product = wc_get_product( get_the_ID() );
var_dump( $the_product->get_type() );
输出正确:
string(8) "variable"