create_external_product() public static method

Create external product.
Since: 2.7.0
public static create_external_product ( ) : WC_Product_External
return WC_Product_External
Esempio n. 1
0
 /**
  * Test getting product type.
  *
  * @since 2.7.0
  */
 function test_get_product_type()
 {
     $simple = WC_Helper_Product::create_simple_product();
     $external = WC_Helper_Product::create_external_product();
     $grouped = WC_Helper_Product::create_grouped_product();
     $variable = WC_Helper_Product::create_variation_product();
     $children = $variable->get_children();
     $child_id = $children[0];
     $this->assertEquals('simple', WC()->product_factory->get_product_type($simple->get_id()));
     $this->assertEquals('external', WC()->product_factory->get_product_type($external->get_id()));
     $this->assertEquals('grouped', WC()->product_factory->get_product_type($grouped->get_id()));
     $this->assertEquals('variable', WC()->product_factory->get_product_type($variable->get_id()));
     $this->assertEquals('variation', WC()->product_factory->get_product_type($child_id));
     $simple->delete(true);
     $external->delete(true);
     $grouped->delete(true);
     $variable->delete(true);
 }
Esempio n. 2
0
 /**
  * Test updating an external product. Make sure both our external
  * product data and the main product data are written to and present.
  *
  * @since 2.7.0
  */
 function test_external_product_update()
 {
     $product = WC_Helper_Product::create_external_product();
     $this->assertEquals('Buy external product', $product->get_button_text());
     $this->assertEquals('10', $product->get_regular_price());
     $product->set_button_text('Buy my external product');
     $product->set_regular_price(15);
     $product->save();
     // Reread from database
     $product = new WC_Product_External($product->get_id());
     $this->assertEquals('Buy my external product', $product->get_button_text());
     $this->assertEquals('15', $product->get_regular_price());
 }
Esempio n. 3
0
 /**
  * Test editing a single product. Tests multiple product types.
  *
  * @since 2.7.0
  */
 public function test_update_product()
 {
     wp_set_current_user($this->user);
     // test simple products
     $product = WC_Helper_Product::create_simple_product();
     $response = $this->server->dispatch(new WP_REST_Request('GET', '/wc/v1/products/' . $product->get_id()));
     $data = $response->get_data();
     $this->assertEquals('DUMMY SKU', $data['sku']);
     $this->assertEquals(10, $data['regular_price']);
     $this->assertEmpty($data['sale_price']);
     $request = new WP_REST_Request('PUT', '/wc/v1/products/' . $product->get_id());
     $request->set_body_params(array('sku' => 'FIXED-SKU', 'sale_price' => '8', 'description' => 'Testing', 'images' => array(array('position' => 0, 'src' => 'https://cldup.com/Dr1Bczxq4q.png', 'alt' => 'test upload image'))));
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertContains('Testing', $data['description']);
     $this->assertEquals('8', $data['price']);
     $this->assertEquals('8', $data['sale_price']);
     $this->assertEquals('10', $data['regular_price']);
     $this->assertEquals('FIXED-SKU', $data['sku']);
     $this->assertContains('Dr1Bczxq4q', $data['images'][0]['src']);
     $this->assertContains('test upload image', $data['images'][0]['alt']);
     // test variable product (varations are tested in product-variations.php)
     $product = WC_Helper_Product::create_variation_product();
     $response = $this->server->dispatch(new WP_REST_Request('GET', '/wc/v1/products/' . $product->get_id()));
     $data = $response->get_data();
     $this->assertEquals(array('small'), $data['attributes'][0]['options']);
     $request = new WP_REST_Request('PUT', '/wc/v1/products/' . $product->get_id());
     $request->set_body_params(array('attributes' => array(array('id' => 0, 'name' => 'pa_color', 'options' => array('red', 'yellow'), 'visible' => false, 'variation' => 1), array('name' => 'pa_size', 'options' => array('small'), 'visible' => false, 'variation' => 1))));
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertEquals(array('small'), $data['attributes'][0]['options']);
     $this->assertEquals(array('red', 'yellow'), $data['attributes'][1]['options']);
     $product->delete(true);
     // test external product
     $product = WC_Helper_Product::create_external_product();
     $response = $this->server->dispatch(new WP_REST_Request('GET', '/wc/v1/products/' . $product->get_id()));
     $data = $response->get_data();
     $this->assertEquals('Buy external product', $data['button_text']);
     $this->assertEquals('http://woocommerce.com', $data['external_url']);
     $request = new WP_REST_Request('PUT', '/wc/v1/products/' . $product->get_id());
     $request->set_body_params(array('button_text' => 'Test API Update', 'external_url' => 'http://automattic.com'));
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertEquals('Test API Update', $data['button_text']);
     $this->assertEquals('http://automattic.com', $data['external_url']);
     $product->delete(true);
 }