4

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

5

I got this....

To get slug use:

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

Thanks to all for helping me.

1
  • 3
    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]. May 8 '20 at 13:04
3
$att=$_product->get_attribute('pa_color');

$values = wc_get_product_terms( $product->id, 'pa_color', array( 'fields' => 'all' ) );
foreach($values as $val){
  echo "<pre>";
  var_dump($val->slug);
  echo "</pre>";
}
2
  • 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/… Jul 14 '18 at 18:30
-1

Use next:

$attribute->get_name()
1
  • 2
    Could you elaborate a bit why this is a solution? You should post answers here and not only solutions :) 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.