gpt4 book ai didi

php - 通过 Woocommerce 中的变体 ID 获取属性 slug 值

转载 作者:搜寻专家 更新时间:2023-10-31 20:58:14 25 4
gpt4 key购买 nike

我正在构建自定义产品页面,但我遇到了变体问题

这是我的代码:

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 - 蓝色”

最佳答案

属性 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>";
}
}

经过测试并有效。

关于php - 通过 Woocommerce 中的变体 ID 获取属性 slug 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50967692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com