PluginProbe
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export / trunk
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export vtrunk
3.0 2.24.2 2.24.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 2.0 2.0.1 2.1 2.1.1 2.1.2 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.16.1 2.16.2 All 96 releases
wp-ultimate-exporter / exportExtensions / SureCartExport.php

SureCartExport.php in Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export trunk, at exportExtensions/SureCartExport.php

665 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /******************************************************************************************
4 * Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License
5 * Unauthorized copying of this file, via any medium is strictly prohibited
6 * Proprietary and confidential
7 * You can contact Smackcoders at email address info@smackcoders.com.
8 *******************************************************************************************/
9
10 namespace Smackcoders\SMEXP;
11
12 if (!defined('ABSPATH'))
13 exit; // Exit if accessed directly
14
15 /**
16 * Class SureCartExport
17 * Handles all SureCart export functionality
18 * @package Smackcoders\WCSV
19 */
20 class SureCartExport
21 {
22 protected static $instance = null, $export_instance;
23 public $totalRowCount;
24 public $plugin;
25
26 public static function getInstance()
27 {
28 if (null == self::$instance) {
29 self::$instance = new self;
30 SureCartExport::$export_instance = ExportExtension::getInstance();
31 }
32 return self::$instance;
33 }
34
35 /**
36 * SureCartExport constructor.
37 */
38 public function __construct()
39 {
40 $this->plugin = Plugin::getInstance();
41 }
42
43 /**
44 * Export SureCart Products data
45 *
46 * @param int $id Product ID
47 * @return void
48 */
49 public function getSureCartProductDataMaster($id)
50 {
51 global $wpdb;
52
53 if (!is_plugin_active('surecart/surecart.php')) {
54 return;
55 }
56
57 $post_type = $wpdb->get_var(
58 $wpdb->prepare(
59 "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d",
60 $id
61 )
62 );
63
64 if ($post_type !== 'sc_product') {
65 return;
66 }
67
68
69 // Get product meta
70 $sku = get_post_meta($id, 'sku', true);
71 $price = get_post_meta($id, 'price', true);
72 if (empty($price))
73 $price = get_post_meta($id, 'min_price_amount', true); // Fallback
74 $sale_price = get_post_meta($id, 'sale_price', true);
75 if (empty($sale_price))
76 $sale_price = get_post_meta($id, 'scratch_display_amount', true); // Fallback
77
78 $stock_enabled = get_post_meta($id, 'stock_enabled', true);
79 $stock_quantity = get_post_meta($id, 'available_stock', true); // matched debug
80 $allow_purchase_out_of_stock = get_post_meta($id, 'allow_out_of_stock_purchases', true); // matched debug
81 $tax_enabled = get_post_meta($id, 'tax_enabled', true);
82 $tax_status = get_post_meta($id, 'tax_status', true);
83 $product_type = get_post_meta($id, 'product_type', true);
84 $recurring_interval = get_post_meta($id, 'recurring_interval', true);
85 $recurring_period = get_post_meta($id, 'recurring_period', true);
86 $recurring_price = get_post_meta($id, 'recurring_price', true);
87 $variants = get_post_meta($id, 'variants', true);
88 $download_files = get_post_meta($id, 'download_files', true);
89
90 // Featured image
91 $thumbnail_id = (int) get_post_thumbnail_id($id);
92 $featured_image_url = $thumbnail_id ? wp_get_attachment_url($thumbnail_id) : '';
93
94 // Gallery images
95 $gallery_ids = get_post_meta($id, 'gallery_images', true);
96 $gallery_urls = [];
97 if (!empty($gallery_ids) && is_array($gallery_ids)) {
98 foreach ($gallery_ids as $gallery_id) {
99 $gallery_url = wp_get_attachment_url($gallery_id);
100 if ($gallery_url) {
101 $gallery_urls[] = $gallery_url;
102 }
103 }
104 }
105
106 // Product categories and tags
107 $categories = wp_get_post_terms($id, 'sc_product_category', ['fields' => 'names']);
108 if (is_wp_error($categories))
109 $categories = [];
110 $tags = wp_get_post_terms($id, 'sc_product_tag', ['fields' => 'names']);
111 if (is_wp_error($tags))
112 $tags = [];
113
114 // Format download files
115 $download_files_output = '';
116 if (!empty($download_files) && is_array($download_files)) {
117 foreach ($download_files as $file) {
118 if (!empty($file['file'])) {
119 $download_files_output .=
120 'file_name:' . ($file['name'] ?? '') .
121 ',file_url:' . ($file['file'] ?? '') . ' | ';
122 }
123 }
124 $download_files_output = rtrim($download_files_output, ' | ');
125 }
126
127 // Format variants
128 $variants_output = '';
129 if (!empty($variants) && is_array($variants)) {
130 $variants_output = json_encode($variants);
131 }
132
133 // Get all custom meta
134 $all_meta = get_post_meta($id);
135 $custom_meta = [];
136 foreach ($all_meta as $key => $values) {
137 // Skip internal keys and already processed keys
138 if (strpos($key, '_') === 0)
139 continue;
140
141 if (
142 !in_array($key, [
143 'sku',
144 'price',
145 'min_price_amount',
146 'max_price_amount',
147 'display_amount',
148 'sale_price',
149 'scratch_display_amount',
150 'range_display_amount',
151 'stock_enabled',
152 'available_stock',
153 'stock_quantity',
154 'allow_out_of_stock_purchases',
155 'purchase_limit',
156 'tax_enabled',
157 'tax_status',
158 'shipping_enabled',
159 'product_type',
160 'recurring',
161 'recurring_interval',
162 'recurring_period',
163 'recurring_price',
164 'variants',
165 'download_files',
166 'gallery_images',
167 'sc_id',
168 'product'
169 ])
170 ) {
171 $custom_meta[$key] = is_array($values) ? $values[0] : $values;
172 }
173 }
174
175 $product_meta_output = '';
176 if (!empty($custom_meta)) {
177 foreach ($custom_meta as $key => $value) {
178 $product_meta_output .= $key . ':' . (is_array($value) ? json_encode($value) : $value) . '|';
179 }
180 $product_meta_output = rtrim($product_meta_output, '|');
181 }
182
183 // Set export data
184 SureCartExport::$export_instance->data[$id]['product_id'] = $id;
185 SureCartExport::$export_instance->data[$id]['product_name'] = get_the_title($id);
186 SureCartExport::$export_instance->data[$id]['post_title'] = get_the_title($id);
187 SureCartExport::$export_instance->data[$id]['post_content'] = get_post_field('post_content', $id);
188 SureCartExport::$export_instance->data[$id]['post_excerpt'] = get_post_field('post_excerpt', $id);
189 SureCartExport::$export_instance->data[$id]['post_status'] = get_post_status($id);
190 SureCartExport::$export_instance->data[$id]['sku'] = $sku ?: '';
191 SureCartExport::$export_instance->data[$id]['price'] = $price ?: '';
192 SureCartExport::$export_instance->data[$id]['sale_price'] = $sale_price ?: '';
193 SureCartExport::$export_instance->data[$id]['stock_enabled'] = $stock_enabled ? 'yes' : 'no';
194 SureCartExport::$export_instance->data[$id]['stock_quantity'] = $stock_quantity ?: 0;
195 SureCartExport::$export_instance->data[$id]['allow_purchase_out_of_stock'] = $allow_purchase_out_of_stock ? 'yes' : 'no';
196 SureCartExport::$export_instance->data[$id]['tax_enabled'] = $tax_enabled ? 'yes' : 'no';
197 SureCartExport::$export_instance->data[$id]['tax_status'] = $tax_status ?: '';
198 SureCartExport::$export_instance->data[$id]['featured_image'] = $featured_image_url;
199 SureCartExport::$export_instance->data[$id]['gallery_images'] = implode(',', $gallery_urls);
200 SureCartExport::$export_instance->data[$id]['product_type'] = $product_type ?: '';
201 SureCartExport::$export_instance->data[$id]['recurring_interval'] = $recurring_interval ?: '';
202 SureCartExport::$export_instance->data[$id]['recurring_period'] = $recurring_period ?: '';
203 SureCartExport::$export_instance->data[$id]['recurring_price'] = $recurring_price ?: '';
204 SureCartExport::$export_instance->data[$id]['variants'] = $variants_output;
205 SureCartExport::$export_instance->data[$id]['product_categories'] = !empty($categories) ? implode(',', $categories) : '';
206 SureCartExport::$export_instance->data[$id]['product_tags'] = !empty($tags) ? implode(',', $tags) : '';
207 SureCartExport::$export_instance->data[$id]['download_files'] = $download_files_output;
208 SureCartExport::$export_instance->data[$id]['product_meta'] = $product_meta_output;
209 }
210
211
212 /**
213 * Export SureCart Customers data
214 *
215 * @param int $id Customer ID
216 * @return void
217 */
218 public function getSureCartCustomerDataMaster($id)
219 {
220 if (!is_plugin_active('surecart/surecart.php') || !class_exists('\\SureCart\\Models\\Customer')) {
221 return;
222 }
223
224 // Support both cloud UUID (from PostExport) and WP post ID (from WPQueryExport)
225 $lookup_id = $id;
226 if (is_numeric($id) && (int) $id === (float) $id && $id < 2147483647) {
227 $sc_id = get_post_meta($id, 'sc_id', true);
228 if ($sc_id) {
229 $lookup_id = $sc_id;
230 }
231 }
232
233 $customer = \SureCart\Models\Customer::find($lookup_id);
234 // Handle WP_Error or empty result - try post meta sc_id if first find failed
235 if ((is_wp_error($customer) || !$customer) && $lookup_id === $id) {
236 $sc_id = get_post_meta($id, 'sc_id', true);
237 if ($sc_id) {
238 $customer = \SureCart\Models\Customer::find($sc_id);
239 if ($customer) {
240 $lookup_id = $sc_id;
241 }
242 }
243 }
244
245 // Use meta fallback if still no valid customer object
246 $use_meta_fallback = false;
247 if (is_wp_error($customer) || !$customer) {
248 $use_meta_fallback = true;
249 }
250 $purchase_count = 0;
251 $lifetime_value = 0;
252 $order_ids = '';
253 $notes = '';
254
255 if (!$use_meta_fallback) {
256 // Get customer data from Model
257 $customer_email = $customer->email ?? '';
258 $customer_name = $customer->name ?? '';
259 $first_name = $customer->first_name ?? '';
260 $last_name = $customer->last_name ?? '';
261 $user_id = $customer->wp_user_id ?? 0;
262 $phone_number = $customer->phone ?? $customer->phone_number ?? '';
263 $date_created = $customer->created_at ?? '';
264
265 // Billing Address (Customer level)
266 $billing_address = $customer->billing_address ?? [];
267 if (is_object($billing_address) && method_exists($billing_address, 'toArray')) {
268 $billing_address = $billing_address->toArray();
269 } elseif (is_object($billing_address)) {
270 $billing_address = json_decode(wp_json_encode($billing_address), true);
271 } else {
272 $billing_address = (array) $billing_address;
273 }
274
275 // Metadata
276 $metadata = (array) ($customer->metadata ?? []);
277
278 // Notes from customer
279 $notes = $customer->notes ?? '';
280 if (is_array($notes)) {
281 $notes = implode("\n", $notes);
282 }
283
284 // Fetch purchase_count, lifetime_value, order_ids from Order API
285 if (class_exists('\\SureCart\\Models\\Order') && !empty($customer->id)) {
286 try {
287 $orders = \SureCart\Models\Order::where(['customer_id' => $customer->id, 'limit' => 500])->get();
288 if (!empty($orders)) {
289 $order_id_list = [];
290 $total = 0;
291 foreach ($orders as $o) {
292 $order_id_list[] = $o->id;
293 $total += (int) ($o->total_amount ?? 0);
294 }
295 $purchase_count = count($order_id_list);
296 $lifetime_value = $total / 100; // API amounts are typically in cents
297 $order_ids = implode(',', $order_id_list);
298 }
299 } catch (\Throwable $e) {
300 }
301 }
302 } else {
303 // Fallback to Post Meta
304 $customer_email = get_post_meta($id, 'sc_customer_email', true);
305 $first_name = get_post_meta($id, 'sc_first_name', true);
306 $last_name = get_post_meta($id, 'sc_last_name', true);
307 $customer_name = $first_name . ' ' . $last_name;
308 if (empty(trim($customer_name))) {
309 $customer_name = get_post_meta($id, 'sc_customer_name', true);
310 }
311 if (empty(trim($customer_name))) {
312 $customer_name = get_the_title($id);
313 }
314
315 // User ID Lookup
316 $user_id = get_post_meta($id, 'sc_wp_user_id', true);
317 if (empty($user_id) && !empty($customer_email)) {
318 $user = get_user_by('email', $customer_email);
319 if ($user) {
320 $user_id = $user->ID;
321 }
322 }
323 if (!$user_id)
324 $user_id = 0;
325
326 $phone_number = get_post_meta($id, 'sc_phone_number', true);
327 $date_created = get_the_date('Y-m-d H:i:s', $id);
328
329 // Construct billing address from meta
330 // 1. Try generic array field
331 $meta_billing = get_post_meta($id, 'sc_billing_address', true);
332 if (is_array($meta_billing)) {
333 $billing_address = [
334 'address_1' => $meta_billing['line_1'] ?? $meta_billing['address_1'] ?? '',
335 'address_2' => $meta_billing['line_2'] ?? $meta_billing['address_2'] ?? '',
336 'city' => $meta_billing['city'] ?? '',
337 'state' => $meta_billing['state'] ?? '',
338 'postal_code' => $meta_billing['postal_code'] ?? '',
339 'country' => $meta_billing['country'] ?? '',
340 ];
341 } else {
342 // 2. Try individual fields
343 $billing_address = [
344 'address_1' => get_post_meta($id, 'sc_billing_address_line_1', true),
345 'address_2' => get_post_meta($id, 'sc_billing_address_line_2', true),
346 'city' => get_post_meta($id, 'sc_billing_city', true),
347 'state' => '',
348 'postal_code' => '',
349 'country' => get_post_meta($id, 'sc_billing_country', true),
350 ];
351 }
352
353 // Metadata (raw or processed?)
354 $metadata = [];
355 // Maybe sc_metadata?
356 $meta_json = get_post_meta($id, 'sc_metadata', true);
357 if ($meta_json) {
358 if (is_array($meta_json))
359 $metadata = $meta_json;
360 else
361 $metadata = json_decode($meta_json, true) ?: [];
362 }
363
364 $notes = get_post_meta($id, 'sc_notes', true) ?: '';
365 }
366
367
368 $billing_address_line_1 = $billing_address['line_1'] ?? $billing_address['address_1'] ?? '';
369 $billing_address_line_2 = $billing_address['line_2'] ?? $billing_address['address_2'] ?? '';
370 $billing_city = $billing_address['city'] ?? '';
371 $billing_state = $billing_address['state'] ?? '';
372 $billing_postal_code = $billing_address['postal_code'] ?? '';
373 $billing_country = $billing_address['country'] ?? '';
374
375 $user_login = '';
376 $user_email = '';
377 if ($user_id > 0) {
378 $user_data = get_userdata($user_id);
379 if ($user_data) {
380 $user_login = $user_data->user_login;
381 $user_email = $user_data->user_email;
382 }
383 }
384
385 // Set export data
386 $customer_sc_id = !$use_meta_fallback ? ($customer->id ?? '') : get_post_meta($id, 'sc_id', true);
387 SureCartExport::$export_instance->data[$id]['sc_id'] = $customer_sc_id ?: '';
388 SureCartExport::$export_instance->data[$id]['customer_id'] = $id;
389 SureCartExport::$export_instance->data[$id]['customer_email'] = $customer_email ?: '';
390 SureCartExport::$export_instance->data[$id]['customer_name'] = $customer_name ?: '';
391 SureCartExport::$export_instance->data[$id]['first_name'] = $first_name ?: '';
392 SureCartExport::$export_instance->data[$id]['last_name'] = $last_name ?: '';
393 SureCartExport::$export_instance->data[$id]['user_id'] = $user_id ?: 0;
394 SureCartExport::$export_instance->data[$id]['user_login'] = $user_login;
395 SureCartExport::$export_instance->data[$id]['user_email'] = $user_email;
396 SureCartExport::$export_instance->data[$id]['phone_number'] = $phone_number ?: '';
397 SureCartExport::$export_instance->data[$id]['date_created'] = $date_created;
398 SureCartExport::$export_instance->data[$id]['billing_address_line_1'] = $billing_address_line_1 ?: '';
399 SureCartExport::$export_instance->data[$id]['billing_address_line_2'] = $billing_address_line_2 ?: '';
400 SureCartExport::$export_instance->data[$id]['billing_city'] = $billing_city ?: '';
401 SureCartExport::$export_instance->data[$id]['billing_state'] = $billing_state ?: '';
402 SureCartExport::$export_instance->data[$id]['billing_postal_code'] = $billing_postal_code ?: '';
403 SureCartExport::$export_instance->data[$id]['billing_country'] = $billing_country ?: '';
404 SureCartExport::$export_instance->data[$id]['purchase_count'] = $purchase_count;
405 SureCartExport::$export_instance->data[$id]['lifetime_value'] = $lifetime_value;
406 SureCartExport::$export_instance->data[$id]['order_ids'] = $order_ids ?: '';
407 SureCartExport::$export_instance->data[$id]['notes'] = $notes ?: '';
408
409 $metadata_output = '';
410 if (!empty($metadata)) {
411 foreach ($metadata as $key => $value) {
412 $metadata_output .= $key . ':' . (is_array($value) ? json_encode($value) : $value) . '|';
413 }
414 }
415 SureCartExport::$export_instance->data[$id]['customer_meta'] = rtrim($metadata_output, '|');
416 }
417
418 /**
419 * Export SureCart Coupons data
420 *
421 * @param int $id Coupon ID
422 * @return void
423 */
424 public function getSureCartCouponDataMaster($id)
425 {
426 // 1. Determine if $id is a WP Post ID or a SureCart UUID
427 $is_uuid = !is_numeric($id) && strlen($id) > 10; // Simple heuristic for UUID
428
429 $coupon_code = '';
430 $coupon_model = null;
431
432 if (!$is_uuid) {
433 // It's a WP Post ID (Imported Coupon)
434 $coupon_code = get_post_meta($id, 'sc_coupon_code', true);
435
436 // Try getting model via meta
437 if (is_plugin_active('surecart/surecart.php') && class_exists('\\SureCart\\Models\\Coupon')) {
438 $sc_id = get_post_meta($id, 'sc_id', true);
439 if (!empty($sc_id)) {
440 try {
441 $coupon_model = \SureCart\Models\Coupon::find($sc_id);
442 } catch (\Exception $e) { /* ignore */
443 }
444 }
445 }
446 } else {
447 // It's a SureCart UUID (Native Coupon exported via custom flow?)
448 // We cannot use get_post_meta on a UUID.
449 if (is_plugin_active('surecart/surecart.php') && class_exists('\\SureCart\\Models\\Coupon')) {
450 try {
451 $coupon_model = \SureCart\Models\Coupon::find($id);
452 } catch (\Exception $e) { /* ignore */
453 }
454 }
455 }
456
457 if (empty($coupon_code)) {
458 if ($coupon_model) {
459 $coupon_code = $coupon_model->code;
460 } else {
461 // Fallback to title
462 $post = get_post($id);
463 if ($post) {
464 $coupon_code = $post->post_title;
465 }
466 }
467 }
468
469
470 // Define mapping: Export Key => [Meta Key, Model Property]
471 $field_map = [
472 'promotion_codes' => ['sc_promotion_code', 'promotion_code'], // New field
473 'discount_type' => ['sc_discount_type', 'discount_type'],
474 'discount_amount' => ['sc_discount_amount', 'amount'],
475 'status' => ['sc_status', 'status'],
476 'usage_limit' => ['sc_usage_limit', 'max_redemptions'],
477 'usage_count' => ['sc_usage_count', 'times_redeemed'],
478 'usage_limit_per_user' => ['sc_usage_limit_per_user', 'max_redemptions_per_customer'],
479 'start_date' => ['sc_start_date', 'start_date'],
480 'end_date' => ['sc_end_date', 'end_date'],
481 'minimum_amount' => ['sc_minimum_amount', 'min_subtotal_amount'], // FIXED: min_amount -> min_subtotal_amount
482 'maximum_amount' => ['sc_maximum_amount', 'max_subtotal_amount'], // FIXED: max_amount -> max_subtotal_amount
483 'applies_to' => ['sc_applies_to', 'applies_to'],
484 'duration' => ['sc_duration', 'duration'],
485 'duration_in_months' => ['sc_duration_in_months', 'duration_in_months'],
486 'currency' => ['sc_currency', 'currency'],
487 'archived' => ['sc_archived', 'archived'],
488 // Arrays need special handling
489 'product_requirements' => ['sc_product_ids', 'product_ids'],
490 'excluded_products' => ['sc_excluded_product_ids', 'excluded_product_ids'],
491 'category_requirements' => ['sc_category_ids', 'category_ids'],
492 'excluded_categories' => ['sc_excluded_category_ids', 'excluded_category_ids']
493 ];
494
495 // Initialize data array
496 SureCartExport::$export_instance->data[$id]['coupon_id'] = $id;
497
498 // 1. Code
499 $code_val = '';
500 if (!$is_uuid) {
501 $code_val = get_post_meta($id, 'sc_coupon_code', true);
502 }
503 if (empty($code_val) && $coupon_model) {
504 $code_val = $coupon_model->code ?? $coupon_model->promotion_code ?? $coupon_model->name;
505 }
506 if (empty($code_val) && !$is_uuid) {
507 $post = get_post($id);
508 if ($post)
509 $code_val = $post->post_title;
510 }
511 SureCartExport::$export_instance->data[$id]['coupon_code'] = $code_val;
512
513 // NEW: Promotion Codes (if distinct from coupon_code)
514 // Usually coupon_code IS the promotion code. But if user requests it separate:
515 $promo_codes_val = '';
516 if (!$is_uuid) {
517 $promo_codes_val = get_post_meta($id, 'sc_promotion_code', true);
518 }
519 if (empty($promo_codes_val) && $coupon_model) {
520 $p_code = $coupon_model->promotion_code ?? null;
521 if (!empty($p_code)) {
522 $promo_codes_val = $p_code;
523 } else {
524 $promo_codes_val = $code_val;
525 }
526 }
527 SureCartExport::$export_instance->data[$id]['promotion_codes'] = $promo_codes_val;
528
529 // 2. Discount Type & Amount
530 $disc_type = '';
531 $disc_amount = '';
532 if (!$is_uuid) {
533 $disc_type = get_post_meta($id, 'sc_discount_type', true);
534 $disc_amount = get_post_meta($id, 'sc_discount_amount', true);
535 }
536
537 if (empty($disc_type) && $coupon_model) {
538 if (!empty($coupon_model->percent_off)) {
539 $disc_type = 'percentage';
540 $disc_amount = $coupon_model->percent_off;
541 } elseif (!empty($coupon_model->amount_off)) {
542 $disc_type = 'fixed';
543 $disc_amount = $coupon_model->amount_off;
544 } else {
545 $disc_type = 'fixed';
546 $disc_amount = 0;
547 }
548 }
549 SureCartExport::$export_instance->data[$id]['discount_type'] = $disc_type;
550 SureCartExport::$export_instance->data[$id]['discount_amount'] = $disc_amount;
551
552 // 3. Status
553 $status = '';
554 if (!$is_uuid) {
555 $status = get_post_meta($id, 'sc_status', true);
556 }
557 if (empty($status) && $coupon_model) {
558 if (!empty($coupon_model->archived)) {
559 $status = 'archived';
560 } elseif (!empty($coupon_model->expired)) {
561 $status = 'expired';
562 } else {
563 $status = 'active';
564 }
565 }
566 SureCartExport::$export_instance->data[$id]['status'] = $status;
567
568 // 4. Dates
569 $start_date = '';
570 if (!$is_uuid) {
571 $start_date = get_post_meta($id, 'sc_start_date', true);
572 }
573 if (empty($start_date) && $coupon_model) {
574 $start_date = $coupon_model->created_at;
575 if (is_numeric($start_date))
576 $start_date = date('Y-m-d H:i:s', $start_date);
577 }
578 SureCartExport::$export_instance->data[$id]['start_date'] = $start_date;
579
580 $end_date = '';
581 if (!$is_uuid) {
582 $end_date = get_post_meta($id, 'sc_end_date', true);
583 }
584 if (empty($end_date) && $coupon_model) {
585 $end_date = $coupon_model->redeem_by;
586 if (is_numeric($end_date))
587 $end_date = date('Y-m-d H:i:s', $end_date);
588 }
589 SureCartExport::$export_instance->data[$id]['end_date'] = $end_date;
590
591 // 5. Limits & Amounts
592 // Iterate through map
593 foreach ($field_map as $export_key => $sources) {
594 $meta_key = $sources[0];
595 $model_prop = $sources[1];
596
597 // Skip manually handled fields
598 if (in_array($export_key, ['promotion_codes', 'discount_type', 'discount_amount', 'status', 'start_date', 'end_date']))
599 continue;
600
601 $val = '';
602 if (!$is_uuid) {
603 $val = get_post_meta($id, $meta_key, true);
604 }
605 if (empty($val) && $coupon_model) {
606 $p_val = $coupon_model->$model_prop ?? null;
607 if (!is_null($p_val)) {
608 if (is_array($p_val))
609 $val = implode(',', $p_val);
610 else
611 $val = $p_val;
612 }
613 }
614 SureCartExport::$export_instance->data[$id][$export_key] = $val;
615 }
616
617 // 6. Applies To
618 // (Handled in loop above via field map? No, appies_to needs logic?)
619 // Actually, field map handles basic scalar. Check logic below.
620
621 // Re-check logic for applies_to if logic is complex
622 $applies_to = '';
623 if (!$is_uuid) {
624 $applies_to = get_post_meta($id, 'sc_applies_to', true);
625 }
626 if (empty($applies_to) && $coupon_model) {
627 $applies_to = $coupon_model->filter_match_type ?? 'all';
628 }
629 SureCartExport::$export_instance->data[$id]['applies_to'] = $applies_to ?: 'all';
630
631
632 // 7. First Order Only
633 $first_order = '';
634 if (!$is_uuid) {
635 $first_order = get_post_meta($id, 'sc_first_order_only', true);
636 }
637 if ($first_order === '' && $coupon_model) {
638 $first_order = $coupon_model->first_order_only ?? 0;
639 }
640 SureCartExport::$export_instance->data[$id]['first_order_only'] = $first_order;
641
642
643 // Handle Coupon Meta (JSON)
644 $sc_metadata = '';
645 if (!$is_uuid) {
646 $sc_metadata = get_post_meta($id, 'sc_metadata', true);
647 }
648 if (empty($sc_metadata) && $coupon_model) {
649 $sc_metadata = $coupon_model->metadata;
650 }
651
652 $metadata_output = '';
653 if (!empty($sc_metadata)) {
654 $sc_metadata = (array) $sc_metadata;
655 foreach ($sc_metadata as $key => $value) {
656 $metadata_output .= $key . ':' . (is_array($value) ? json_encode($value) : $value) . '|';
657 }
658 }
659 SureCartExport::$export_instance->data[$id]['coupon_meta'] = rtrim($metadata_output, '|');
660 }
661
662
663 }
664
665