I know that I can create attributes of color, size and etc. But for some reason, I had to combine 3 attributes into one and then displayed them differently.

Now, I've the attribute with the values :

pa_mixed : Red, Blue, Green, S, M, L, Children, Adult

But on the front end I have to display like :

Colors : Blue, Red
Size : M
Aged : Children

So far I have tried :

    if (!empty ($product->get_attribute('pa_mixed'))):
        $pa_mixed = $product->get_attribute('pa_mixed');
    else:
        $pa_mixed = "";
    endif;

    $size = array('S','M','L'); 
      
      if ( in_array( $size, $pa_mixed ) ) {
         //colors attributes
      }
      //etc

And finally I got lost

请帮我找到正确的方法,谢谢

推荐答案

Updated,(handling fewer attribute classified types).

try 以下操作,在简短说明下方的单个产品页面中显示按类型分类的产品属性:

add_action( 'woocommerce_single_product_summary', 'display_pa_mixed_classified_attributes', 25 );
function display_pa_mixed_classified_attributes() {
    global $product;

    $options = $product->get_attribute('pa_mixed'); // Attribute term names set in the product (string)
    $options = ! empty($options) ? explode(', ', $options) : array(); // Convert to an array

    if ( ! empty($options) ) {
        // Classifying "Mixed" attribute terms
        $data    = array(
            'colors' => array('label' => __('Colors', 'woocommerce'), 'options' => array('Red', 'Blue', 'Green')),
            'size'   => array('label' => __('Size', 'woocommerce'), 'options' => array('S', 'M', 'L')),
            'aged'   => array('label' => __('Aged', 'woocommerce'), 'options' => array('Children', 'Adult')),
        );

        $types   = array('colors', 'size', 'aged'); // Defined types
        $values  = $new_types = array(); // Initializing

        // Loop through the "Mixed" attributes term names set in the product
        foreach( $options as $option ) {
            foreach( $types as $type ) {
                if ( isset($data[$type]['options']) && in_array($option, $data[$type]['options']) ) {
                    $values[$type][] = $option;
                    $new_types[$type] = $type;
                }
            }
        }
        echo '<div class="attribute_pa_mixed" style="padding-bottom:16px">';

        // Display: Loop through classified types
        foreach( $new_types as $type ) {
            printf( '<div class="attribute-%s">
            <span class="label">%s :</span> <span class="options">%s</span>
            </div>', $type, $data[$type]['label'], implode(', ', $values[$type]) );
        }
        echo '</div>';
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试和工作.

基于此属性,产品设置:

enter image description here

我在产品(single product page)上看到以下显示:

enter image description here

Php相关问答推荐

以编程方式更改新订单、已取消订单和失败订单的邮箱WooCommerce通知

Laravel数据表在一个视图中传递两个实例

仅在WooCommerce管理订单视图上显示订单项自定义元数据

从WooCommerce购物车和 checkout 页面自定义发货标签

在AJAX请求后,Laravel重定向.(不允许使用方法)

按与WooCommerce管理顺序列表中的特定字符串不匹配的特定元值筛选订单

laravel中获取的数据会自动更改时间戳值

这是在可召回的背景下吗?

筛选器具有多个查询之一

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

如何在laravel中添加延迟?

invalid_grant 和无效的 JWT 签名

使用产品自定义字段作为 WooCommerce 中特定用户角色的折扣百分比

带有分类术语数组的 WordPress Elementor 自定义查询 - 如何要求所有术语都在返回的帖子中

使用其ID查询和捕获与订单相关的其他详细信息的shopware 6方法

如何获取用户之间的最新消息列表

自定义错误页面不适用于 Apache 和 nginx 设置的 .htaccess

htaccess 无限循环问题

PHP 中的 curl 验证失败(openssh 连接正常)

如何获取最后一条消息属于 Laravel 中特定用户的对话列表