Woocommerce 3中获取产品属性标签名称

Get the product attribute label name in Woocommerce 3

我在 Woocommerce 中的产品上使用了很多产品属性,我循环遍历 table 中的所有变体,这些变体可以在产品页面上使用简码显示。

为此 table 我需要 table 头部中的所有产品属性(这是在遍历变体之前),我使用以下方法获取属性:

$attributes = $product->get_variation_attributes();
foreach ($attributes as $key => $value) {
    echo '<td>'.&key.'</td>';
}

这不是很优雅,是吗?

所以这也有效:

$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
    echo '<td>'$attribute['name']'</td>';
}

在这两种情况下,我都得到了产品属性的别名。我需要获取标签名称,因为每个名称都有一个 Polylang 翻译(还有术语)。

如何获取产品属性标签名称而不是分类名称?

您将使用 wc_attribute_label() 专用的 Woocommerce 功能:

foreach ($product->get_variation_attributes() as $taxonomy => $term_names ) {
    // Get the attribute label
    $attribute_label_name = wc_attribute_label($taxonomy);

    // Display attribute labe name
    echo '<td>'.$attribute_label_name.'</td>';
}

或:

foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) {
    // Get the attribute label
    $attribute_label_name = wc_attribute_label($taxonomy);

    // Display attribute labe name
    echo '<td>'.$attribute_label_name.'</td>';
}