예제 #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();
 }