Skip to content

Commit 4cecc92

Browse files
author
Anton
committed
add static
1 parent 4173f73 commit 4cecc92

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

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

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ public function __construct($retailcrm = false)
137137
add_action('init', [$this, 'add_loyalty_endpoint'], 11, 1);
138138
add_action('woocommerce_account_menu_items', [$this, 'add_loyalty_item'], 11, 1);
139139
add_action('woocommerce_account_loyalty_endpoint', [$this, 'show_loyalty'], 11, 1);
140-
add_action('wp_ajax_create_loyalty_coupon', [$this, 'create_loyalty_coupon'], 104);
141-
add_action('wp_ajax_apply_coupon_to_cart', [$this, 'apply_coupon_to_cart'], 105);
140+
add_action('wp_ajax_create_loyalty_coupon', [WC_Retailcrm_Loyalty::class, 'create_loyalty_coupon'], 104);
141+
add_action('wp_ajax_apply_coupon_to_cart', [WC_Retailcrm_Loyalty::class, 'apply_coupon_to_cart'], 105);
142142

143143
// Add coupon hooks for loyalty program
144144
add_action('woocommerce_cart_coupon', [$this, 'coupon_info'], 11, 1);
@@ -1697,52 +1697,6 @@ public function add_retailcrm_tracking_script() {
16971697
}
16981698
}
16991699

1700-
public function create_loyalty_coupon()
1701-
{
1702-
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
1703-
1704-
if ( ! wp_verify_nonce($nonce, 'loyalty_coupon_nonce') ) {
1705-
wp_send_json_error('Incorrect request');
1706-
}
1707-
1708-
if (!isset($_POST['count']) || $_POST['count'] <= 0) {
1709-
wp_send_json_error('Incorrect bonus count');
1710-
}
1711-
1712-
global $woocommerce;
1713-
1714-
$coupon = new WC_Coupon();
1715-
1716-
$coupon->set_usage_limit(1);
1717-
$coupon->set_amount(intval($_POST['count']));
1718-
$coupon->set_email_restrictions($woocommerce->customer->get_email());
1719-
$coupon->set_code('loyalty' . wp_rand());
1720-
$coupon->save();
1721-
1722-
wp_send_json_success(['coupon_code' => $coupon->get_code()]);
1723-
}
1724-
1725-
public function apply_coupon_to_cart()
1726-
{
1727-
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
1728-
1729-
if ( ! wp_verify_nonce($nonce, 'apply_coupon_nonce') ) {
1730-
wp_send_json_error('Incorrect request');
1731-
}
1732-
1733-
if (!isset($_POST['coupon_code']) || $_POST['coupon_code'] === '') {
1734-
wp_send_json_error('Incorrect coupon code');
1735-
}
1736-
1737-
$coupon_code = sanitize_text_field(wp_unslash($_POST['coupon_code']));
1738-
1739-
if (WC()->cart->apply_coupon($coupon_code)) {
1740-
wp_send_json_success('Coupon applied successfully');
1741-
} else {
1742-
wp_send_json_error('Failed to apply coupon');
1743-
}
1744-
}
1745-
17461700
private function accessLog($prefixNonce = ''): void
17471701
{
17481702
if ($prefixNonce !== '') {

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,52 @@ public function getBonusDetails($loyaltyId, $status)
646646
return [];
647647
}
648648
}
649+
650+
public static function create_loyalty_coupon()
651+
{
652+
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
653+
654+
if ( ! wp_verify_nonce($nonce, 'loyalty_coupon_nonce') ) {
655+
wp_send_json_error('Incorrect request');
656+
}
657+
658+
if (!isset($_POST['count']) || $_POST['count'] <= 0) {
659+
wp_send_json_error('Incorrect bonus count');
660+
}
661+
662+
global $woocommerce;
663+
664+
$coupon = new WC_Coupon();
665+
666+
$coupon->set_usage_limit(1);
667+
$coupon->set_amount(intval($_POST['count']));
668+
$coupon->set_email_restrictions($woocommerce->customer->get_email());
669+
$coupon->set_code('loyalty' . wp_rand());
670+
$coupon->save();
671+
672+
wp_send_json_success(['coupon_code' => $coupon->get_code()]);
673+
}
674+
675+
public static function apply_coupon_to_cart()
676+
{
677+
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
678+
679+
if ( ! wp_verify_nonce($nonce, 'apply_coupon_nonce') ) {
680+
wp_send_json_error('Incorrect request');
681+
}
682+
683+
if (!isset($_POST['coupon_code']) || $_POST['coupon_code'] === '') {
684+
wp_send_json_error('Incorrect coupon code');
685+
}
686+
687+
$coupon_code = sanitize_text_field(wp_unslash($_POST['coupon_code']));
688+
689+
if (WC()->cart->apply_coupon($coupon_code)) {
690+
wp_send_json_success('Coupon applied successfully');
691+
} else {
692+
wp_send_json_error('Failed to apply coupon');
693+
}
694+
}
649695
}
650696

651697
endif;

0 commit comments

Comments
 (0)