/** * Test wc_get_coupon_code_by_id(). * * @since 2.7.0 */ public function test_wc_get_coupon_code_by_id() { // Create coupon. $code = 'testcoupon'; $coupon = WC_Helper_Coupon::create_coupon($code); $this->assertEquals($code, wc_get_coupon_code_by_id($coupon->get_id())); // Delete coupon. WC_Helper_Coupon::delete_coupon($coupon->get_id()); $this->assertEmpty(wc_get_coupon_code_by_id(0)); }
/** * Test percent product discount method. * * @since 2.3 */ public function test_percent_product_discount() { // Create product $product = WC_Helper_Product::create_simple_product(); update_post_meta($product->id, '_price', '10'); update_post_meta($product->id, '_regular_price', '10'); // Create coupon $coupon = WC_Helper_Coupon::create_coupon(); update_post_meta($coupon->get_id(), 'discount_type', 'percent_product'); update_post_meta($coupon->get_id(), 'coupon_amount', '5'); // Create a flat rate method WC_Helper_Shipping::create_simple_flat_rate(); // We need this to have the calculate_totals() method calculate totals if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } // Add fee WC_Helper_Fee::add_cart_fee(); // Add product to cart WC()->cart->add_to_cart($product->id, 1); // Add coupon WC()->cart->add_discount($coupon->get_code()); // Set the flat_rate shipping method WC()->session->set('chosen_shipping_methods', array('flat_rate')); WC()->cart->calculate_totals(); // Test if the cart total amount is equal 29.5 $this->assertEquals(29.5, WC()->cart->total); // Clearing WC notices wc_clear_notices(); // Clean up the cart WC()->cart->empty_cart(); // Remove coupons WC()->cart->remove_coupons(); // Remove fee WC_Helper_Fee::remove_cart_fee(); // Delete the flat rate method WC()->session->set('chosen_shipping_methods', array()); WC_Helper_Shipping::delete_simple_flat_rate(); // Delete coupon WC_Helper_Coupon::delete_coupon($coupon->get_id()); // Delete product WC_Helper_Product::delete_product($product->id); }
/** * Test wc_cart_totals_coupon_label(). * * @since 2.4 */ public function test_wc_cart_totals_coupon_label() { $coupon = WC_Helper_Coupon::create_coupon(); $this->expectOutputString(apply_filters('woocommerce_cart_totals_coupon_label', 'Coupon: ' . $coupon->code), wc_cart_totals_coupon_label($coupon)); WC_Helper_Coupon::delete_coupon($coupon->id); }
/** * Test cart coupons */ public function test_get_coupons() { // Create coupon $coupon = \WC_Helper_Coupon::create_coupon(); // Add coupon WC()->cart->add_discount($coupon->code); $this->assertEquals(count(WC()->cart->get_coupons()), 1); // Clean up the cart WC()->cart->empty_cart(); // Remove coupons WC()->cart->remove_coupons(); // Delete coupon \WC_Helper_Coupon::delete_coupon($coupon->id); }
/** * Test setting custom fields. * @since 2.7.0 */ public function test_set_custom_fields() { $coupon = WC_Helper_Coupon::create_coupon(); $coupon_id = $coupon->get_id(); $meta_value = time() . '-custom-value'; $coupon->add_meta_data('my-custom-field', $meta_value, true); $coupon->save(); $coupon->read($coupon_id); $this->assertEquals($meta_value, $coupon->get_meta('my-custom-field')); }
/** * Test batch operations on coupons. * @since 2.7.0 */ public function test_batch_coupon() { wp_set_current_user($this->user); $coupon_1 = WC_Helper_Coupon::create_coupon('dummycoupon-1'); $coupon_2 = WC_Helper_Coupon::create_coupon('dummycoupon-2'); $coupon_3 = WC_Helper_Coupon::create_coupon('dummycoupon-3'); $coupon_4 = WC_Helper_Coupon::create_coupon('dummycoupon-4'); $request = new WP_REST_Request('POST', '/wc/v1/coupons/batch'); $request->set_body_params(array('update' => array(array('id' => $coupon_1->get_id(), 'amount' => '5.15')), 'delete' => array($coupon_2->get_id(), $coupon_3->get_id()), 'create' => array(array('code' => 'new-coupon', 'amount' => '11.00')))); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals('5.15', $data['update'][0]['amount']); $this->assertEquals('11.00', $data['create'][0]['amount']); $this->assertEquals('new-coupon', $data['create'][0]['code']); $this->assertEquals($coupon_2->get_id(), $data['delete'][0]['id']); $this->assertEquals($coupon_3->get_id(), $data['delete'][1]['id']); $request = new WP_REST_Request('GET', '/wc/v1/coupons'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals(3, count($data)); }
/** * Test coupon increase, decrease, user usage count methods. * @since 2.7.0 */ function test_coupon_usage_magic_methods() { $coupon = WC_Helper_Coupon::create_coupon(); $user_id = 1; $this->assertEquals(0, $coupon->get_usage_count()); $this->assertEmpty($coupon->get_used_by()); $coupon->inc_usage_count('*****@*****.**'); $this->assertEquals(1, $coupon->get_usage_count()); $this->assertEquals(array('*****@*****.**'), $coupon->get_used_by()); $coupon->inc_usage_count($user_id); $coupon->inc_usage_count($user_id); $data_store = WC_Data_Store::load('coupon'); $this->assertEquals(2, $data_store->get_usage_by_user_id($coupon, $user_id)); $coupon->dcr_usage_count('*****@*****.**'); $coupon->dcr_usage_count($user_id); $this->assertEquals(1, $coupon->get_usage_count()); $this->assertEquals(array(1), $coupon->get_used_by()); }
/** * Test PUT /coupons/bulk. * @since 2.7.0 */ public function test_edit_coupon_bulk() { $coupon_1 = WC_Helper_Coupon::create_coupon('dummycoupon-1-' . time()); $test_coupon_data = $this->get_defaults(array('description' => rand_str())); $test_coupon_data['coupon']['id'] = $coupon_1->get_id(); $coupons = array('coupons' => array($test_coupon_data['coupon'])); $response = $this->endpoint->bulk($coupons); $this->assertNotWPError($response); $this->assertArrayHasKey('coupons', $response); $this->check_get_coupon_response($response['coupons'][0], new WC_Coupon($response['coupons'][0]['code'])); }