PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] How to show some few attributes on woocommerce category page?

 February 17, 2022     php, woocommerce, wordpress     No comments   

Issue

I want to show two of many attributes of my products on category pages namely heating capacity and cooling capacity as i am making an online store in woocommerce and i am working with my custom theme. it is not a variation or something it's just a normal attribute with text which i would like to show something like this as you can see the heating and cooling system listed on the category page itself i tried the code

if (!function_exists('shop_attributes_in_loop')) {
function shop_attributes_in_loop(){
    global $product;
        $attributes = $product->get_attributes();
        if(!empty($attributes)){
            $attribute_single = array_keys($attributes);
            $myArray = array();
        echo '<div class="product_attributes">';
            foreach ($attribute_single as $attribute => $value) {
                $myArray[] = ucfirst($value);
            }
        echo implode(', ', $myArray).'</div>';
        }
    }
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');

but it shows something wierd like Placement-of-outdoor-unit, Installation, Pa_model-no-indoor, Pa_model-no-outdoor etc.. can anyone help me.. enter image description here


Solution

I actually found out how its to be done :)

add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );

function custom_display_post_meta() {
   global $product;
   $attr = array('pa_cooling-capacity-watts', 'pa_heating-capacity-watts');
   foreach ( $attr as $attribute ) {
   $values = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'names' ) );
    echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
}


Answered By - Taj Khan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing