sgb*_*004 6 wordpress woocommerce
我尝试获取分组产品的类型,但如果我使用 WC_Product_Factory,woocommerce 返回空或始终“简单”。
当我使用时:
$the_product = new WC_Product(2886);
echo $the_product->product_type;
Run Code Online (Sandbox Code Playgroud)
返回空。
当我使用 WC_Product_Factory 时:
$the_product = new WC_Product(2886);
$the_product_factory = new WC_Product_Factory();
$the_product = $the_product_factory->get_product($the_product);
echo $the_product->product_type;
Run Code Online (Sandbox Code Playgroud)
总是返回“简单”
我尝试使用:
$the_product = wc_get_product(2886);
echo $the_product->product_type;
Run Code Online (Sandbox Code Playgroud)
但这会返回错误“在...中的非对象上调用成员函数 get_product()”
我的代码位于:
add_action( 'plugins_loaded', function(){
$the_product = new WC_Product(2886);
echo $the_product->product_type;
$the_product = new WC_Product(2886);
$the_product_factory = new WC_Product_Factory();
$the_product = $the_product_factory->get_product($the_product);
echo $the_product->product_type;
$the_product = wc_get_product(2886);
echo $the_product->product_type;
die();
});
Run Code Online (Sandbox Code Playgroud)
嗯,谢谢你的帮助!
我找到了解决方案还是我的错误,将plugins_loaded更改为init:
add_action( 'init', function(){
$the_product = new WC_Product(2886);
echo $the_product->product_type;
$the_product = new WC_Product(2886);
$the_product_factory = new WC_Product_Factory();
$the_product = $the_product_factory->get_product($the_product);
echo $the_product->product_type;
$the_product = wc_get_product(2886);
echo $the_product->product_type;
die();
});
Run Code Online (Sandbox Code Playgroud)
还有这个工作!
快乐编码!:)
归档时间: |
|
查看次数: |
2908 次 |
最近记录: |