Create a dummy variation product.
public static create_variation_product ( ) : WC_Product_Variable | ||
Résultat | WC_Product_Variable |
/** * Test test_wc_api_order_get_variation_id_returns_correct_id. * * @since 2.4 */ public function test_wc_api_order_get_variation_id_returns_correct_id() { parent::setUp(); $product = WC_Helper_Product::create_variation_product(); $orders_api = WC()->api->WC_API_Orders; $variation_id = $orders_api->get_variation_id($product, array('size' => 'small')); $this->assertSame($product->id + 1, $variation_id); $variation_id = $orders_api->get_variation_id($product, array('size' => 'large')); $this->assertSame($product->id + 2, $variation_id); }
/** * Test add to cart variable product. * * @since 2.3 */ public function test_add_to_cart_variable() { $product = WC_Helper_Product::create_variation_product(); $variations = $product->get_available_variations(); $variation = array_shift($variations); // Add the product to the cart. Methods returns boolean on failure, string on success. $this->assertNotFalse(WC()->cart->add_to_cart($product->id, 1, $variation['variation_id'], array('Size' => ucfirst($variation['attributes']['attribute_pa_size'])))); // Check if the item is in the cart $this->assertEquals(1, WC()->cart->get_cart_contents_count()); // Clean up the cart WC()->cart->empty_cart(); // @todo clean up the variable product }
/** * 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); }
/** * Test reading a variable product. * * @since 2.7.0 */ public function test_variable_read() { $product = WC_Helper_Product::create_variation_product(); $children = $product->get_children(); // Test sale prices too update_post_meta($children[0], '_price', '8'); update_post_meta($children[0], '_sale_price', '8'); delete_transient('wc_var_prices_' . $product->get_id()); $product = new WC_Product_Variable($product->get_id()); $this->assertEquals(2, count($product->get_children())); $expected_prices['price'][$children[0]] = 8.0; $expected_prices['price'][$children[1]] = 15.0; $expected_prices['regular_price'][$children[0]] = 10.0; $expected_prices['regular_price'][$children[1]] = 15.0; $expected_prices['sale_price'][$children[0]] = 8.0; $expected_prices['sale_price'][$children[1]] = 15.0; $this->assertEquals($expected_prices, $product->get_variation_prices()); $expected_attributes = array('pa_size' => array('small', 'large')); $this->assertEquals($expected_attributes, $product->get_variation_attributes()); $product->delete(); }
/** * 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); }
/** * Test batch managing product variations. */ public function test_product_variations_batch() { wp_set_current_user($this->user); $product = WC_Helper_Product::create_variation_product(); $children = $product->get_children(); $request = new WP_REST_Request('POST', '/wc/v1/products/' . $product->get_id() . '/variations/batch'); $request->set_body_params(array('update' => array(array('id' => $children[0], 'description' => 'Updated description.', 'image' => array(array('position' => 0, 'src' => 'https://cldup.com/Dr1Bczxq4q.png', 'alt' => 'test upload image')))), 'delete' => array(array('id' => $children[1])), 'create' => array(array('sku' => 'DUMMY SKU VARIABLE MEDIUM', 'regular_price' => '12', 'description' => 'A medium size.', 'attributes' => array(array('name' => 'pa_size', 'option' => 'medium')))))); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertContains('Updated description.', $data['update'][0]['description']); $this->assertEquals('DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku']); $this->assertEquals('medium', $data['create'][0]['attributes'][0]['option']); $this->assertEquals($children[1], $data['delete'][0]['id']); $request = new WP_REST_Request('GET', '/wc/v1/products/' . $product->get_id() . '/variations'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals(2, count($data)); $product->delete(true); }
/** * Tests wc_get_products(). * * @since 2.7.0 */ public function test_wc_get_products() { $test_cat_1 = wp_insert_term('Testing 1', 'product_cat'); $test_tag_1 = wp_insert_term('Tag 1', 'product_tag'); $test_tag_2 = wp_insert_term('Tag 2', 'product_tag'); $term_cat_1 = get_term_by('id', $test_cat_1['term_id'], 'product_cat'); $term_tag_1 = get_term_by('id', $test_tag_1['term_id'], 'product_tag'); $term_tag_2 = get_term_by('id', $test_tag_2['term_id'], 'product_tag'); $product = WC_Helper_Product::create_simple_product(); $product->set_tag_ids(array($test_tag_1['term_id'])); $product->set_category_ids(array($test_cat_1['term_id'])); $product->set_sku('GET TEST SKU SIMPLE'); $product->save(); $product_2 = WC_Helper_Product::create_simple_product(); $product_2->set_category_ids(array($test_cat_1['term_id'])); $product_2->save(); $external = WC_Helper_Product::create_simple_product(); $external->set_category_ids(array($test_cat_1['term_id'])); $external->set_sku('GET TEST SKU EXTERNAL'); $external->save(); $external_2 = WC_Helper_Product::create_simple_product(); $external_2->set_tag_ids(array($test_tag_2['term_id'])); $external_2->save(); $grouped = WC_Helper_Product::create_grouped_product(); $variation = WC_Helper_Product::create_variation_product(); $variation->set_tag_ids(array($test_tag_1['term_id'])); $variation->save(); $draft = WC_Helper_Product::create_simple_product(); $draft->set_status('draft'); $draft->save(); $this->assertEquals(9, count(wc_get_products(array('return' => 'ids')))); // test status $products = wc_get_products(array('return' => 'ids', 'status' => 'draft')); $this->assertEquals(array($draft->get_id()), $products); // test type $products = wc_get_products(array('return' => 'ids', 'type' => 'variation')); $this->assertEquals(2, count($products)); // test parent $products = wc_get_products(array('return' => 'ids', 'type' => 'variation', 'parent' => $variation->get_id())); $this->assertEquals(2, count($products)); // test skus $products = wc_get_products(array('return' => 'ids', 'sku' => 'GET TEST SKU')); $this->assertEquals(2, count($products)); $this->assertContains($product->get_id(), $products); $this->assertContains($external->get_id(), $products); // test categories $products = wc_get_products(array('return' => 'ids', 'category' => array($term_cat_1->slug))); $this->assertEquals(3, count($products)); // test tags $products = wc_get_products(array('return' => 'ids', 'tag' => array($term_tag_1->slug))); $this->assertEquals(2, count($products)); $products = wc_get_products(array('return' => 'ids', 'tag' => array($term_tag_2->slug))); $this->assertEquals(1, count($products)); $products = wc_get_products(array('return' => 'ids', 'tag' => array($term_tag_1->slug, $term_tag_2->slug))); $this->assertEquals(3, count($products)); // test limit $products = wc_get_products(array('return' => 'ids', 'limit' => 5)); $this->assertEquals(5, count($products)); // test offset $products = wc_get_products(array('return' => 'ids', 'limit' => 2)); $products_offset = wc_get_products(array('return' => 'ids', 'limit' => 2, 'offset' => 2)); $this->assertEquals(2, count($products)); $this->assertEquals(2, count($products_offset)); $this->assertNotEquals($products, $products_offset); // test page $products_page_1 = wc_get_products(array('return' => 'ids', 'limit' => 2)); $products_page_2 = wc_get_products(array('return' => 'ids', 'limit' => 2, 'page' => 2)); $this->assertEquals(2, count($products_page_1)); $this->assertEquals(2, count($products_page_2)); $this->assertNotEquals($products_page_1, $products_page_2); // test exclude $products = wc_get_products(array('return' => 'ids', 'limit' => 200, 'exclude' => array($product->get_id()))); $this->assertNotContains($product->get_id(), $products); $variation->delete(true); }