Skip to content

Commit fa45eb9

Browse files
author
Anton
committed
add discount coupon
1 parent 8b57365 commit fa45eb9

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/include/class-wc-retailcrm-base.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ public function coupon_info()
10171017
WC_Retailcrm_Logger::setHook(current_action());
10181018

10191019
try {
1020-
$result = $this->loyalty->createLoyaltyCoupon();
1020+
$result = $this->loyalty->processingLoyaltyCoupon();
10211021

10221022
if ($result) {
10231023
echo wp_kses($result, [
@@ -1067,7 +1067,7 @@ public function refresh_loyalty_coupon()
10671067
WC_Retailcrm_Logger::setHook(current_action());
10681068

10691069
try {
1070-
$this->loyalty->createLoyaltyCoupon(true);
1070+
$this->loyalty->processingLoyaltyCoupon(true);
10711071
} catch (Throwable $exception) {
10721072
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
10731073
}
@@ -1090,7 +1090,7 @@ public function remove_coupon($couponCode)
10901090

10911091
try {
10921092
if (!$this->loyalty->deleteLoyaltyCoupon($couponCode)) {
1093-
$this->loyalty->createLoyaltyCoupon(true);
1093+
$this->loyalty->processingLoyaltyCoupon(true);
10941094
}
10951095
} catch (Throwable $exception) {
10961096
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
@@ -1103,7 +1103,7 @@ public function apply_coupon($couponCode)
11031103

11041104
try {
11051105
if (!$this->loyalty->isLoyaltyCoupon($couponCode)) {
1106-
$this->loyalty->createLoyaltyCoupon(true);
1106+
$this->loyalty->processingLoyaltyCoupon(true);
11071107
}
11081108
} catch (Throwable $exception) {
11091109
WC_Retailcrm_Logger::exception(__METHOD__, $exception);

src/include/class-wc-retailcrm-loyalty.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private function getLoyaltyAccounts(int $userId)
210210
return $response;
211211
}
212212

213-
public function createLoyaltyCoupon($refreshCoupon = false)
213+
public function processingLoyaltyCoupon($refreshCoupon = false)
214214
{
215215
global $woocommerce;
216216

@@ -274,6 +274,12 @@ public function createLoyaltyCoupon($refreshCoupon = false)
274274
$coupon->delete(true);
275275
}
276276

277+
if ($this->validator->loyaltyAccount['level']['type'] === 'discount') {
278+
$this->discountCouponApply();
279+
280+
return $resultString;
281+
}
282+
277283
$html = sprintf(
278284
'<div style="margin-bottom:15px padding-top:15px">
279285
<div id="hidden-count" hidden>%d</div>
@@ -292,6 +298,21 @@ public function createLoyaltyCoupon($refreshCoupon = false)
292298
return $resultString;
293299
}
294300

301+
private function discountCouponApply()
302+
{
303+
global $woocommerce;
304+
305+
$coupon = new WC_Coupon();
306+
307+
$coupon->set_usage_limit(1);
308+
$coupon->set_amount(intval($_POST['count']));
309+
$coupon->set_email_restrictions($woocommerce->customer->get_email());
310+
$coupon->set_code('loyalty' . wp_rand());
311+
$coupon->save();
312+
313+
WC()->cart->apply_coupon($coupon->get_code());
314+
}
315+
295316
public function clearLoyaltyCoupon()
296317
{
297318
global $woocommerce;

0 commit comments

Comments
 (0)