From f4a4f6c36175a0193c5f5a73b96fae89de731750 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 9 Aug 2016 14:15:04 +0100 Subject: [PATCH] Reduce stock and other notices --- includes/abstracts/abstract-wc-order.php | 73 +++++++++++-------- .../api/class-wc-rest-orders-controller.php | 2 +- .../api/legacy/v3/class-wc-api-orders.php | 2 +- includes/class-wc-form-handler.php | 4 +- .../gateways/bacs/class-wc-gateway-bacs.php | 2 +- .../cheque/class-wc-gateway-cheque.php | 2 +- .../gateways/cod/class-wc-gateway-cod.php | 2 +- .../class-wc-gateway-paypal-request.php | 2 +- .../class-wc-gateway-paypal-response.php | 4 +- ...ss-wc-addons-gateway-simplify-commerce.php | 2 +- .../class-wc-shortcode-checkout.php | 6 +- includes/wc-cart-functions.php | 2 +- includes/wc-order-functions.php | 2 +- includes/wc-user-functions.php | 2 +- templates/order/order-details-customer.php | 4 +- 15 files changed, 62 insertions(+), 49 deletions(-) diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 7f953c3d1e0..a7993656a52 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -296,35 +296,37 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { */ protected function save_items() { foreach ( $this->_items as $item_group => $items ) { - foreach ( $items as $item_key => $item ) { - $item->set_order_id( $this->get_id() ); - $item_id = $item->save(); + if ( is_array( $items ) ) { + foreach ( $items as $item_key => $item ) { + $item->set_order_id( $this->get_id() ); + $item_id = $item->save(); - // If ID changed (new item saved to DB)... - if ( $item_id !== $item_key ) { - $this->_items[ $item_group ][ $item_id ] = $item; - unset( $this->_items[ $item_group ][ $item_key ] ); + // If ID changed (new item saved to DB)... + if ( $item_id !== $item_key ) { + $this->_items[ $item_group ][ $item_id ] = $item; + unset( $this->_items[ $item_group ][ $item_key ] ); - // Legacy action handler - switch ( $item_group ) { - case 'fee_lines' : - if ( has_action( 'woocommerce_add_order_fee_meta' ) && isset( $item->legacy_fee, $item->legacy_fee_key ) ) { - _deprecated_function( 'Action: woocommerce_add_order_fee_meta', '2.7', 'Use woocommerce_new_order_item action instead.' ); - do_action( 'woocommerce_add_order_fee_meta', $this->get_id(), $item_id, $item->legacy_fee, $item->legacy_fee_key ); - } - break; - case 'shipping_lines' : - if ( has_action( 'woocommerce_add_shipping_order_item' ) && isset( $item->legacy_package_key ) ) { - _deprecated_function( 'Action: woocommerce_add_shipping_order_item', '2.7', 'Use woocommerce_new_order_item action instead.' ); - do_action( 'woocommerce_add_shipping_order_item', $item_id, $item->legacy_package_key ); - } - break; - case 'line_items' : - if ( has_action( 'woocommerce_add_order_item_meta' ) && isset( $item->legacy_values, $item->legacy_cart_item_key ) ) { - _deprecated_function( 'Action: woocommerce_add_order_item_meta', '2.7', 'Use woocommerce_new_order_item action instead.' ); - do_action( 'woocommerce_add_order_item_meta', $item_id, $item->legacy_values, $item->legacy_cart_item_key ); - } - break; + // Legacy action handler + switch ( $item_group ) { + case 'fee_lines' : + if ( has_action( 'woocommerce_add_order_fee_meta' ) && isset( $item->legacy_fee, $item->legacy_fee_key ) ) { + _deprecated_function( 'Action: woocommerce_add_order_fee_meta', '2.7', 'Use woocommerce_new_order_item action instead.' ); + do_action( 'woocommerce_add_order_fee_meta', $this->get_id(), $item_id, $item->legacy_fee, $item->legacy_fee_key ); + } + break; + case 'shipping_lines' : + if ( has_action( 'woocommerce_add_shipping_order_item' ) && isset( $item->legacy_package_key ) ) { + _deprecated_function( 'Action: woocommerce_add_shipping_order_item', '2.7', 'Use woocommerce_new_order_item action instead.' ); + do_action( 'woocommerce_add_shipping_order_item', $item_id, $item->legacy_package_key ); + } + break; + case 'line_items' : + if ( has_action( 'woocommerce_add_order_item_meta' ) && isset( $item->legacy_values, $item->legacy_cart_item_key ) ) { + _deprecated_function( 'Action: woocommerce_add_order_item_meta', '2.7', 'Use woocommerce_new_order_item action instead.' ); + do_action( 'woocommerce_add_order_item_meta', $item_id, $item->legacy_values, $item->legacy_cart_item_key ); + } + break; + } } } } @@ -831,18 +833,29 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { * @return Array of WC_Order_item */ public function get_items( $types = 'line_item' ) { + $type_to_group = array( + 'line_item' => 'line_items', + 'tax' => 'tax_lines', + 'shipping' => 'shipping_lines', + 'fee' => 'fee_lines', + 'coupon' => 'coupon_lines', + ); + if ( is_array( $types ) ) { foreach ( $types as $type ) { - if ( is_null( $this->_items[ $type ] ) ) { - $this->_items[ $type ] = $this->get_items_from_db( $type ); + if ( isset( $type_to_group[ $type ] ) && is_null( $this->_items[ $type_to_group[ $type ] ] ) ) { + $this->_items[ $type_to_group[ $type ] ] = $this->get_items_from_db( $type ); } } } $items = array(); + $types = (array) $types; foreach ( $types as $type ) { - $items = array_merge( $items, $this->_items[ $type ] ); + if ( isset( $type_to_group[ $type ] ) && isset( $this->_items[ $type_to_group[ $type ] ] ) ) { + $items = array_merge( $items, $this->_items[ $type_to_group[ $type ] ] ); + } } return apply_filters( 'woocommerce_order_get_items', $items, $this ); diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index a54aa29e777..0a83409f7dd 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -128,7 +128,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller { 'id' => $order->get_id(), 'parent_id' => $post->post_parent, 'status' => $order->get_status(), - 'order_key' => $order->order_key, + 'order_key' => $order->get_order_key(), 'number' => $order->get_order_number(), 'currency' => $order->get_currency(), 'version' => $order->order_version, diff --git a/includes/api/legacy/v3/class-wc-api-orders.php b/includes/api/legacy/v3/class-wc-api-orders.php index 7662a16bdec..03c8cc90485 100644 --- a/includes/api/legacy/v3/class-wc-api-orders.php +++ b/includes/api/legacy/v3/class-wc-api-orders.php @@ -161,7 +161,7 @@ class WC_API_Orders extends WC_API_Resource { $order_data = array( 'id' => $order->get_id(), 'order_number' => $order->get_order_number(), - 'order_key' => $order->order_key, + 'order_key' => $order->get_order_key(), 'created_at' => $this->server->format_datetime( $order_post->post_date_gmt ), 'updated_at' => $this->server->format_datetime( $order_post->post_modified_gmt ), 'completed_at' => $this->server->format_datetime( $order->completed_date, true ), diff --git a/includes/class-wc-form-handler.php b/includes/class-wc-form-handler.php index ae52f74afc1..f5a9375c3f0 100644 --- a/includes/class-wc-form-handler.php +++ b/includes/class-wc-form-handler.php @@ -288,7 +288,7 @@ class WC_Form_Handler { $order_id = absint( $wp->query_vars['order-pay'] ); $order = wc_get_order( $order_id ); - if ( $order->get_id() == $order_id && $order->order_key == $order_key && $order->needs_payment() ) { + if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key && $order->needs_payment() ) { do_action( 'woocommerce_before_pay_action', $order ); @@ -660,7 +660,7 @@ class WC_Form_Handler { if ( $order->has_status( 'cancelled' ) ) { // Already cancelled - take no action - } elseif ( $user_can_cancel && $order_can_cancel && $order->get_id() === $order_id && $order->order_key === $order_key ) { + } elseif ( $user_can_cancel && $order_can_cancel && $order->get_id() === $order_id && $order->get_order_key() === $order_key ) { // Cancel the order + restore stock $order->cancel_order( __('Order cancelled by customer.', 'woocommerce' ) ); diff --git a/includes/gateways/bacs/class-wc-gateway-bacs.php b/includes/gateways/bacs/class-wc-gateway-bacs.php index 807e73db276..091e46e3369 100644 --- a/includes/gateways/bacs/class-wc-gateway-bacs.php +++ b/includes/gateways/bacs/class-wc-gateway-bacs.php @@ -337,7 +337,7 @@ class WC_Gateway_BACS extends WC_Payment_Gateway { $order->update_status( 'on-hold', __( 'Awaiting BACS payment', 'woocommerce' ) ); // Reduce stock levels - $order->reduce_order_stock(); + wc_reduce_stock_levels( $order_id ); // Remove cart WC()->cart->empty_cart(); diff --git a/includes/gateways/cheque/class-wc-gateway-cheque.php b/includes/gateways/cheque/class-wc-gateway-cheque.php index 0aff21a2ab8..b57fc8c9f5b 100644 --- a/includes/gateways/cheque/class-wc-gateway-cheque.php +++ b/includes/gateways/cheque/class-wc-gateway-cheque.php @@ -116,7 +116,7 @@ class WC_Gateway_Cheque extends WC_Payment_Gateway { $order->update_status( 'on-hold', _x( 'Awaiting check payment', 'Check payment method', 'woocommerce' ) ); // Reduce stock levels - $order->reduce_order_stock(); + wc_reduce_stock_levels( $order_id ); // Remove cart WC()->cart->empty_cart(); diff --git a/includes/gateways/cod/class-wc-gateway-cod.php b/includes/gateways/cod/class-wc-gateway-cod.php index a5286f8d86e..e43163cd89c 100644 --- a/includes/gateways/cod/class-wc-gateway-cod.php +++ b/includes/gateways/cod/class-wc-gateway-cod.php @@ -203,7 +203,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway { $order->update_status( apply_filters( 'woocommerce_cod_process_payment_order_status', $order->has_downloadable_item() ? 'on-hold' : 'processing', $order ), __( 'Payment to be made upon delivery.', 'woocommerce' ) ); // Reduce stock levels - $order->reduce_order_stock(); + wc_reduce_stock_levels( $order_id ); // Remove cart WC()->cart->empty_cart(); diff --git a/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php b/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php index cd3b66a7008..074d279c7c0 100644 --- a/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php +++ b/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php @@ -77,7 +77,7 @@ class WC_Gateway_Paypal_Request { 'paymentaction' => $this->gateway->get_option( 'paymentaction' ), 'bn' => 'WooThemes_Cart', 'invoice' => $this->gateway->get_option( 'invoice_prefix' ) . $order->get_order_number(), - 'custom' => json_encode( array( 'order_id' => $order->get_id(), 'order_key' => $order->order_key ) ), + 'custom' => json_encode( array( 'order_id' => $order->get_id(), 'order_key' => $order->get_order_key() ) ), 'notify_url' => $this->notify_url, 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), diff --git a/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php b/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php index b2a9d585201..0753653d94e 100644 --- a/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php +++ b/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php @@ -40,7 +40,7 @@ abstract class WC_Gateway_Paypal_Response { $order = wc_get_order( $order_id ); } - if ( ! $order || $order->order_key !== $order_key ) { + if ( ! $order || $order->get_order_key() !== $order_key ) { WC_Gateway_Paypal::log( 'Error: Order Keys do not match.' ); return false; } @@ -66,7 +66,7 @@ abstract class WC_Gateway_Paypal_Response { */ protected function payment_on_hold( $order, $reason = '' ) { $order->update_status( 'on-hold', $reason ); - $order->reduce_order_stock(); + wc_reduce_stock_levels( $order_id ); WC()->cart->empty_cart(); } } diff --git a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php index b65d13ca1e9..6e2494b32de 100644 --- a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php +++ b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php @@ -214,7 +214,7 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { } // Reduce stock levels - $order->reduce_order_stock(); + wc_reduce_stock_levels( $order_id ); // Remove cart WC()->cart->empty_cart(); diff --git a/includes/shortcodes/class-wc-shortcode-checkout.php b/includes/shortcodes/class-wc-shortcode-checkout.php index 89dee8fcfca..1480f9683f5 100644 --- a/includes/shortcodes/class-wc-shortcode-checkout.php +++ b/includes/shortcodes/class-wc-shortcode-checkout.php @@ -90,7 +90,7 @@ class WC_Shortcode_Checkout { return; } - if ( $order->get_id() == $order_id && $order->order_key == $order_key ) { + if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key ) { if ( $order->needs_payment() ) { @@ -133,7 +133,7 @@ class WC_Shortcode_Checkout { $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : ''; $order = wc_get_order( $order_id ); - if ( $order->get_id() == $order_id && $order->order_key == $order_key ) { + if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key ) { if ( $order->needs_payment() ) { @@ -200,7 +200,7 @@ class WC_Shortcode_Checkout { if ( $order_id > 0 ) { $order = wc_get_order( $order_id ); - if ( $order->order_key != $order_key ) { + if ( $order->get_order_key() != $order_key ) { $order = false; } } diff --git a/includes/wc-cart-functions.php b/includes/wc-cart-functions.php index d379bf25f8a..703667d7b58 100644 --- a/includes/wc-cart-functions.php +++ b/includes/wc-cart-functions.php @@ -162,7 +162,7 @@ function wc_clear_cart_after_payment() { if ( $order_id > 0 ) { $order = wc_get_order( $order_id ); - if ( $order->order_key === $order_key ) { + if ( $order->get_order_key() === $order_key ) { WC()->cart->empty_cart(); } } diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index a17dff8c343..3c95669af55 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -432,7 +432,7 @@ function wc_downloadable_file_permission( $download_id, $product_id, $order, $qt 'user_id' => absint( $order->get_user_id() ), 'user_email' => $user_email, 'order_id' => $order->get_id(), - 'order_key' => $order->order_key, + 'order_key' => $order->get_order_key(), 'downloads_remaining' => $limit, 'access_granted' => current_time( 'mysql' ), 'download_count' => 0 diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php index 0df693123a9..f2ecfac1414 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -456,7 +456,7 @@ function wc_get_customer_available_downloads( $customer_id ) { 'product_id' => $product_id, 'download_name' => $download_name, 'order_id' => $order->get_id(), - 'order_key' => $order->order_key, + 'order_key' => $order->get_order_key(), 'downloads_remaining' => $result->downloads_remaining, 'access_expires' => $result->access_expires, 'file' => $download_file diff --git a/templates/order/order-details-customer.php b/templates/order/order-details-customer.php index 11a3f204583..d989cfb0db9 100644 --- a/templates/order/order-details-customer.php +++ b/templates/order/order-details-customer.php @@ -23,10 +23,10 @@ if ( ! defined( 'ABSPATH' ) ) {

- customer_note ) : ?> + get_customer_note() ) : ?> - +
customer_note ); ?>get_customer_note() ); ?>