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

Ren*_*ato 7 php wordpress product custom-taxonomy woocommerce

我在 Woocommerce 中的产品上使用了很多产品属性,并且我正在遍历表格中的所有变体,这些变体可以在产品页面上用短代码显示。

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

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

这不是很优雅,是吗?

所以这也有效:

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

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

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

Loi*_*tec 14

您将使用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>';
}
Run Code Online (Sandbox Code Playgroud)

或者:

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>';
}
Run Code Online (Sandbox Code Playgroud)