/** * Use it to avoid repeated get_child calls for the same variation. * * @param int $variation_id * @param WC_Product_Variable $product * @return WC_Product_Variation */ function get_variation($variation_id, $product) { if (isset($this->variations_cache[$variation_id])) { return $this->variations_cache[$variation_id]; } $variation = $product->get_child($variation_id, array('parent_id' => $product->id, 'parent' => $product)); $this->variations_cache[$variation_id] = $variation; return $variation; }
/** * Get all variations of a given WC_Product_Variable. * * @param WC_Product_Variable $wc_variable_product * @return array Array of WC_Product_Variation objects. */ public static function get_wc_product_variations(WC_Product_Variable $wc_variable_product) { $variations = array(); foreach ($wc_variable_product->get_children() as $child_id) { $variation = $wc_variable_product->get_child($child_id); if (empty($variation->variation_id)) { continue; } $variations[] = $variation; } return $variations; }