I am trying to get product attribute slug. I have used below code but it display name.

echo $_product->get_attribute( 'pa_color' );

I am working on woocommerce/cart/cart.php file in theme folder.

Also I checked this is coming in anchor url of product image in cart page but not getting it

anchor url: https://example.com/productos/sweatshirt/?attribute_pa_color=aa2757&attribute_pa_talla=m

I am working on this from today morning but I have not get success. Please guide.

enter image description here


$values = wc_get_product_terms( $product->id, 'pa_color', array( 'fields' => 'all' ) );
foreach($values as $val){
  echo "<pre>";
  echo "</pre>";
| improve this answer | |
  • I used this code, its showing all attributes but i need only one respective attribute. – Abi Sharma Jul 14 '18 at 16:02
  • @AbiSharma - I just found something related to what you are searching for in below question....just display variable value using below thread implementation. wordpress.stackexchange.com/questions/240283/… – dhirenpatel22 Jul 14 '18 at 18:30

I got this....

To get slug use:

$attributes = $_product->get_attributes();
$pa_color = $attributes["pa_color"];

Thanks to all for helping me.

| improve this answer | |
  • This returns a WC_Product_Attribute. So the correct answer to this question is to write (after your code): $pa_color->get_slugs() And if you want the first slug - as you normally would: $pa_color->get_slugs()[0]. – Peter Højlund Andersen May 8 at 13:04

Use next:

| improve this answer | |
  • 1
    Could you elaborate a bit why this is a solution? You should post answers here and not only solutions :) – Krzysiek Dróżdż Mar 12 '19 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.