@@ -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 !== '' ) {
0 commit comments