From 2b3ec357adb73a935064ada83aae0bb533e0f4f7 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 25 Apr 2017 14:12:36 +0300 Subject: [PATCH 1/4] fix-14302 --- includes/abstracts/abstract-wc-order.php | 7 +++---- includes/class-wc-checkout.php | 2 +- includes/class-wc-order-factory.php | 1 + 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 39af9816eee..168768766c3 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -798,9 +798,8 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { * @return string */ protected function get_items_key( $item ) { - if ( is_a( $item, 'WC_Order_Item_Product' ) ) { - return 'line_items'; - } elseif ( is_a( $item, 'WC_Order_Item_Fee' ) ) { + + if ( is_a( $item, 'WC_Order_Item_Fee' ) ) { return 'fee_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Shipping' ) ) { return 'shipping_lines'; @@ -809,7 +808,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { } elseif ( is_a( $item, 'WC_Order_Item_Coupon' ) ) { return 'coupon_lines'; } else { - return ''; + return 'line_items'; } } diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 1fd7117c504..be2f4de8244 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -349,7 +349,7 @@ class WC_Checkout { public function create_order_line_items( &$order, $cart ) { foreach ( $cart->get_cart() as $cart_item_key => $values ) { $product = $values['data']; - $item = new WC_Order_Item_Product(); + $item = apply_filters( 'woocommerce_order_line_item', new WC_Order_Item_Product()); $item->legacy_values = $values; // @deprecated For legacy actions. $item->legacy_cart_item_key = $cart_item_key; // @deprecated For legacy actions. $item->set_props( array( diff --git a/includes/class-wc-order-factory.php b/includes/class-wc-order-factory.php index f465ee8e800..6cacce2c82f 100644 --- a/includes/class-wc-order-factory.php +++ b/includes/class-wc-order-factory.php @@ -79,6 +79,7 @@ class WC_Order_Factory { case 'line_item' : case 'product' : $classname = 'WC_Order_Item_Product'; + $classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id ); break; case 'coupon' : $classname = 'WC_Order_Item_Coupon'; From f71a4988923c75dc64d0629857a482b6bbc79d64 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 25 Apr 2017 16:06:43 +0300 Subject: [PATCH 2/4] Allows to insert conditions for our custom "woocommerce_order_line_item" filter. --- includes/class-wc-checkout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 41551c3ab6a..4e2c9716525 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -349,7 +349,7 @@ class WC_Checkout { public function create_order_line_items( &$order, $cart ) { foreach ( $cart->get_cart() as $cart_item_key => $values ) { $product = $values['data']; - $item = apply_filters( 'woocommerce_order_line_item', new WC_Order_Item_Product()); + $item = apply_filters( 'woocommerce_order_line_item', new WC_Order_Item_Product(), $product); $item->legacy_values = $values; // @deprecated For legacy actions. $item->legacy_cart_item_key = $cart_item_key; // @deprecated For legacy actions. $item->set_props( array( From fdb28218eac2e1893962d8c6c29e8415bd6c2ac5 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 25 Apr 2017 18:07:40 +0300 Subject: [PATCH 3/4] items_type_key filter and woocommerce_order_line_item_object fix --- includes/abstracts/abstract-wc-order.php | 7 ++++--- includes/class-wc-checkout.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 168768766c3..8549f53e45b 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -798,8 +798,9 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { * @return string */ protected function get_items_key( $item ) { - - if ( is_a( $item, 'WC_Order_Item_Fee' ) ) { + if ( is_a( $item, 'WC_Order_Item_Product' ) ) { + return 'line_items'; + } elseif ( is_a( $item, 'WC_Order_Item_Fee' ) ) { return 'fee_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Shipping' ) ) { return 'shipping_lines'; @@ -808,7 +809,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { } elseif ( is_a( $item, 'WC_Order_Item_Coupon' ) ) { return 'coupon_lines'; } else { - return 'line_items'; + return apply_filters( 'woocommerce_items_type_key', '', $item ); } } diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 4e2c9716525..4a5eb2d7298 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -349,7 +349,7 @@ class WC_Checkout { public function create_order_line_items( &$order, $cart ) { foreach ( $cart->get_cart() as $cart_item_key => $values ) { $product = $values['data']; - $item = apply_filters( 'woocommerce_order_line_item', new WC_Order_Item_Product(), $product); + $item = apply_filters( 'woocommerce_order_line_item_object', new WC_Order_Item_Product(), $values ); $item->legacy_values = $values; // @deprecated For legacy actions. $item->legacy_cart_item_key = $cart_item_key; // @deprecated For legacy actions. $item->set_props( array( From 109e951de3bdfa50c8c72a052fefe904c84a658f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 23 May 2017 19:16:30 +0100 Subject: [PATCH 4/4] Renamed filters and actions --- includes/abstracts/abstract-wc-order.php | 3 +-- includes/class-wc-checkout.php | 6 +++++- includes/class-wc-order-factory.php | 6 ++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 8549f53e45b..e7f8bf31d4d 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -808,9 +808,8 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { return 'tax_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Coupon' ) ) { return 'coupon_lines'; - } else { - return apply_filters( 'woocommerce_items_type_key', '', $item ); } + return apply_filters( 'woocommerce_get_items_key', '', $item ); } /** diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 4a5eb2d7298..521db413b69 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -348,8 +348,12 @@ class WC_Checkout { */ public function create_order_line_items( &$order, $cart ) { foreach ( $cart->get_cart() as $cart_item_key => $values ) { + /** + * Filter hook to get inital item object. + * @since 3.1.0 + */ + $item = apply_filters( 'woocommerce_checkout_create_order_line_item_object', new WC_Order_Item_Product(), $cart_item_key, $values, $order ); $product = $values['data']; - $item = apply_filters( 'woocommerce_order_line_item_object', new WC_Order_Item_Product(), $values ); $item->legacy_values = $values; // @deprecated For legacy actions. $item->legacy_cart_item_key = $cart_item_key; // @deprecated For legacy actions. $item->set_props( array( diff --git a/includes/class-wc-order-factory.php b/includes/class-wc-order-factory.php index 6cacce2c82f..2ca01b6bb1f 100644 --- a/includes/class-wc-order-factory.php +++ b/includes/class-wc-order-factory.php @@ -79,7 +79,6 @@ class WC_Order_Factory { case 'line_item' : case 'product' : $classname = 'WC_Order_Item_Product'; - $classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id ); break; case 'coupon' : $classname = 'WC_Order_Item_Coupon'; @@ -93,11 +92,10 @@ class WC_Order_Factory { case 'tax' : $classname = 'WC_Order_Item_Tax'; break; - default : - $classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id ); - break; } + $classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id ); + if ( $classname && class_exists( $classname ) ) { try { return new $classname( $id );