| 1 |
<?php |
| 2 |
/** |
| 3 |
* Discount module |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Modules\Admin\Discount; |
| 8 |
use WPFunnels\Admin\Module\Wpfnl_Admin_Module; |
| 9 |
use WPFunnels\Traits\SingletonTrait; |
| 10 |
|
| 11 |
class Module extends Wpfnl_Admin_Module |
| 12 |
{ |
| 13 |
use SingletonTrait; |
| 14 |
|
| 15 |
|
| 16 |
protected $coupons = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Implement init_ajax() method. |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function init_ajax() |
| 24 |
{ |
| 25 |
add_action('wp_ajax_wpfnl_coupon_search', [$this, 'fetch_coupon']); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Get view |
| 31 |
* |
| 32 |
* @return File |
| 33 |
*/ |
| 34 |
public function get_view() |
| 35 |
{ |
| 36 |
require_once WPFNL_DIR . '/admin/modules/discount/views/view.php'; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Fetch coupon list by coupon code |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function fetch_coupon() |
| 46 |
{ |
| 47 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 48 |
global $wpdb; |
| 49 |
if (isset($_GET['term'])) { |
| 50 |
$term = sanitize_text_field(wp_unslash($_GET['term'])); |
| 51 |
} |
| 52 |
if (empty($term)) { |
| 53 |
wp_die(); |
| 54 |
} |
| 55 |
$coupon_list = []; |
| 56 |
$discount_types = wc_get_coupon_types(); |
| 57 |
$coupons = $wpdb->get_results( |
| 58 |
$wpdb->prepare( |
| 59 |
"SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s AND post_title LIKE %s AND post_status = %s", |
| 60 |
'shop_coupon', |
| 61 |
$wpdb->esc_like($term) . '%', |
| 62 |
'publish' |
| 63 |
) |
| 64 |
); |
| 65 |
if ($coupons) { |
| 66 |
foreach ($coupons as $post) { |
| 67 |
$discount_type = get_post_meta($post->ID, 'discount_type', true); |
| 68 |
if (!empty($discount_types[ $discount_type ])) { |
| 69 |
$coupon_list[$post->post_title] = $post->post_title . ' (Type: ' . $discount_types[ $discount_type ] . ')'; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
wp_send_json($coupon_list); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Set coupon |
| 79 |
* |
| 80 |
* @param Array $coupons |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function set_coupon($coupons) |
| 85 |
{ |
| 86 |
if ($coupons) { |
| 87 |
$discount_types = wc_get_coupon_types(); |
| 88 |
foreach ($coupons as $coupon_title) { |
| 89 |
$coupon = new \WC_Coupon($coupon_title); |
| 90 |
$discount_type = $coupon->get_discount_type(); |
| 91 |
if (isset($discount_type) && $discount_type) { |
| 92 |
$discount_type = ' ( Type: ' . $discount_types[ $discount_type ] . ' )'; |
| 93 |
} |
| 94 |
$this->coupons[$coupon_title] = $coupon_title . $discount_type; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get coupon |
| 101 |
* |
| 102 |
* @return String |
| 103 |
*/ |
| 104 |
public function get_coupons() |
| 105 |
{ |
| 106 |
return $this->coupons; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get module name |
| 111 |
* |
| 112 |
* @return String |
| 113 |
*/ |
| 114 |
public function get_name() |
| 115 |
{ |
| 116 |
return __('discount','wpfnl'); |
| 117 |
} |
| 118 |
} |
| 119 |
|