Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Woocommerce Variation Parent Attribute

I have a Woocommerce variable product. I need to get the attribute (pa_brand) of the parent of that variation? Here is the code I have which is not returning anything. How can I get a product variation from a product id?

global $product;
global $post;
$post_id = $post->ID;
$parent_id = wp_get_post_parent_id( $post_id );
$brand = $product->get_attribute( 'pa_brand' );
like image 573
Rob Gelhausen Avatar asked Mar 24 '26 07:03

Rob Gelhausen


1 Answers

@Rob Gelhausen already answered it as a comment for the question. To get more notice, I'm making it as an answer.

To get Main product ID / Parent product from variation product id, we can use below code.

$variation = wc_get_product($variation_id);
$product = wc_get_product( $variation->get_parent_id() );

To get attribute, we can use below code.

$brand = $product->get_attribute( 'pa_brand' );
like image 126
Sarathlal N Avatar answered Mar 26 '26 13:03

Sarathlal N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!