PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.6 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 All 49 releases
fluent-cart / app / Services / Async / DummyProductService.php

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

173 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 'guid' => get_site_url() . '/?items=' . $productName . '-' . $productNameSuffix
99 ];
100 $product = array_merge($product, $data);
101 $detail = $product['detail'];
102 $variantData = $product['variants'];
103
104 $galleryImages = [];
105
106
107 if (isset($product['gallery']) && is_array($product['gallery'])) {
108 $galleryImages = $product['gallery'];
109 }
110 $categories = Arr::get($product, 'categories');
111
112 $product = Product::query()->create(
113 Arr::except($product, ['detail', 'variants', 'gallery'])
114 );
115
116 $this->attachTerms($categories, $product->ID);
117
118 /**
119 * @var Product $product
120 */
121
122 if (!empty($galleryImages)) {
123 ImageAttachService::attachImageToProduct($product->toArray(), $galleryImages);
124
125 $galleryImageWithId = get_post_meta($product->ID, 'fluent-products-gallery-image', true);
126 if (isset($galleryImageWithId[0])) {
127 set_post_thumbnail($product->ID, Arr::get($galleryImageWithId, '0.id'));
128 }
129 }
130 $variants = $product->variants()->createMany($variantData);
131
132 foreach ($variants as $index => $variant) {
133 $images = Arr::get($variantData, $index . '.images', []);
134 if (is_array($images) && count($images)) {
135 ImageAttachService::attachImageToVariant($variant->id, $images);
136 }
137 }
138
139 $detail['post_id'] = $product->ID;
140 $detail['default_variation_id'] = $variants->first()->id;
141 $product->detail()->create($detail);
142
143 }
144
145 public function attachTerms($categories, $postId)
146 {
147 if (!function_exists('wp_create_term')) {
148 require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
149 }
150
151
152 if (is_string($categories)) {
153 $categories = explode(',', $categories);
154 }
155
156 $termIds = [];
157
158 if (is_array($categories)) {
159 foreach ($categories as $category) {
160 $term = wp_create_term($category, 'product-categories');
161 $termIds[] = $term['term_id'];
162 }
163 }
164 wp_set_post_terms($postId, $termIds, 'product-categories');
165 }
166
167
168 protected function getFilePath(string $category): string
169 {
170 return FLUENTCART_PLUGIN_PATH . 'dummies' . DIRECTORY_SEPARATOR . $category . '.json';
171 }
172
173 }