1
0
Fork 0
mirror of synced 2025-04-02 21:36:14 +03:00

ref #96267 Corrects an error when uploading an order with a discount to CRM (#335)

This commit is contained in:
Kocmonavtik 2024-07-01 18:00:02 +03:00 committed by GitHub
parent d354b11ecb
commit aa21948fb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 76 additions and 18 deletions

View file

@ -36,6 +36,9 @@ if (!class_exists('WC_Retailcrm_Base')) {
/** @var WC_Retailcrm_Loyalty */
protected $loyalty;
/** @var array */
protected $updatedOrderId = [];
/**
* Init and hook in the integration.
*
@ -121,8 +124,24 @@ 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);
if (
!$this->get_option('deactivate_update_order')
|| $this->get_option('deactivate_update_order') == static::NO
) {
add_action('woocommerce_update_order', [$this, 'take_update_order'], 11, 1);
add_action('shutdown', [$this, 'update_order_loyalty'], -1);
add_action('woocommerce_saved_order_items', [$this, 'update_order_items'], 10, 1);
}
} elseif (
!$this->get_option('deactivate_update_order')
|| $this->get_option('deactivate_update_order') == static::NO
) {
add_action('woocommerce_update_order', [$this, 'update_order'], 10, 1);
}
// Subscribed hooks
add_action('register_form', [$this, 'subscribe_register_form'], 99);
add_action('woocommerce_register_form', [$this, 'subscribe_woocommerce_register_form'], 99);
@ -135,13 +154,6 @@ if (!class_exists('WC_Retailcrm_Base')) {
);
}
if (
!$this->get_option('deactivate_update_order')
|| $this->get_option('deactivate_update_order') == static::NO
) {
add_action('woocommerce_update_order', [$this, 'update_order'], 11, 1);
}
if ($this->get_option('abandoned_carts_enabled') === static::YES) {
$this->cart = new WC_Retailcrm_Cart($this->apiClient, $this->settings);
@ -549,6 +561,15 @@ if (!class_exists('WC_Retailcrm_Base')) {
}
}
public function update_order($orderId)
{
if (WC_Retailcrm_Plugin::history_running() === true) {
return;
}
$this->orders->updateOrder($orderId);
}
/**
* Edit order in retailCRM
*
@ -558,13 +579,38 @@ if (!class_exists('WC_Retailcrm_Base')) {
*
* @throws \Exception
*/
public function update_order($order_id)
public function take_update_order($order_id)
{
if (WC_Retailcrm_Plugin::history_running() === true) {
if (
WC_Retailcrm_Plugin::history_running() === true
|| did_action('woocommerce_checkout_order_processed')
|| did_action('woocommerce_new_order')
) {
return;
}
$this->orders->updateOrder($order_id);
$this->updatedOrderId[$order_id] = $order_id;
}
public function update_order_loyalty()
{
if ($this->updatedOrderId !== []) {
foreach ($this->updatedOrderId as $orderId) {
$this->orders->updateOrder($orderId);
}
}
}
public function update_order_items($orderId)
{
$this->orders->updateOrder($orderId);
}
public function trash_order_action($id)
{
if ('shop_order' == get_post_type($id)) {
$this->orders->updateOrder($id, true);
}
}
/**

View file

@ -474,7 +474,13 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
$customerId = $woocommerce->customer ? $woocommerce->customer->get_id() : null;
$site = $this->apiClient->getSingleSiteForKey();
if (!$customerId || !$woocommerce->cart || !$woocommerce->cart->get_cart() || !$site) {
if (
!$customerId
|| !$woocommerce->cart
|| !$woocommerce->cart->get_cart()
|| !$site
|| !$this->validator->checkAccount($customerId)
) {
return '';
}

View file

@ -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 {

View file

@ -184,8 +184,6 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
*/
public function isCancelLoyalty($wcItems, $crmItems): bool
{
$loyaltyDiscount = 0;
/** If the number of sales items does not match */
if (count($wcItems) !== count($crmItems)) {
$this->cancelLoyalty = true;
@ -194,6 +192,8 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
}
foreach ($wcItems as $id => $item) {
$loyaltyDiscount = 0;
/** If a trading position has been added/deleted */
if (!isset($crmItems[$id])) {
$this->cancelLoyalty = true;
@ -210,7 +210,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
foreach ($crmItems[$id]['discounts'] as $discount) {
if (in_array($discount['type'], ['bonus_charge', 'loyalty_level'])) {
$loyaltyDiscount += $discount['amount'];
$loyaltyDiscount = $discount['amount'];
break;
}