Merge pull request #15837 from thenbrent/revert_pr_14706
Revert pull request #14706 with changes to Abstract_WC_Order::get_item() to avoid critical fatal errors
This commit is contained in:
commit
429e4bfed1
|
@ -782,22 +782,14 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an order item object, based on it's ID. The item must belong to the current
|
* Get an order item object, based on it's type.
|
||||||
* order. If the item cannot be found or it doens't belong to current order
|
|
||||||
* FALSE will be returned.
|
|
||||||
*
|
*
|
||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
* @param int $item_id
|
* @param int $item_id
|
||||||
* @return WC_Order_Item
|
* @return WC_Order_Item
|
||||||
*/
|
*/
|
||||||
public function get_item( $item_id ) {
|
public function get_item( $item_id ) {
|
||||||
$type = $this->data_store->get_order_item_type( $this, $item_id );
|
return WC_Order_Factory::get_order_item( $item_id );
|
||||||
if ( ! $type ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$items = $this->get_items( $type );
|
|
||||||
return ! empty( $items[ $item_id ] ) ? $items[ $item_id ] : false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -358,22 +358,4 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||||
public function update_payment_token_ids( $order, $token_ids ) {
|
public function update_payment_token_ids( $order, $token_ids ) {
|
||||||
update_post_meta( $order->get_id(), '_payment_tokens', $token_ids );
|
update_post_meta( $order->get_id(), '_payment_tokens', $token_ids );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the order type of a given item which belongs to WC_Order
|
|
||||||
*
|
|
||||||
* @param WC_Order $order Order Object
|
|
||||||
* @param int $order_id
|
|
||||||
*
|
|
||||||
* @return string Order Item type
|
|
||||||
*/
|
|
||||||
public function get_order_item_type( WC_Order $order, $order_item_id ) {
|
|
||||||
global $wpdb;
|
|
||||||
$query = $wpdb->prepare(
|
|
||||||
"SELECT DISTINCT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d and order_item_id = %d",
|
|
||||||
$order->get_id(),
|
|
||||||
$order_item_id
|
|
||||||
);
|
|
||||||
return $wpdb->get_var( $query );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,10 +479,10 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
|
||||||
'product' => WC_Helper_Product::create_simple_product(),
|
'product' => WC_Helper_Product::create_simple_product(),
|
||||||
'quantity' => 4,
|
'quantity' => 4,
|
||||||
) );
|
) );
|
||||||
$object->add_item( $item );
|
$item->save();
|
||||||
|
$object->add_item( $item->get_id() );
|
||||||
$object->save();
|
$object->save();
|
||||||
$this->assertTrue( $object->get_item( $item->get_id() ) instanceOf WC_Order_Item_Product );
|
$this->assertTrue( $object->get_item( $item->get_id() ) instanceOf WC_Order_Item_Product );
|
||||||
$this->assertEquals( spl_object_hash( $item ), spl_object_hash( $object->get_item( $item->get_id() ) ) );
|
|
||||||
|
|
||||||
$object = new WC_Order();
|
$object = new WC_Order();
|
||||||
$item = new WC_Order_Item_Coupon();
|
$item = new WC_Order_Item_Coupon();
|
||||||
|
@ -495,51 +495,6 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
|
||||||
$object->add_item( $item );
|
$object->add_item( $item );
|
||||||
$object->save();
|
$object->save();
|
||||||
$this->assertTrue( $object->get_item( $item_id ) instanceOf WC_Order_Item_Coupon );
|
$this->assertTrue( $object->get_item( $item_id ) instanceOf WC_Order_Item_Coupon );
|
||||||
|
|
||||||
$object = new WC_Order( $object->get_id() );
|
|
||||||
$this->assertTrue( $object->get_item( $item_id ) instanceOf WC_Order_Item_Coupon );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make sure that items returned by get_item is tied to the order,
|
|
||||||
* and that is saved when the order is saved.
|
|
||||||
*/
|
|
||||||
public function test_get_item_object_is_updated_with_order_save() {
|
|
||||||
$object = new WC_Order();
|
|
||||||
$item = new WC_Order_Item_Product();
|
|
||||||
$item->set_props( array(
|
|
||||||
'product' => WC_Helper_Product::create_simple_product(),
|
|
||||||
'quantity' => 4,
|
|
||||||
) );
|
|
||||||
$object->add_item( $item );
|
|
||||||
$object->save();
|
|
||||||
|
|
||||||
$object = new WC_Order( $object->get_id() );
|
|
||||||
$item = $object->get_item( $item->get_id() );
|
|
||||||
$item->set_quantity( 6 );
|
|
||||||
$object->save();
|
|
||||||
|
|
||||||
$object = new WC_Order( $object->get_id() );
|
|
||||||
$this->assertEquals(6, $object->get_item( $item->get_id() )->get_quantity() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Makes sure that get_item only returns items related to the order
|
|
||||||
*/
|
|
||||||
public function test_get_item_from_another_order() {
|
|
||||||
$object = new WC_Order();
|
|
||||||
$item = new WC_Order_Item_Product();
|
|
||||||
$item->set_props( array(
|
|
||||||
'product' => WC_Helper_Product::create_simple_product(),
|
|
||||||
'quantity' => 4,
|
|
||||||
'order_id' => 999,
|
|
||||||
) );
|
|
||||||
$item->save();
|
|
||||||
$this->assertFalse( $object->get_item( $item->get_id() ) );
|
|
||||||
|
|
||||||
$object->save();
|
|
||||||
$this->assertFalse( $object->get_item( $item->get_id() ) );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue