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

Get attribute slug value by variation ID in Woocommerce

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

这是我的代码:

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

我想知道是否可以提取属性或变体的单个名称?

或没有完整标题的变体名称?

示例: 我想要 "blue"... 而不是 "Magic beautiful Pouf Lumaland - blue"

如有任何帮助,我们将不胜感激。

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

已测试并有效。

更新代码:

现在可以从一个 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>

对于target It,我们需要一个Attribute。 我们可以通过这个得到它(这是一个简单的想法):

// 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 定位到输出列表中的目标。 现在,很快就会看到你的最终代码...

感谢您的帮助。