Php 如果有多个可用值,则使用逗号分隔的产品属性术语

Php 如果有多个可用值,则使用逗号分隔的产品属性术语,php,wordpress,woocommerce,product,taxonomy-terms,Php,Wordpress,Woocommerce,Product,Taxonomy Terms,在WooCommerce中,我目前正在构建一个函数,该函数将响应商店页面中的一些产品属性。如果有多个属性值可用,我想用逗号分隔这些属性,但我不知道如何分隔 我的代码: add_action('woocommerce_after_shop_loop_item_title', 'TitleVariations', 10); function TitleVariations() { global $product; $colormonth = $product->get_attribu

在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>';
}
add_action('woocommerce_后加_shop_loop_item_title','TitleVariations',10);
函数标题变化()
{
全球$产品;
$colormonth=$product->get_属性('color-month');
$finish=$product->get_属性('finish');
$design=$product->get_属性('design');
回声';
echo($colormonth,'woocommerce');
回声($finish,'woocommerce');
echo(交叉设计,woocommerce);
回声';
}

将您的值收集到数组中,然后
内爆
此数组:

$values=[
__($colormonth,'woocommerce'),
__($finish,'woocommerce'),
__($crossdesign,'woocommerce'),
];
//如果`\uuu()`返回的某些值是空字符串,
//您可以筛选阵列以删除它们
$values=数组过滤器($values);
回声';
回波内爆(',',$值);
回声';

当存在多个值时,WC\u Product方法
get\u attribute()
会给出一个逗号分隔的值字符串…您还需要检查每个不同的属性是否至少有一个术语

要获取产品属性标签名称,可以使用
wc\u attribute\u label()
product attribute函数

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>';
    }
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_loop_product_attributes',10);
函数显示\循环\产品\属性()
{
全球$产品;
//在这里定义产品属性名称(slug)
$attribute_names=数组('color-month','finish','design');
$attributes=array();//正在初始化
//循环遍历产品属性数组
foreach($attribute\u name作为$attribute\u name){
if(分类法_存在('pa_.$attribute_name)){
$attribute='pa.'$attribute\u name;//自定义分类法
}否则{
$attribute=$attribute\u name;//自定义属性(不是分类法)
}
$values\u str=$product->get\u属性($attribute);
如果($values\u str){
$attributes[]=''.wc\u attribute\u标签($attribute)。:'.$values\u str;
}
}
//输出产品属性标签/值对(逐行)
如果(!empty($attributes)){
回显“”。内爆(“
”,$attributes)。“”; } }

2) 。但是,如果您想将所有产品属性术语作为逗号分隔的字符串,那么您的代码将类似于

因此,对于您的代码:

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>';
    }
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_loop_product_attributes',10);
函数显示\循环\产品\属性()
{
全球$产品;
$color\u month=$product->get\u属性('color-month');
$finish=$product->get_属性('finish');
$design=$product->get_属性('design');
$attributes=array();//正在初始化
如果($color\u月){
$attributes[]=$color\u月;
}
如果($finish){
$attributes[]=$finish;
}
如果($设计){
$attributes[]=$design;
}
//输出产品属性值
如果(!empty($attributes)){
回显“”。内爆(',',$attributes)。“”;
}
}

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

感谢您的及时回复!我已经实现了这一点,但是有些产品只有一个属性,有些可能有1-5个不同的属性(我还没有添加其他属性)。例如,如果只有一个属性,我怎么能隐藏这些逗号?您还没有用一个值尝试此解决方案……是吗?@mickmackusa可能
\uu()
返回的值是空字符串。这就是为什么会有一些奇怪的
“如何删除空元素?”和“如何用逗号连接字符串?”都是非常重复的问题。这个问题不清楚,因为我们不知道输入是什么
array\u filter()
是虚假值的贪婪杀手,因此这种原生技术可能会破坏输出。@Loic这就是为什么必须依靠老兵来策划好的内容,而不是回答重复的问题。这样,堆栈溢出就成为研究人员更好、更少冗余的资源。@Loic这个问题是一个巨大的重复。我不感激你不去打扰我的结束。如果您不喜欢我的dupe目标,请另找一个——将有几十个可供选择。@mickmackusa抱歉,这与WooCommerce产品类别无关……请参阅我的答案……WooCommerce产品属性是一个非常特定的自定义分类法,这是用于产品变体的…还有一些自定义的产品属性不是分类法…所以这不是dup。@Josh请在一个专用的WordPress Stack Exchange社区上发布WordPress问题。这可能已经足够接近一个副本了:毕竟,OP并没有在检索数据方面遇到困难,OP只是想知道如何内爆非空值数组。或者它与这个答案不同?COMA应该是逗号。所有这些
!empty()
检查是无用的,因为变量已经设置/声明了——只需检查值是否为假。请不要乞求投票,您不再需要rep,您的注释会将此错误的注释规范化为新成员。
!空($attributes)
也是不必要的开销,当truthy检查完成时。这并不意味着它正确,它意味着其他代码也在做不必要的额外工作<代码>$attributes已声明,onl