我正在构建自定义产品页面,但我遇到了各种变化问题,WooCommerce 中通过变体可变属性 ID 获取属性 slug 值,属性 slug 值数组包含在$variation 数组中,所以试试这个:
- global $product;
- if($product->is_type('variable')){
- $variations = $product->get_available_variations();
- foreach($variations as $variation)
- {
- $variation_obj = wc_get_product($variation['variation_id']);
- // Variation can have many poduct attributes
- $attr_slugs = implode(', ', $variation['attributes']);
- $stock = $variation_obj->get_stock_quantity();
- $COD = $variation_obj->get_sku();
- $regularprice = $variation_obj->get_regular_price();
- $saleprice = $variation_obj->get_sale_price();
- $diff = $regularprice-$saleprice;
- echo "<p>Var Name: " .$attr_slugs."</p>";
- echo "<p>Var Name: " .$COD."</p>";
- echo "<p>regular price: ".$variation['display_regular_price']."</p>";
- echo "<p>sale price: ".$saleprice."</p>";
- echo "<p>risparmi: ". $diff."</p>";
- echo "<p>in stock: ".$stock."</p>";
- }
- }