Fix order item meta saving

This commit is contained in:
Mike Jolley 2016-11-23 15:10:18 +00:00
parent d846ca6311
commit b1007c9d62
4 changed files with 17 additions and 20 deletions

View File

@ -47,9 +47,6 @@ class WC_Order_Item_Product extends WC_Order_Item {
* @throws WC_Data_Exception * @throws WC_Data_Exception
*/ */
public function set_quantity( $value ) { 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 ) ); $this->set_prop( 'quantity', wc_stock_amount( $value ) );
} }

View File

@ -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(); 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 | Meta Data Handling

View File

@ -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(), 'order_id' => $item->get_order_id(),
) ); ) );
$item->set_id( $wpdb->insert_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() ); 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(), 'order_id' => $item->get_order_id(),
), array( 'order_item_id' => $item->get_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() ); 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->read_meta_data();
$item->set_object_read( true ); $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 ) {}
} }

View File

@ -534,7 +534,7 @@ function wc_create_refund( $args = array() ) {
} }
if ( is_callable( array( $refunded_item, 'set_quantity' ) ) ) { if ( is_callable( array( $refunded_item, 'set_quantity' ) ) ) {
$refunded_item->set_quantity( $qty ); $refunded_item->set_quantity( $qty * -1 );
} }
$refund->add_item( $refunded_item ); $refund->add_item( $refunded_item );