PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / admin / modules / product / class-wpfnl-product.php

class-wpfnl-product.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at admin/modules/product/class-wpfnl-product.php

323 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Product module
4 *
5 * @package
6 */
7 namespace WPFunnels\Modules\Admin\Product;
8
9 use WPFunnels\Admin\Module\Wpfnl_Admin_Module;
10 use WPFunnels\Traits\SingletonTrait;
11 use WPFunnels\Wpfnl_functions;
12 use \WC_Subscriptions_Product;
13 use Wpfnl_Type_Factory;
14 class Module extends Wpfnl_Admin_Module
15 {
16 use SingletonTrait;
17
18 protected $products = [];
19
20 public function get_view()
21 {
22 require_once WPFNL_DIR . '/admin/modules/product/views/view.php';
23 }
24
25
26 public function set_products($products)
27 {
28 if (is_plugin_active('woocommerce/woocommerce.php')) {
29 if ($products) {
30 foreach ($products as $saved_product) {
31 $product = wc_get_product($saved_product['id']);
32 if (is_object($product)) {
33 $product_name = $product->get_name();
34 $this->products[] = [
35 'id' => $saved_product['id'],
36 'name' => $product_name,
37 'quantity' => $saved_product['quantity'],
38 ];
39 }
40 }
41 }
42 }
43 }
44
45 public function get_products()
46 {
47 return $this->products;
48 }
49
50 public function init_ajax()
51 {
52 add_action('wp_ajax_wpfnl_product_search', [ $this, 'fetch_products' ]);
53 add_action('wp_ajax_wpfnl_search_coupon', [ $this, 'fetch_coupons' ]);
54 add_action('wp_ajax_wpfnl_product_search_gbf', [ $this, 'fetch_products_gbf' ]);
55 add_action('wp_ajax_wpfnl_product_search_for_gbf_type', [ $this, 'fetch_specific_products_gbf' ]);
56 add_action('wp_ajax_wpfnl_product_search_by_category_and_name', [ $this, 'wpfnl_product_search_by_category_and_name' ]);
57
58 wp_ajax_helper()->handle('global-funnel-add-upsell-product')
59 ->with_callback([ $this, 'global_funnel_add_upsell_product' ])
60 ->with_validation($this->get_validation_data());
61 }
62
63 /**
64 * Fetch product from WC data store
65 *
66 * @throws \Exception
67 * @since 1.0.0
68 */
69 public function fetch_products()
70 {
71 check_ajax_referer('wpfnl-admin', 'security');
72 if (isset($_GET['term'])) {
73 $term = (string) esc_attr( wp_unslash($_GET['term']) );
74 }
75 if (empty($term)) {
76 wp_die();
77 }
78 $products = [];
79 if( $_GET['isLms'] == 'true' ){
80 $_class = 'lms';
81 }else{
82 $_class = 'wc';
83 }
84 $class_object = Wpfnl_Type_Factory::build($_class);
85 if( $class_object ){
86 $products = $class_object->retrieve_item( $term );
87 }
88 wp_send_json($products);
89 }
90
91
92 /**
93 * Fetch Coupons from WooCommerce or LMS data store via AJAX.
94 *
95 * This function handles the AJAX request to fetch coupons from either the WooCommerce (wc)
96 * or Learning Management System (LMS) data store based on the provided term. It performs security checks,
97 * processes the term, retrieves relevant coupons, and sends back the JSON response with the coupon data.
98 *
99 * @since 2.8.6
100 */
101 public function fetch_coupons()
102 {
103 // Verify the security nonce.
104 check_ajax_referer('wpfnl-admin', 'security');
105
106 // Check if the 'term' parameter is set in the request.
107 if (!isset($_GET['term'])) {
108 wp_die();
109 }
110
111 // Sanitize and process the term parameter.
112 $term = (string) esc_attr(wp_unslash($_GET['term']));
113
114 // Check if the term is empty.
115 if (empty($term)) {
116 wp_die();
117 }
118
119 $coupons = [];
120
121 // Determine the class based on the 'isLms' parameter.
122 if (sanitize_text_field($_GET['isLms']) == 'true') {
123 $_class = 'lms';
124 } else {
125 $_class = 'wc';
126 }
127
128 // Build the appropriate class object based on the determined class.
129 $class_object = Wpfnl_Type_Factory::build($_class);
130
131 // If the class object is not available, terminate.
132 if (!$class_object) {
133 wp_die();
134 }
135
136 // Retrieve coupons using the appropriate class object.
137 $coupons = $class_object->retrieve_coupon($term);
138
139 // Send JSON response with the fetched coupons.
140 wp_send_json($coupons);
141 }
142
143
144 /**
145 * Fetch product from WC data store
146 *
147 * @throws \Exception
148 * @since 1.0.0
149 */
150 public function fetch_products_gbf()
151 {
152 check_ajax_referer('wpfnl-admin', 'security');
153 if (isset($_GET['term'])) {
154 $term = (string) esc_attr( wp_unslash($_GET['term']) );
155 }
156 if (empty($term)) {
157 wp_die();
158 }
159
160 $products = [];
161 $data_store = \WC_Data_Store::load('product');
162 $ids = $data_store->search_products($term, '', false, false, 10);
163
164 $product_objects = array_filter(array_map('wc_get_product', $ids), 'wc_products_array_filter_readable');
165 if( is_array($product_objects) ){
166 foreach ($product_objects as $product_object) {
167
168 if( $product_object && (( $product_object->managing_stock() && $product_object->get_stock_quantity() > 0 ) || ( !$product_object->managing_stock() && $product_object->get_stock_status() !== 'outofstock' )) ){
169 $formatted_name = $product_object->get_name();
170 if($product_object->get_type() == 'variable' || $product_object->get_type() == 'variable-subscription') {
171 if( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ){
172 $signUpFee = \WC_Subscriptions_Product::get_sign_up_fee( $product_object );
173 }
174 $variations = $product_object->get_available_variations();
175 $isPro = Wpfnl_functions::is_wpfnl_pro_activated();
176 if($isPro){
177 $parent_id = $product_object->get_id();
178 $products[] = [
179 'name' => $formatted_name,
180 'id' => $parent_id,
181 'regular_price' => $product_object->get_regular_price(),
182 'sale_price' => $product_object->get_sale_price(),
183 ];
184 }
185 if( is_array($variations ) ){
186 foreach ($variations as $variation) {
187 $variation_product = wc_get_product($variation['variation_id']);
188 if( $variation_product ){
189 if( ( $variation_product->managing_stock() && $variation_product->get_stock_quantity() > 0 ) || ( !$variation_product->managing_stock() && $variation_product->get_stock_status() !== 'outofstock' ) ){
190 $products[] = [
191
192 'name' => Wpfnl_functions::get_formated_product_name( $variation_product ),
193 'id' => $variation['variation_id'],
194 'regular_price' => $variation['display_price'],
195 'sale_price' => $variation['display_regular_price'],
196 ];
197 }
198 }
199 }
200 }
201
202 }else {
203 $products[] = [
204 'name' => rawurldecode($formatted_name),
205 'id' => $product_object->get_id(),
206 'regular_price' => $product_object->get_regular_price(),
207 'sale_price' => $product_object->get_sale_price(),
208 ];
209 }
210 }
211 }
212 }
213
214 wp_send_json($products);
215 }
216
217 /**
218 * Fetch specific product from WC data store for gbf type
219 *
220 * @return [type]
221 */
222 public function fetch_specific_products_gbf()
223 {
224 check_ajax_referer('wpfnl-admin', 'security');
225 if (isset($_GET['term'])) {
226 $term = (string) esc_attr( wp_unslash($_GET['term']) );
227 }
228 if (empty($term)) {
229 wp_die();
230 }
231
232 $products = [];
233 $data_store = \WC_Data_Store::load('product');
234 $ids = $data_store->search_products($term, '', false, false, 10);
235
236 $product_objects = array_filter(array_map('wc_get_product', $ids), 'wc_products_array_filter_readable');
237 if( $product_objects ){
238 foreach ($product_objects as $product_object) {
239 if( ( $product_object->managing_stock() && $product_object->get_stock_quantity() > 0 ) || ( !$product_object->managing_stock() && $product_object->get_stock_status() !== 'outofstock' ) ){
240 $formatted_name = $product_object->get_name();
241 if($product_object->get_type() == 'variable' || $product_object->get_type() == 'variable-subscription') {
242 if( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ){
243 $signUpFee = \WC_Subscriptions_Product::get_sign_up_fee( $product_object );
244 }
245 $variations = $product_object->get_available_variations();
246 $parent_id = $product_object->get_id();
247 $products[] = [
248 'name' => $formatted_name,
249 'id' => $parent_id,
250 'regular_price' => $product_object->get_regular_price(),
251 'sale_price' => $product_object->get_sale_price(),
252 ];
253 if( !empty($variations) ){
254 foreach ($variations as $variation) {
255 $variation_product = wc_get_product($variation['variation_id']);
256 if( $variation_product ){
257 if( ( $variation_product->managing_stock() && $variation_product->get_stock_quantity() > 0 ) || ( !$variation_product->managing_stock() && $variation_product->get_stock_status() !== 'outofstock' ) ){
258 $products[] = [
259
260 'name' => Wpfnl_functions::get_formated_product_name( $variation_product ),
261 'id' => $variation['variation_id'],
262 'regular_price' => $variation['display_price'],
263 'sale_price' => $variation['display_regular_price'],
264 ];
265 }
266 }
267 }
268 }
269
270 }else {
271 $products[] = [
272 'name' => rawurldecode($formatted_name),
273 'id' => $product_object->get_id(),
274 'regular_price' => $product_object->get_regular_price(),
275 'sale_price' => $product_object->get_sale_price(),
276 ];
277 }
278 }
279 }
280 }
281 wp_send_json($products);
282 }
283
284 public function get_name()
285 {
286 return 'product';
287 }
288
289 /**
290 * Wpfnl_product_search_by_category_and_name
291 */
292 public function wpfnl_product_search_by_category_and_name(){
293 check_ajax_referer('wpfnl-admin', 'security');
294 $category_id = $_GET['category'];
295 $product_name = $_GET['term'];
296 $args = array(
297 'post_status' => 'publish',
298 'posts_per_page' => -1,
299 'post_type' => ['product','product_variation'],
300 's' => $product_name,
301 );
302 $products = get_posts($args);
303 if(empty($products)){
304 $data = [
305 'status' => 'success',
306 'data' => 'Product not found',
307 ];
308 }else{
309 foreach($products as $key => $product){
310 $_product = wc_get_product($product->ID);
311 if( $_product ){
312 $products[$key]->price = $_product->get_price();
313 }
314 }
315 $data = [
316 'status' => 'success',
317 'data' => $products,
318 ];
319 }
320 wp_send_json($data);
321 }
322 }
323