diff --git a/resources/pot/retailcrm-es_ES.pot b/resources/pot/retailcrm-es_ES.pot index 356d5f0..a09c4d5 100644 --- a/resources/pot/retailcrm-es_ES.pot +++ b/resources/pot/retailcrm-es_ES.pot @@ -588,3 +588,6 @@ msgstr "bonos" msgid "Use coupon:" msgstr "Utiliza el cupón:" + +msgid "Points will be awarded upon completion of the order:" +msgstr "Los puntos se concederán al finalizar el pedido:" diff --git a/resources/pot/retailcrm-ru_RU.pot b/resources/pot/retailcrm-ru_RU.pot index 193c76e..d6cf7c0 100644 --- a/resources/pot/retailcrm-ru_RU.pot +++ b/resources/pot/retailcrm-ru_RU.pot @@ -597,3 +597,6 @@ msgstr "бонусов" msgid "Use coupon:" msgstr "Используйте купон:" + +msgid "Points will be awarded upon completion of the order:" +msgstr "По завершению заказа будет начислено баллов:" diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 710a813..d732de8 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -120,6 +120,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('woocommerce_before_cart_empted', [$this, 'clear_loyalty_coupon'], 11, 1); 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); } // Subscribed hooks @@ -743,6 +744,15 @@ if (!class_exists('WC_Retailcrm_Base')) { } } + public function reviewCreditBonus() + { + $resultHtml = $this->loyalty->getCreditBonuses(); + + if ($resultHtml) { + echo $resultHtml; + } + } + /** * In this method we include CSS file * diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 67ab5d7..a880535 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -113,28 +113,9 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : private function getDiscountLoyalty($cartItems, $site, $customerId) { - $order = [ - 'site' => $site, - 'customer' => ['externalId' => $customerId], - 'privilegeType' => 'loyalty_level' - ]; + $response = $this->calculateDiscountLoyalty($cartItems, $site, $customerId); - $useXmlId = isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] === WC_Retailcrm_Base::YES; - - foreach ($cartItems as $item) { - $product = $item['data']; - - $order['items'][] = [ - 'offer' => $useXmlId ? ['xmlId' => $product->get_sku()] : ['externalId' => $product->get_id()], - 'quantity' => $item['quantity'], - 'initialPrice' => wc_get_price_including_tax($product), - 'discountManualAmount' => ($item['line_subtotal'] - $item['line_total']) / $item['quantity'] - ]; - } - - $response = $this->apiClient->calculateDiscountLoyalty($site, $order); - - if (!$response->isSuccessful() || !isset($response['calculations'])) { + if ($response === 0) { return 0; } @@ -455,6 +436,98 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return ['items' => $crmItems, 'discountType' => $discountType]; } + + public function calculateDiscountLoyalty($cartItems, $site, $customerId, $bonuses = 0) + { + $order = [ + 'site' => $site, + 'customer' => ['externalId' => $customerId], + 'privilegeType' => 'loyalty_level' + ]; + + $useXmlId = isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] === WC_Retailcrm_Base::YES; + + foreach ($cartItems as $item) { + $product = $item['data']; + + $order['items'][] = [ + 'offer' => $useXmlId ? ['xmlId' => $product->get_sku()] : ['externalId' => $product->get_id()], + 'quantity' => $item['quantity'], + 'initialPrice' => wc_get_price_including_tax($product), + 'discountManualAmount' => ($item['line_subtotal'] - $item['line_total']) / $item['quantity'] + ]; + } + + $response = $this->apiClient->calculateDiscountLoyalty($site, $order, (float) $bonuses); + + if (!$response->isSuccessful() || !isset($response['calculations'])) { + return 0; + } + + return $response; + } + + public function getCreditBonuses(): string + { + global $woocommerce; + + $customerId = $woocommerce->customer ? $woocommerce->customer->get_id() : null; + $site = $this->apiClient->getSingleSiteForKey(); + + if (!$customerId || !$woocommerce->cart || !$woocommerce->cart->get_cart() || !$site) { + return ''; + } + + $loyaltyCoupon = null; + $coupons = $woocommerce->cart->get_applied_coupons(); + + foreach ($coupons as $coupon) { + if ($this->isLoyaltyCoupon($coupon)) { + $loyaltyCoupon = new WC_Coupon($coupon); + + $woocommerce->cart->remove_coupon($coupon); + $woocommerce->cart->calculate_totals(); + + break; + } + } + + if ($loyaltyCoupon) { + $chargeBonuses = $loyaltyCoupon->get_amount(); + } + + $cartItems = $woocommerce->cart->get_cart(); + $response = $this->calculateDiscountLoyalty($cartItems, $site, $customerId, $chargeBonuses ?? 0); + + if ($loyaltyCoupon) { + $coupon = new WC_Coupon(); + $coupon->set_usage_limit(0); + $coupon->set_amount($loyaltyCoupon->get_amount()); + $coupon->set_email_restrictions($loyaltyCoupon->get_email_restrictions()); + $coupon->set_code($loyaltyCoupon->get_code()); + $coupon->save(); + + $woocommerce->cart->apply_coupon($coupon->get_code()); + $woocommerce->cart->calculate_totals(); + } + + if ($response === 0) { + return ''; + } + + $creditBonuses = $response['order']['bonusesCreditTotal']; + + if ($creditBonuses) { + return $this->getHtmlCreditBonuses($creditBonuses); + } + + return ''; + } + + private function getHtmlCreditBonuses($creditBonuses) + { + return '' . __("Points will be awarded upon completion of the order:", 'retailcrm') . ' ' . $creditBonuses . ''; + } } endif; diff --git a/src/languages/retailcrm-es_ES.l10n.php b/src/languages/retailcrm-es_ES.l10n.php index 7a6b3e6..5d58e8e 100644 --- a/src/languages/retailcrm-es_ES.l10n.php +++ b/src/languages/retailcrm-es_ES.l10n.php @@ -247,6 +247,7 @@ return [ "It is possible to write off" => "Es posible debitar", "bonuses" => "bonificaciones", "Use coupon:" => "Utiliza el cupón:", + "Points will be awarded upon completion of the order:" => "Los puntos se concederán al finalizar el pedido:", ], "language" => "es", "x-generator" => "GlotPress/2.4.0-alpha", diff --git a/src/languages/retailcrm-es_ES.mo b/src/languages/retailcrm-es_ES.mo index 43567c3..051c9cf 100644 Binary files a/src/languages/retailcrm-es_ES.mo and b/src/languages/retailcrm-es_ES.mo differ diff --git a/src/languages/retailcrm-ru_RU.l10n.php b/src/languages/retailcrm-ru_RU.l10n.php index 514c7e5..5afac2b 100644 --- a/src/languages/retailcrm-ru_RU.l10n.php +++ b/src/languages/retailcrm-ru_RU.l10n.php @@ -246,6 +246,7 @@ return [ "It is possible to write off" => "Возможно списать", "bonuses" => "бонусов", "Use coupon:" => "Используйте купон:", + "Points will be awarded upon completion of the order:" => "По завершению заказа будет начислено баллов:", ], "language" => "ru", "x-generator" => "GlotPress/2.4.0-alpha", diff --git a/src/languages/retailcrm-ru_RU.mo b/src/languages/retailcrm-ru_RU.mo index acd4ebf..5cb7b69 100644 Binary files a/src/languages/retailcrm-ru_RU.mo and b/src/languages/retailcrm-ru_RU.mo differ