From b1007c9d62e78371919108890fb775e27fafd476 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 23 Nov 2016 15:10:18 +0000 Subject: [PATCH] Fix order item meta saving --- includes/class-wc-order-item-product.php | 3 --- includes/class-wc-order-item.php | 16 ---------------- .../abstract-wc-order-item-type-data-store.php | 16 ++++++++++++++++ includes/wc-order-functions.php | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/includes/class-wc-order-item-product.php b/includes/class-wc-order-item-product.php index 93a0d2c81a0..bb67dbab945 100644 --- a/includes/class-wc-order-item-product.php +++ b/includes/class-wc-order-item-product.php @@ -47,9 +47,6 @@ class WC_Order_Item_Product extends WC_Order_Item { * @throws WC_Data_Exception */ public function set_quantity( $value ) { - if ( 0 > $value ) { - $this->error( 'order_item_product_invalid_quantity', __( 'Quantity must be positive', 'woocommerce' ) ); - } $this->set_prop( 'quantity', wc_stock_amount( $value ) ); } diff --git a/includes/class-wc-order-item.php b/includes/class-wc-order-item.php index 54faada60fd..ef8290aa0ff 100644 --- a/includes/class-wc-order-item.php +++ b/includes/class-wc-order-item.php @@ -174,22 +174,6 @@ class WC_Order_Item extends WC_Data implements ArrayAccess { return is_array( $type ) ? in_array( $this->get_type(), $type ) : $type === $this->get_type(); } - /** - * Save properties specific to this order item. - * @return int Item ID - */ - public function save() { - parent::save(); - - if ( $this->data_store ) { - $this->data_store->save_item_data( $this ); - } - - $this->apply_changes(); - - return $this->get_id(); - } - /* |-------------------------------------------------------------------------- | Meta Data Handling diff --git a/includes/data-stores/abstract-wc-order-item-type-data-store.php b/includes/data-stores/abstract-wc-order-item-type-data-store.php index f27029afb80..1a0667661a9 100644 --- a/includes/data-stores/abstract-wc-order-item-type-data-store.php +++ b/includes/data-stores/abstract-wc-order-item-type-data-store.php @@ -26,6 +26,9 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i 'order_id' => $item->get_order_id(), ) ); $item->set_id( $wpdb->insert_id ); + $this->save_item_data( $item ); + $item->save_meta_data(); + $item->apply_changes(); do_action( 'woocommerce_new_order_item', $item->get_id(), $item, $item->get_order_id() ); } @@ -45,6 +48,10 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i 'order_id' => $item->get_order_id(), ), array( 'order_item_id' => $item->get_id() ) ); + $this->save_item_data( $item ); + $item->save_meta_data(); + $item->apply_changes(); + do_action( 'woocommerce_update_order_item', $item->get_id(), $item, $item->get_order_id() ); } @@ -90,4 +97,13 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i $item->read_meta_data(); $item->set_object_read( true ); } + + /** + * Saves an item's data to the database / item meta. + * Ran after both create and update, so $item->get_id() will be set. + * + * @since 2.7.0 + * @param WC_Order_Item $item + */ + public function save_item_data( &$item ) {} } diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index b08bdf02b25..84a0f8309b5 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -534,7 +534,7 @@ function wc_create_refund( $args = array() ) { } if ( is_callable( array( $refunded_item, 'set_quantity' ) ) ) { - $refunded_item->set_quantity( $qty ); + $refunded_item->set_quantity( $qty * -1 ); } $refund->add_item( $refunded_item );