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 . '