From d332c8bea8f2615c3cf664fa50c180b570a70b31 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 8 May 2024 17:38:02 +0300 Subject: [PATCH] ref #72069 Functional transfer Deleting repetitive code sections --- src/include/class-wc-retailcrm-base.php | 14 +++----- src/include/class-wc-retailcrm-loyalty.php | 35 ++++++++++++------- .../class-wc-retailcrm-loyalty-validator.php | 7 ++-- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 7ab9377..3f3b734 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -117,7 +117,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('woocommerce_add_to_cart', [$this, 'refresh_loyalty_coupon'], 11, 1); add_action('woocommerce_after_cart_item_quantity_update', [$this, 'refresh_loyalty_coupon'], 11, 1); add_action('woocommerce_cart_item_removed', [$this, 'refresh_loyalty_coupon'], 11, 1); - add_action('woocommerce_before_cart_empted', [$this, 'delete_loyalty_coupon'], 11, 1); + add_action('woocommerce_before_cart_empted', [$this, 'clear_loyalty_coupon'], 11, 1); add_action('woocommerce_removed_coupon', [$this, 'removed_coupon'], 11, 1); add_action('woocommerce_applied_coupon', [$this, 'applied_coupon'], 11, 1); } @@ -712,10 +712,10 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - public function delete_loyalty_coupon() + public function clear_loyalty_coupon() { try { - $this->loyalty->deleteAppliedLoyaltyCoupon(); + $this->loyalty->clearLoyaltyCoupon(); } catch (Throwable $exception) { writeBaseLogs($exception->getMessage()); } @@ -724,11 +724,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function removed_coupon($couponCode) { try { - if (preg_match('/^pl\d+$/m', $couponCode) === 1) { - $coupon = new WC_Coupon($couponCode); - - $coupon->delete(true); - } else { + if (!$this->loyalty->deleteLoyaltyCoupon($couponCode)) { $this->loyalty->createLoyaltyCoupon(true); } } catch (Throwable $exception) { @@ -739,7 +735,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function applied_coupon($couponCode) { try { - if (preg_match('/^pl\d+$/m', $couponCode) !== 1) { + if (!$this->loyalty->isLoyaltyCoupon($couponCode)) { $this->loyalty->createLoyaltyCoupon(true); } } catch (Throwable $exception) { diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 314441e..e51ec7f 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -134,7 +134,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : $discount = 0; - //Checking if the loyalty discount is a percentage discount + //Checking if the loyalty discount is a percent discount foreach ($response['order']['items'] as $item) { if (!isset($item['discounts'])) { continue; @@ -197,7 +197,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : $couponsLp = []; // Check exists used loyalty coupons foreach ($woocommerce->cart->get_coupons() as $code => $coupon) { - if (preg_match('/^pl\d+$/m', $code) === 1) { + if ($this->isLoyaltyCoupon($code)) { $couponsLp[] = $code; } } @@ -249,7 +249,6 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : //Generate new coupon $coupon = new WC_Coupon(); - //$coupon->set_individual_use(true); // запрещает использование других купонов одноврeменно с этим $coupon->set_usage_limit(0); $coupon->set_amount($lpDiscountSum); $coupon->set_email_restrictions($woocommerce->customer->get_email()); @@ -262,14 +261,8 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return $resultString; } - $loyaltyInfo = $this->getLoyaltyAccounts($customerId); - - if (!isset($loyaltyInfo['loyaltyAccounts'][0])) { - return null; - } - //If a percentage discount, automatically apply a loyalty coupon - if ($loyaltyInfo['loyaltyAccounts'][0]['level']['type'] === 'discount') { + if ($validator->loyaltyAccount['level']['type'] === 'discount') { $woocommerce->cart->apply_coupon($coupon->get_code()); return $resultString; @@ -280,12 +273,12 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return $resultString . '
' . 'Your coupon: ' . $coupon->get_code() . '
'; } - public function deleteAppliedLoyaltyCoupon() + public function clearLoyaltyCoupon() { global $woocommerce; foreach ($woocommerce->cart->get_coupons() as $code => $coupon) { - if (preg_match('/^pl\d+$/m', $code) === 1) { + if ($this->isLoyaltyCoupon($code)) { $woocommerce->cart->remove_coupon($code); $coupon = new WC_Coupon($code); @@ -295,6 +288,24 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } } + public function deleteLoyaltyCoupon($couponCode) + { + if ($this->isLoyaltyCoupon($couponCode)) { + $coupon = new WC_Coupon($couponCode); + + $coupon->delete(true); + + return true; + } + + return false; + } + + public function isLoyaltyCoupon($couponCode): bool + { + return preg_match('/^pl\d+$/m', $couponCode) === 1; + } + public function getCouponLoyalty($email) { global $wpdb; diff --git a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php index a65d608..755e3b0 100644 --- a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php +++ b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php @@ -16,10 +16,13 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) : { /** @var WC_Retailcrm_Client_V5 */ protected $apiClient; - protected $crmUser; - protected $loyaltyAccount; + protected $isActiveCorp; + public $crmUser; + + public $loyaltyAccount; + public function __construct($apiClient, $isActiveCorp) { $this->apiClient = $apiClient;