| @@ -1,0 +1,66 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * WooCommerce Extension | |
| 4 | + * | |
| 5 | + * @package NotificationX\Extensions | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace NotificationX\Extensions\WooCommerce; | |
| 9 | + | |
| 10 | +use NotificationX\Core\Helper; | |
| 11 | +use NotificationX\Core\Rules; | |
| 12 | +use NotificationX\Extensions\GlobalFields; | |
| 13 | + | |
| 14 | +trait Woo { | |
| 15 | + public $post_type = 'product'; | |
| 16 | + | |
| 17 | + | |
| 18 | + public function _init_fields(){ | |
| 19 | + add_filter('nx_conversion_product_list', [$this, 'products']); | |
| 20 | + add_filter('nx_conversion_category_list', [$this, 'categories']); | |
| 21 | + } | |
| 22 | + | |
| 23 | + | |
| 24 | + public function categories($options){ | |
| 25 | + | |
| 26 | + $product_categories = get_terms(array( | |
| 27 | + 'taxonomy' => 'product_cat', | |
| 28 | + 'hide_empty' => false, | |
| 29 | + )); | |
| 30 | + | |
| 31 | + $category_list = []; | |
| 32 | + | |
| 33 | + if( ! is_wp_error( $product_categories ) ) { | |
| 34 | + foreach( $product_categories as $product ) { | |
| 35 | + $category_list[ $product->slug ] = $product->name; | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + $options = GlobalFields::get_instance()->normalize_fields($category_list, 'source', $this->id, $options); | |
| 40 | + return $options; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function products($options){ | |
| 44 | + $product_list = Helper::get_post_titles_by_search($this->post_type); | |
| 45 | + $options = GlobalFields::get_instance()->normalize_fields($product_list, 'source', $this->id, $options); | |
| 46 | + return $options; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Lists available tags in the selected form. | |
| 51 | + * | |
| 52 | + * @param array $args An array of arguments, including inputValue. | |
| 53 | + * @return array An indexed array of product IDs and titles. | |
| 54 | + */ | |
| 55 | + public function restResponse($args) { | |
| 56 | + // Check if inputValue is provided | |
| 57 | + if ( empty( $args['search_empty']) && empty($args['inputValue'] ) ) { | |
| 58 | + return []; | |
| 59 | + } | |
| 60 | + // Get the products that match the inputValue | |
| 61 | + $products = Helper::get_post_titles_by_search($this->post_type, $args['inputValue']); | |
| 62 | + // Normalize the fields and return as an indexed array | |
| 63 | + return array_values(GlobalFields::get_instance()->normalize_fields($products, 'source', $this->id)); | |
| 64 | + } | |
| 65 | + | |
| 66 | +} | |