我有一个自定义页面,我试图列出商店中的每个产品及其变化 .
此外,我正在尝试列出按产品属性排序的变量'价格与slug'尺寸'
为了进行测试,我正在尝试使用ID 381我的代码来获取单个产品的变体
$handle=new WC_Product('381');
$variations1=$handle->get_avaialable_variations();
foreach ($variations1 as $key => $value) {
echo '<option value="'.$value['variation_id'].'">'.implode('/',$value['attributes']).'-'.$value['price_html'].'</option>';
}
但我得到的错误是
PHP Fatal error: Call to undefined method WC_Product::get_avaialable_variations()
我试过用
$handle=new WC_Product_Variable('381');
代替
$handle=new WC_Product('381');
但错误是一样的 .
这里有什么帮助?
3 回答
试试这个代码 .
注意:使用此$ single_variation-> get_price_html()但其输出带有html span标记,这会导致隐藏在选项标记中 .
测试了上面的代码,结果如下 .
如果这对您有用,请告诉我 .
您的代码中有拼写错误 -
get_avaialable_variations
它应该是
get_available_variations
我在stackoverflow之前找到了这个,不记得链接 (please edit if find the link to the answer with this method) . 它显示输出的变化数据 . 该方法将变量id作为参数 .
output :
.