Ecommerce Shopify WordPress Discussion

Classify product attribute terms by type in Woocommerce product for display

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 please help me to find the right way, thank you
Updated (handling fewer attribute classified types) Try the following, that will display product attributes, classified by type, in single product pages, below the short description: 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>'; } } Code goes in functions.php file of your child theme (or in a plugin). Tested and work. Based on this attributes, product settings: I get the following display on a product (single product page):

February 4, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.