我正在构建自定义产品页面,但是版本存在问题
这是我的代码:
global $product;
$variations = $product->get_available_variations();
foreach($variations as $variation)
{
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_variation($variation_id);
//THIS IS PROBLEM FAIL TEST (It's exemple)
$type = $variation_obj->get_attributes()->get_name();
//-----
$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>Variation: " .$type."</p>";
echo "<p>P.Code: " .$COD."</p>";
echo "<p>Regular price: ".$regularprice."</p>";
echo "<p>Sale price: ".$saleprice."</p>";
echo "<p>Difference: ". $diff."</p>";
echo "<p>Stock: ".$stock."</p>";
}
我想知道是否可以提取属性或变体的单个名称?
还是没有完整标题的版本名称?
示例: 我想要“蓝色” ...而不是“神奇的美丽Pouf Lumaland-蓝色”
任何帮助将不胜感激。
答案 0 :(得分:1)
{sl}属性数组包含在$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>";
}
}
经过测试可以正常工作。
答案 1 :(得分:0)
更新代码:
现在可以从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定位到输出列表中。 现在,请稍后再见您以获取最终代码...
感谢帮助。