【问题标题】:Comma separated product attribute terms if there are multiple values available如果有多个可用值,则用逗号分隔产品属性项
【发布时间】:2021-04-18 14:55:56
【问题描述】:

在 WooCommerce 中,我目前正在构建一个函数,该函数将在商店页面中呼应某些产品属性。如果有多个属性值可用,我想用逗号分隔它们,但我不知道如何。

我的代码:

add_action('woocommerce_after_shop_loop_item_title', 'TitleVariations', 10);
function TitleVariations()
{
global $product;
    
$colormonth = $product->get_attribute('color-month');
$finish = $product->get_attribute('finish');
$design = $product->get_attribute('design');

echo '<span class="variation-display">';
echo __($colormonth, 'woocommerce');
echo __($finish, 'woocommerce');
echo __($crossdesign, 'woocommerce');
echo '</span>';
}

【问题讨论】:

  • @Loic 这个问题是重复的。我不感谢您取消关闭。如果你不喜欢我的欺骗目标,再找一个——会有几十个可供选择。
  • @mickmackusa 抱歉,这与 WooCommerce 产品类别无关……请参阅我的答案…… WooCommerce 产品属性是一种非常具体的自定义分类法,用于产品变体……还有自定义产品属性不是分类学......所以这不是一个重复。
  • @Josh 请在WordPress Development 上发布 WordPress 问题——一个专门的 WordPress Stack Exchange 社区。​​span>
  • 这可能已经足够接近重复了:stackoverflow.com/a/49630369/2943403 毕竟,OP 并没有努力检索数据,OP 只想知道如何内爆非数组-空值。

标签: php wordpress woocommerce product taxonomy-terms


【解决方案1】:

收集你的值到数组,然后implode这个数组:

$values = [
    __($colormonth, 'woocommerce'),
    __($finish, 'woocommerce'),
    __($crossdesign, 'woocommerce'),
];

// if some values returned by `__()` are empty strings, 
// you can filter your array so as to remove them
$values = array_filter($values);

echo '<span class="variation-display">';
echo implode(', ', $values);
echo '</span>';

【讨论】:

  • 感谢您的及时回复!我已经实现了这一点,但是有些产品只有一个属性,有些可能有 1-5 个不同的属性(我还没有添加其他属性)。例如,如果只有 1 个属性,我怎么能隐藏这些逗号?
  • 您还没有尝试使用单一值的解决方案...是吗?
  • @mickmackusa 可能__() 返回的值是空字符串。这就是为什么会有一些奇怪的,
  • "如何删除空元素?"和“如何用逗号连接字符串?”都是超级重复的问题。这个问题不清楚,因为我们不知道输入是什么。 array_filter() 是虚假值的贪婪杀手,因此这种原生技术可能会损坏输出。
  • @Loic 这就是为什么必须依靠退伍军人来策划好的内容而不是回答重复的问题。这样一来,Stack Overflow 就成为研究人员更好、更少冗余的资源。
【解决方案2】:

当有多个值时,WC_Product 方法get_attribute() 会给出一个逗号分隔的值字符串……您还需要检查每个不同的属性在列表中是否有一个术语……

要获取产品属性标签名称,可以使用wc_attribute_label()产品属性函数。

1)。如果您想获取带有标签名称和术语值的每个产品属性(一行中的每个不同属性),您将使用以下内容。

此代码还处理自定义产品属性

add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
    global $product;
    
    // Here define your product attribute names (slugs)
    $attribute_names = array('color-month', 'finish', 'design'); 
    $attributes      = array(); // Initializing
    
    // Loop Through product attributes array
    foreach( $attribute_names as $attribute_name ) {
        if( taxonomy_exists( 'pa_' . $attribute_name )  ) {
            $attribute = 'pa_' . $attribute_name; // Custom taxonomy
        } else {
            $attribute = $attribute_name; // Custom attribute (not a taxonomy)
        }

        $values_str = $product->get_attribute($attribute);

        if ( $values_str ) {
            $attributes[] = '<strong>' . wc_attribute_label($attribute) . ':</strong> ' . $values_str;
        }
    }

    // Output product attribute label / values pairs (one by line)
    if( ! empty( $attributes ) ) { 
        echo '<span class="variation-display">' . implode( '<br>', $attributes ) . '</span>';
    }
}

2)。但是,如果您想将所有产品属性术语作为逗号分隔的字符串获取,您的代码将类似于 Display specific product attributes under product title in Woocommerce archive pages

所以对于你的代码:

add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
    global $product;
        
    $color_month = $product->get_attribute('color-month');
    $finish      = $product->get_attribute('finish');
    $design      = $product->get_attribute('design');

    $attributes  = array(); // Initializing
    
    if ( $color_month ) {
        $attributes[] = $color_month;
    }
    if (  $finish ) {
        $attributes[] = $finish;
    }
    if ( $design ) {
        $attributes[] = $design;
    }

    // Output product attribute values
    if( ! empty( $attributes ) ) { 
        echo '<span class="variation-display">' . implode( ', ', $attributes ) . '</span>';
    }
}

代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

【讨论】:

  • 和这个答案有什么不同吗? stackoverflow.com/a/53161699/1839439
  • 逗号应该是逗号。所有这些!empty() 检查都是无用的,因为变量已经设置/声明——只需检查值是否为假。
  • 请不要乞求支持,您不再需要代表,您的评论使新成员的这种不良评论正常化。
  • ! empty( $attributes ) 在进行真实检查时也是不必要的开销。
  • 这并不正确,这意味着其他代码也在做不必要的额外工作。 $attributes 已声明,唯一要做的就是检查它是否真实,仅此而已。检查$attributes 是否为空:if (!$attributes) { 或非空:if ($attributes) {
猜你喜欢
  • 2018-01-25
  • 2020-10-14
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多