get_children() public method

Return a products child ids.
public get_children ( string $context = 'view' ) : array
$context string
return array Children ids
Exemplo n.º 1
2
 /**
  * Test variable and variations.
  *
  * @since 2.7.0
  */
 function test_variables_and_variations()
 {
     $product = new WC_Product_Variable();
     $product->set_name('Variable Product');
     $attribute = new WC_Product_Attribute();
     $attribute->set_id(0);
     $attribute->set_name('pa_color');
     $attribute->set_options(explode(WC_DELIMITER, 'green | red'));
     $attribute->set_visible(false);
     $attribute->set_variation(true);
     $product->set_attributes(array($attribute));
     $product->save();
     $this->assertEquals('Variable Product', $product->get_name());
     $variation = new WC_Product_Variation();
     $variation->set_name('Variation #1 of Dummy Variable CRUD Product');
     $variation->set_parent_id($product->get_id());
     $variation->set_regular_price(10);
     $variation->set_sku('CRUD DUMMY SKU VARIABLE GREEN');
     $variation->set_manage_stock('no');
     $variation->set_downloadable('no');
     $variation->set_virtual('no');
     $variation->set_stock_status('instock');
     $variation->set_attributes(array('pa_color' => 'green'));
     $variation->save();
     $this->assertEquals('Variation #1 of Dummy Variable CRUD Product', $variation->get_name());
     $this->assertEquals('CRUD DUMMY SKU VARIABLE GREEN', $variation->get_sku());
     $this->assertEquals(10, $variation->get_price());
     $product = new WC_Product_Variable($product->get_id());
     $children = $product->get_children();
     $this->assertEquals($variation->get_id(), $children[0]);
     $expected_attributes = array('pa_color' => array('green'));
     $this->assertEquals($expected_attributes, $product->get_variation_attributes());
     $variation_2 = new WC_Product_Variation();
     $variation_2->set_name('Variation #2 of Dummy Variable CRUD Product');
     $variation_2->set_parent_id($product->get_id());
     $variation_2->set_regular_price(10);
     $variation_2->set_sku('CRUD DUMMY SKU VARIABLE RED');
     $variation_2->set_manage_stock('no');
     $variation_2->set_downloadable('no');
     $variation_2->set_virtual('no');
     $variation_2->set_stock_status('instock');
     $variation_2->set_attributes(array('pa_color' => 'red'));
     $variation_2->save();
     $this->assertEquals('Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name());
     $this->assertEquals('CRUD DUMMY SKU VARIABLE RED', $variation_2->get_sku());
     $this->assertEquals(10, $variation_2->get_price());
     $product = new WC_Product_Variable($product->get_id());
     $children = $product->get_children();
     $this->assertEquals($variation_2->get_id(), $children[1]);
     $this->assertEquals(2, count($children));
     $expected_attributes = array('pa_color' => array('green', 'red'));
     $this->assertEquals($expected_attributes, $product->get_variation_attributes());
     $variation_2->set_name('UPDATED - Variation #2 of Dummy Variable CRUD Product');
     $variation_2->set_regular_price(15);
     $variation_2->set_sale_price(9.99);
     $variation_2->set_date_on_sale_to('32532537600');
     $variation_2->save();
     $product = new WC_Product_Variable($product->get_id());
     $expected_prices['price'][$children[0]] = 10.0;
     $expected_prices['price'][$children[1]] = 9.99;
     $expected_prices['regular_price'][$children[0]] = 10.0;
     $expected_prices['regular_price'][$children[1]] = 15.0;
     $expected_prices['sale_price'][$children[0]] = 10.0;
     $expected_prices['sale_price'][$children[1]] = 9.99;
     $this->assertEquals($expected_prices, $product->get_variation_prices());
     $this->assertEquals('UPDATED - Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name());
     $product->set_name('Renamed Variable Product');
     $product->save();
     $this->assertEquals('Renamed Variable Product', $product->get_name());
     $product->delete();
 }
 /**
  * Sync variable product prices with the children lowest/highest price per
  * unit.
  *
  * Code based on WC_Product_Variable version 2.0.0
  * @see WC_Product_Variable::variable_product_sync()
  * @see WC_Price_Calculator_Product::variable_product_unsync()
  *
  * @since 3.0
  * @param WC_Product_Variable $product the variable product
  * @param WC_Price_Calculator_Settings $settings the calculator settings
  */
 public static function variable_product_sync($product, $settings)
 {
     // save the original values so we can restore the product
     $product->wcmpc_min_variation_price = $product->min_variation_price;
     $product->wcmpc_min_variation_regular_price = $product->min_variation_regular_price;
     $product->wcmpc_min_variation_sale_price = $product->min_variation_sale_price;
     $product->wcmpc_max_variation_price = $product->max_variation_price;
     $product->wcmpc_max_variation_regular_price = $product->max_variation_regular_price;
     $product->wcmpc_max_variation_sale_price = $product->max_variation_sale_price;
     $product->wcmpc_price = $product->price;
     $product->min_variation_price = $product->min_variation_regular_price = $product->min_variation_sale_price = $product->max_variation_price = $product->max_variation_regular_price = $product->max_variation_sale_price = '';
     foreach ($product->get_children() as $variation_product_id) {
         $variation_product = apply_filters('wc_measurement_price_calculator_variable_product_sync', SV_WC_Plugin_Compatibility::wc_get_product($variation_product_id), $product);
         $child_price = $variation_product->price;
         $child_regular_price = $variation_product->regular_price;
         $child_sale_price = $variation_product->sale_price;
         // get the product measurement
         $measurement = self::get_product_measurement($variation_product, $settings);
         $measurement->set_unit($settings->get_pricing_unit());
         if ('' === $child_price && '' === $child_regular_price || !$measurement->get_value()) {
             continue;
         }
         // convert to price per unit
         if ('' !== $child_price) {
             $child_price /= $measurement->get_value();
         }
         // Regular prices
         if ($child_regular_price !== '') {
             // convert to price per unit
             $child_regular_price /= $measurement->get_value();
             if (!is_numeric($product->min_variation_regular_price) || $child_regular_price < $product->min_variation_regular_price) {
                 $product->min_variation_regular_price = $child_regular_price;
             }
             if (!is_numeric($product->max_variation_regular_price) || $child_regular_price > $product->max_variation_regular_price) {
                 $product->max_variation_regular_price = $child_regular_price;
             }
         }
         // Sale prices
         if ($child_sale_price !== '') {
             // convert to price per unit
             $child_sale_price /= $measurement->get_value();
             if ($child_price == $child_sale_price) {
                 if (!is_numeric($product->min_variation_sale_price) || $child_sale_price < $product->min_variation_sale_price) {
                     $product->min_variation_sale_price = $child_sale_price;
                 }
                 if (!is_numeric($product->max_variation_sale_price) || $child_sale_price > $product->max_variation_sale_price) {
                     $product->max_variation_sale_price = $child_sale_price;
                 }
             }
         }
         // Actual prices
         if ($child_price !== '') {
             if ($child_price > $product->max_variation_price) {
                 $product->max_variation_price = $child_price;
             }
             if ($product->min_variation_price === '' || $child_price < $product->min_variation_price) {
                 $product->min_variation_price = $child_price;
             }
         }
     }
     // as seen in WC_Product_Variable::get_price_html()
     $product->price = $product->min_variation_price;
 }
 public function get_product_variations($prod_id)
 {
     // verify we are in woocommerce product
     if (function_exists('get_product')) {
         $product = new WC_Product_Variable($prod_id);
         // WC_Product
         if (isset($product->id) && (int) $product->id > 0) {
             return $product->get_children();
         }
     }
     return array();
 }
 /**
  * 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;
 }