PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / Async / DummyProductService.php

DummyProductService.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.1, at app/Services/Async/DummyProductService.php

174 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Async;
4
5 use FluentCart\App\Models\Product;
6 use FluentCart\App\Services\DateTime\DateTime;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\Framework\Support\Str;
9
10 class DummyProductService
11 {
12 public static function create(string $category, $index)
13 {
14 $instance = new self();
15 $filePath = $instance->getFilePath($category);
16 if (!file_exists($filePath)) {
17 return new \WP_Error(
18 400,
19 __('Products for this category is not found', 'fluent-cart'),
20 );
21 }
22 $json = file_get_contents($filePath);
23 try {
24 $products = json_decode($json, true);
25
26 $product = Arr::get($products, $index);
27 if (empty($product)) {
28 return new \WP_Error(
29 404,
30 __('Product Not Found', 'fluent-cart'),
31 );
32 }
33 $instance->insert($product);
34
35 return true;
36
37 } catch (\Exception $exception) {
38 return new \WP_Error(
39 400,
40 __('Products for this category is not found', 'fluent-cart'),
41 );
42 }
43 }
44
45 public static function createAll(string $category)
46 {
47 $instance = new self();
48 $filePath = $instance->getFilePath($category);
49 if (!file_exists($filePath)) {
50 return new \WP_Error(
51 400,
52 __('Products for this category is not found', 'fluent-cart'),
53 );
54 }
55 $json = file_get_contents($filePath);
56 try {
57 $products = json_decode($json, true);
58 foreach ($products as $product) {
59 $instance->insert($product);
60 }
61 return true;
62
63 } catch (\Exception $exception) {
64 return new \WP_Error(
65 400,
66 __('Products for this category is not found', 'fluent-cart'),
67 );
68 }
69 }
70
71
72 protected function insert($product)
73 {
74
75 $productName = Str::slug($product['post_title'], '-', null);
76 $now = DateTime::gmtNow();
77 $createdDate = $now->format('Y-m-d H:i:s');
78 $productNameSuffix = $now->format('d-m-Y-H-i-s');
79
80 $data = [
81 'post_author' => get_current_user_id(),
82 'post_date' => $createdDate,
83 'post_date_gmt' => $createdDate,
84 'post_content_filtered' => '',
85 'post_status' => 'publish',
86 'post_type' => 'fluent-products',
87 'comment_status' => 'open',
88 'ping_status' => 'closed',
89 'post_password' => '',
90 'post_name' => $productName . '-' . $productNameSuffix,
91 'to_ping' => '',
92 'pinged' => '',
93 'post_modified' => $createdDate,
94 'post_modified_gmt' => $createdDate,
95 'post_parent' => 0,
96 'menu_order' => 0,
97 'post_mime_type' => '',
98 ];
99 $product = array_merge($product, $data);
100 $detail = $product['detail'];
101 $variantData = $product['variants'];
102
103 $galleryImages = [];
104
105
106 if (isset($product['gallery']) && is_array($product['gallery'])) {
107 $galleryImages = $product['gallery'];
108 }
109 $categories = Arr::get($product, 'categories');
110
111 $product = Product::query()->create(
112 Arr::except($product, ['detail', 'variants', 'gallery'])
113 );
114
115 $product->update(['guid' => get_permalink($product->ID)]);
116
117 $this->attachTerms($categories, $product->ID);
118
119 /**
120 * @var Product $product
121 */
122
123 if (!empty($galleryImages)) {
124 ImageAttachService::attachImageToProduct($product->toArray(), $galleryImages);
125
126 $galleryImageWithId = get_post_meta($product->ID, 'fluent-products-gallery-image', true);
127 if (isset($galleryImageWithId[0])) {
128 set_post_thumbnail($product->ID, Arr::get($galleryImageWithId, '0.id'));
129 }
130 }
131 $variants = $product->variants()->createMany($variantData);
132
133 foreach ($variants as $index => $variant) {
134 $images = Arr::get($variantData, $index . '.images', []);
135 if (is_array($images) && count($images)) {
136 ImageAttachService::attachImageToVariant($variant->id, $images);
137 }
138 }
139
140 $detail['post_id'] = $product->ID;
141 $detail['default_variation_id'] = $variants->first()->id;
142 $product->detail()->create($detail);
143
144 }
145
146 public function attachTerms($categories, $postId)
147 {
148 if (!function_exists('wp_create_term')) {
149 require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
150 }
151
152
153 if (is_string($categories)) {
154 $categories = explode(',', $categories);
155 }
156
157 $termIds = [];
158
159 if (is_array($categories)) {
160 foreach ($categories as $category) {
161 $term = wp_create_term($category, 'product-categories');
162 $termIds[] = $term['term_id'];
163 }
164 }
165 wp_set_post_terms($postId, $termIds, 'product-categories');
166 }
167
168
169 protected function getFilePath(string $category): string
170 {
171 return FLUENTCART_PLUGIN_PATH . 'dummies' . DIRECTORY_SEPARATOR . $category . '.json';
172 }
173
174 }