【问题标题】:Get the product attribute label name in Woocommerce 3在 Woocommerce 3 中获取产品属性标签名称
【发布时间】:2019-04-04 09:58:54
【问题描述】:

我在 Woocommerce 中的产品上使用了很多产品属性,并且我正在循环浏览可以在产品页面上使用简码显示的表格中的所有变体。

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

$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>';
}

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

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

【问题讨论】:

  • 尝试“var_dump”“$attributes”对象。这可能会让您了解需要在循环中使用“$attributes”对象的哪个属性。
  • 谢谢。有助于我了解更多这里发生的事情。不幸的是,在数组中没有名称,只有 slug。我对这两个代码块都进行了尝试。使用第二种方法时得到的数组包含 id、position、variation ...但仍然没有实际名称。

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

您将使用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>';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2020-11-02
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多