PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / GlobalTieredPricing / LookupService.php

LookupService.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/GlobalTieredPricing/LookupService.php

117 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\GlobalTieredPricing;
2
3 use Exception;
4 use TierPricingTable\Core\ServiceContainerTrait;
5 use WC_Customer;
6 use WP_Term;
7 use WP_User_Query;
8
9 class LookupService {
10
11 use ServiceContainerTrait;
12
13 const CATEGORIES_SEARCH_ACTION = 'woocommerce_json_search_tpt_categories';
14 const CUSTOMERS_SEARCH_ACTION = 'woocommerce_json_search_tpt_customers';
15
16 public function __construct() {
17 add_action( 'wp_ajax_' . self::CATEGORIES_SEARCH_ACTION, array( $this, 'categoriesSearchHandler' ) );
18 add_action( 'wp_ajax_' . self::CUSTOMERS_SEARCH_ACTION, array( $this, 'customersSearchHandler' ) );
19 }
20
21 public function customersSearchHandler() {
22
23 if ( ! current_user_can( 'manage_options' ) ) {
24 wp_send_json( array() );
25 }
26
27 $term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false;
28
29 if ( $term ) {
30 $wp_user_query = new WP_User_Query(
31 array(
32 'search' => '*' . $term . '*',
33 'search_columns' => array(
34 'user_login',
35 'user_nicename',
36 'user_email',
37 ),
38 'fields' => 'ID',
39 ) );
40
41 $users = $wp_user_query->get_results();
42
43 if ( $users ) {
44 $_users = array();
45
46 foreach ( $users as $userId ) {
47 try {
48 $customer = new WC_Customer( $userId );
49 } catch ( Exception $e ) {
50 continue;
51 }
52
53 if ( $customer instanceof WC_Customer ) {
54 $_users[ $userId ] = Formatter::formatCustomerString( $customer );
55 }
56
57 }
58
59 wp_send_json( $_users );
60 }
61 }
62
63 wp_send_json( array() );
64 }
65
66 public function categoriesSearchHandler() {
67
68 if ( ! current_user_can( 'manage_options' ) ) {
69 wp_send_json( array() );
70 }
71
72 $term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false;
73
74 if ( $term ) {
75 $args = array(
76 'taxonomy' => array( 'product_cat' ),
77 'orderby' => 'id',
78 'order' => 'ASC',
79 'limit' => 5,
80 'hide_empty' => false,
81 'fields' => 'all',
82 'name__like' => $term
83 );
84
85 $terms = get_terms( $args );
86
87 if ( $terms ) {
88 $_terms = array();
89
90 foreach ( $terms as $term ) {
91 if ( $term instanceof WP_Term ) {
92 $_terms[ $term->term_id ] = self::getCategoryLabel( $term );
93 }
94 }
95
96 wp_send_json( $_terms );
97 }
98 }
99
100 wp_send_json( array() );
101 }
102
103 public static function getCategoryLabel( WP_Term $category ): string {
104 $parentTermName = '';
105
106 if ( $category->parent ) {
107 $parentTerm = get_term( $category->parent );
108
109 if ( $parentTerm ) {
110 $parentTermName = ' (' . $parentTerm->name . ')';
111 }
112 }
113
114 return $category->name . $parentTermName;
115 }
116 }
117