更新代码:
现在可以从一个 id 循环所有可能的变体,并为它们分配一个 id 以通过 Js 定位它们:
// cicle all outputs
$allvariations = $product->get_available_variations();
foreach($allvariations as $variation)
{
// find actual variation, ID
$this_Variation = wc_get_product($variation['variation_id']);
// all sub value of the ID
$type_selected = implode(', ', $variation['attributes']); // this create a simple autoput name
$type_target = implode('_', $variation['attributes']); // this create a Id for targetting the correct outputs
$stock_status = $this_Variation ->get_stock_status();
$stock = $this_Variation ->get_stock_quantity();
$COD = $this_Variation ->get_sku();
$regularprice = $this_Variation ->get_regular_price();
$saleprice = $this_Variation ->get_sale_price();
$diff = $regularprice-$saleprice;
$imgurl = $variation['image']['url'];
//... and other if you wont
//output
echo'<div id="'.$type_target.'">';
// html Output:
if( $imgurl )
{
echo '<img src="'.$imgurl.'"/>';
}
else
{
echo '<img src="unavailabe" />';
}
echo "<p>Product type: " .$type_selected."</p>";
echo "<p>P. COD: " .$COD."</p>";
if( $saleprice >= 1 )
{
echo "<p>Regular price: ".$variation['display_regular_price']."</p>";
echo "<p>Sale price: ".$saleprice."</p>";
echo "<p>Sale difference: ". $diff."</p>";
}
else
{
echo "<p>Price: ".$variation['display_regular_price']."</p>";
}
if( $stock == 0 || $stock_status == "outofstock" )
{
echo "<p><b>OUT OF STOCK!</b></p>";
}
else
{
echo "<p>We have: ".$stock." atricles in stock</p>";
}
echo "</div>";
}
产品输出示例(所有产品和提取选项的循环):
<div id="Small_Orange">
<img src="http://xxxx/xxx/theimage.jpg">
<p>Product type: Small, Rose</p>
<p>P. COD: PF_LMLND-112233</p>
<p>Price: 22</p>
<p>We have: 150 atricles in stock</p>
</div>
对于目标它我们需要一个属性。
我们可以通过这个得到它(这是一个简单的想法):
// cicle all options
foreach ( $product->get_attributes() as $attribute )
{
// Write a option name group (ex... "color")
echo "<p><b>".$attribute['name']."</b></p>";
// Loop the attr by name
$attributeValues = explode('|',$attribute['value']);
foreach ( $attributeValues as $thisValue )
{
// From name to radio selector
echo $attributes_list = '
<span class="button-group">
<label for="'.$thisValue.'">'.$thisValue.'</label>
<input class="button-radio" type="radio" id="'.$thisValue.'" name="'.$attribute['name'].'" />
</span>
';
}
}
现在我们有一个单选或按钮或其他用于将 id 定位到输出列表中。
现在,很快就会看到你的最终代码......
感谢您的帮助。