From 0b5d0c99ba930a5627029a0f4e6ceee4c005037a Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Thu, 27 Jun 2024 12:34:39 +0300 Subject: [PATCH] Added bonus cancellation when deleting an order --- src/include/class-wc-retailcrm-base.php | 8 ++++++++ src/include/class-wc-retailcrm-orders.php | 14 ++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index d173d34..12a5d5e 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -124,6 +124,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('woocommerce_removed_coupon', [$this, 'remove_coupon'], 11, 1); add_action('woocommerce_applied_coupon', [$this, 'apply_coupon'], 11, 1); add_action('woocommerce_review_order_before_payment', [$this, 'reviewCreditBonus'], 11, 1); + add_action('wp_trash_post', [$this, 'trash_order_action'], 10, 1); } // Subscribed hooks @@ -584,6 +585,13 @@ if (!class_exists('WC_Retailcrm_Base')) { } } + public function trash_order_action($id) + { + if ('shop_order' == get_post_type($id)) { + $this->orders->updateOrder($id, true); + } + } + /** * Init google analytics code */ diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 18256ab..9c423f7 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -309,7 +309,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : * @return WC_Order $order | null * @throws \Exception */ - public function updateOrder($orderId) + public function updateOrder($orderId, $statusTrash = false) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; @@ -319,7 +319,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : $wcOrder = wc_get_order($orderId); $needRecalculate = false; - $this->processOrder($wcOrder, true); + $this->processOrder($wcOrder, true, $statusTrash); if ($this->cancelLoyalty) { $this->cancelLoyalty = false; @@ -422,7 +422,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : * @return void * @throws \Exception */ - protected function processOrder($order, $update = false) + protected function processOrder($order, $update = false, $statusTrash = false) { if (!$order instanceof WC_Order) { return; @@ -496,7 +496,13 @@ if (!class_exists('WC_Retailcrm_Orders')) : if ($result !== []) { $crmItems = $result['items']; - if ($result['discountType'] !== null && in_array($order->get_status(), ['cancelled', 'refunded'])) { + if ( + $statusTrash + || ( + $result['discountType'] !== null + && in_array($order->get_status(), ['cancelled', 'refunded']) + ) + ) { $this->cancelLoyalty = true; $this->order_item->cancelLoyalty = true; } else {