Put back add_product for convenience. Fix unit tests.

This commit is contained in:
Mike Jolley 2016-08-22 18:00:52 +01:00
parent 3d696266d2
commit a8e47992a6
5 changed files with 72 additions and 87 deletions

View File

@ -16,55 +16,6 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
abstract class WC_Abstract_Legacy_Order extends WC_Data {
/**
* Add a product line item to the order.
* @param \WC_Product $product
* @param int $qty
* @param array $args
* @return int order item ID
*/
public function add_product( $product, $qty = 1, $args = array() ) {
_deprecated_function( 'WC_Order::add_product', '2.7', 'Create new WC_Order_Item_Product object and add to order with WC_Order::add_item()' );
$args = wp_parse_args( $args, array(
'quantity' => $qty,
'name' => $product ? $product->get_title() : '',
'tax_class' => $product ? $product->get_tax_class() : '',
'product_id' => $product ? $product->get_id() : '',
'variation_id' => $product && isset( $product->variation_id ) ? $product->variation_id : 0,
'variation' => $product && isset( $product->variation_id ) ? $product->get_variation_attributes() : array(),
'subtotal' => $product ? $product->get_price_excluding_tax( $qty ) : '',
'total' => $product ? $product->get_price_excluding_tax( $qty ) : '',
'subtotal_tax' => 0,
'total_tax' => 0,
'taxes' => array(
'subtotal' => array(),
'total' => array(),
),
) );
// BW compatibility with old args
if ( isset( $args['totals'] ) ) {
foreach ( $args['totals'] as $key => $value ) {
if ( 'tax' === $key ) {
$args['total_tax'] = $value;
} elseif ( 'tax_data' === $key ) {
$args['taxes'] = $value;
} else {
$args[ $key ] = $value;
}
}
}
$item = new WC_Order_Item_Product( $args );
$item->set_backorder_meta();
$item->set_order_id( $this->get_id() );
$item->save();
$this->add_item( $item );
wc_do_deprecated_action( 'woocommerce_order_add_product', array( $this->get_id(), $item->get_id(), $product, $qty, $args ), '2.7', 'Use woocommerce_new_order_item action instead.' );
return $item->get_id();
}
/**
* Add coupon code to the order.
* @param string $code

View File

@ -957,10 +957,60 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
if ( $item->get_id() ) {
$this->_items[ $items_key ][ $item->get_id() ] = $item;
} else {
$this->_items[ $items_key ][ 'new:' . md5( json_encode( $item ) ) ] = $item;
$this->_items[ $items_key ][ 'new:' . sizeof( $this->_items[ $items_key ] ) ] = $item;
}
}
/**
* Add a product line item to the order. This is the only line item type with
* it's own method because it saves looking up order amounts (costs are added up for you).
* @param \WC_Product $product
* @param int $qty
* @param array $args
* @return int order item ID
*/
public function add_product( $product, $qty = 1, $args = array() ) {
if ( $product ) {
$default_args = array(
'name' => $product->get_title(),
'tax_class' => $product->get_tax_class(),
'product_id' => $product->get_id(),
'variation_id' => isset( $product->variation_id ) ? $product->variation_id : 0,
'variation' => isset( $product->variation_id ) ? $product->get_variation_attributes() : array(),
'subtotal' => $product->get_price_excluding_tax( $qty ),
'total' => $product->get_price_excluding_tax( $qty ),
'quantity' => $qty,
);
} else {
$default_args = array(
'quantity' => $qty,
);
}
$args = wp_parse_args( $args, $default_args );
// BW compatibility with old args
if ( isset( $args['totals'] ) ) {
foreach ( $args['totals'] as $key => $value ) {
if ( 'tax' === $key ) {
$args['total_tax'] = $value;
} elseif ( 'tax_data' === $key ) {
$args['taxes'] = $value;
} else {
$args[ $key ] = $value;
}
}
}
$item = new WC_Order_Item_Product( $args );
$item->set_backorder_meta();
$item->set_order_id( $this->get_id() );
$item->save();
$this->add_item( $item );
wc_do_deprecated_action( 'woocommerce_order_add_product', array( $this->get_id(), $item->get_id(), $product, $qty, $args ), '2.7', 'Use woocommerce_new_order_item action instead.' );
return $item->get_id();
}
/*
|--------------------------------------------------------------------------
| Payment Token Handling

View File

@ -51,11 +51,7 @@ class WC_Helper_Order {
$order = wc_create_order( $order_data );
// Add order products
$item = new WC_Order_Item_Product( array(
'product' => $product,
'quantity' => 4,
) );
$order->add_item( $item );
$order->add_product( $product, 4 );
// Set billing address
$order->set_billing_first_name( 'Jeroen' );

View File

@ -19,7 +19,7 @@ class WC_Tests_CRUD_Refunds extends WC_Unit_Test_Case {
*/
function test_get_refund_amount() {
$object = new WC_Order_Refund();
$object->set_refund_amount( 20 );
$object->set_amount( 20 );
$this->assertEquals( '20.00', $object->get_amount() );
}
@ -28,7 +28,7 @@ class WC_Tests_CRUD_Refunds extends WC_Unit_Test_Case {
*/
function test_get_refund_reason() {
$object = new WC_Order_Refund();
$object->set_refund_reason( 'Customer is an idiot' );
$object->set_reason( 'Customer is an idiot' );
$this->assertEquals( 'Customer is an idiot', $object->get_reason() );
}

View File

@ -415,8 +415,8 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
* Test: get_item
*/
function test_get_item() {
$object = new WC_Order();
$item_id = new WC_Order_Item_Product( array(
$object = new WC_Order();
$item = new WC_Order_Item_Product( array(
'product' => WC_Helper_Product::create_simple_product(),
'quantity' => 4
) );
@ -471,15 +471,22 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
function test_calculate_shipping() {
$object = new WC_Order();
$rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', array(), 'flat_rate' );
$item = new WC_Order_Item_Shipping( array(
$item_1 = new WC_Order_Item_Shipping( array(
'method_title' => $rate->label,
'method_id' => $rate->id,
'total' => wc_format_decimal( $rate->cost ),
'taxes' => $rate->taxes,
'meta_data' => $rate->get_meta_data(),
) );
$object->add_item( $item );
$object->add_item( $item );
$item_2 = new WC_Order_Item_Shipping( array(
'method_title' => $rate->label,
'method_id' => $rate->id,
'total' => wc_format_decimal( $rate->cost ),
'taxes' => $rate->taxes,
'meta_data' => $rate->get_meta_data(),
) );
$object->add_item( $item_1 );
$object->add_item( $item_2 );
$object->save();
$object->calculate_shipping();
$this->assertEquals( 20, $object->get_shipping_total() );
@ -504,12 +511,8 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
);
WC_Tax::_insert_tax_rate( $tax_rate );
$object = new WC_Order();
$item_id = new WC_Order_Item_Product( array(
'product' => WC_Helper_Product::create_simple_product(),
'quantity' => 4
) );
$object->add_item( $item_id );
$object = new WC_Order();
$object->add_product( WC_Helper_Product::create_simple_product(), 4 );
$rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', array(), 'flat_rate' );
$item = new WC_Order_Item_Shipping( array(
@ -550,13 +553,8 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
WC_Tax::_insert_tax_rate( $tax_rate );
$object = new WC_Order();
$item_id = new WC_Order_Item_Product( array(
'product' => WC_Helper_Product::create_simple_product(),
'quantity' => 4
) );
$object->add_item( $item_id );
$object->add_product( WC_Helper_Product::create_simple_product(), 4 );
$object = new WC_Order();
$rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', array(), 'flat_rate' );
$item = new WC_Order_Item_Shipping( array(
'method_title' => $rate->label,
@ -625,23 +623,13 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
* Test: has_free_item
*/
function test_has_free_item() {
$object = new WC_Order();
$item_id = new WC_Order_Item_Product( array(
'product' => WC_Helper_Product::create_simple_product(),
'quantity' => 4
) );
$object->add_item( $item_id );
$object->save();
$object = new WC_Order();
$object->add_product( WC_Helper_Product::create_simple_product(), 4 );
$this->assertFalse( $object->has_free_item() );
$free_product = WC_Helper_Product::create_simple_product();
$free_product->set_price( 0 );
$item_id = new WC_Order_Item_Product( array(
'product' => $free_product,
'quantity' => 4
) );
$object->add_item( $item_id );
$object->save();
$object->add_product( $free_product, 4 );
$this->assertTrue( $object->has_free_item() );
}