Skip to content
Advertisement

Get weight value from WooCommerce variations of a variable product

I am working on a plugin that auto adjusts product prices. All of my products are variable products which has made this tougher than I can solve.

JavaScript

I have a check to make sure the product is variable. The only product Attribute is ‘Size’. Here I have 4-8 different Size Variations per product. Each of these have a weight value which seemed to be default implemented by Woocommerce. I am unable to get the weight though from each variation. Curious if I am calling get_weight() from the wrong place, or if there is a different method for this. get_weight() of course does not work so I wonder if getting attributes from variations is completely wrong?

Advertisement

Answer

Using WC_Variable_Product get_visible_children() (or get_children()) method, try:

JavaScript

or you can use WC_Variable_Product get_available_variations() as follows:

JavaScript

Both code snippet works.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement