Skip to content

Commit 2bb1e60

Browse files
prepare export of product query
1 parent ee923a9 commit 2bb1e60

File tree

3 files changed

+109
-10
lines changed

3 files changed

+109
-10
lines changed

src/Controllers/CLI/Product_Controller.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77

88
class CLI_Products
99
{
10-
function getProtectedValue($obj, $name) {
11-
$array = (array)$obj;
12-
$prefix = chr(0).'*'.chr(0);
13-
return $array[$prefix.$name];
14-
}
15-
1610
function __get_product_data_simple($product_id) {
1711
$wc_product_data = \wc_get_product( $product_id );
1812
$image_id = $wc_product_data->get_image_id();
1913
$image_url = \wp_get_attachment_image_url( $image_id, 'full' );
2014

21-
$obj_product = $this->getProtectedValue($wc_product_data, 'data');
15+
$obj_product = Helpers::getProtectedValue($wc_product_data, 'data');
2216

2317
$product_data = [
2418
'method' => 'product',
@@ -63,11 +57,17 @@ function execute()
6357

6458
if ($woo_product->is_type('simple')) {
6559
$product_type = 'simple';
66-
$product_data[] = $this->__get_product_data_simple($product->ID);
60+
$product_data[] = [
61+
'method' => 'product',
62+
'data' => Helpers::getProductData($product->ID)
63+
];
6764

6865
} elseif ($woo_product->is_type('variable')) {
6966
$product_type = 'variable';
70-
$product_data[] = $this->__get_product_data_simple($product->ID);
67+
$product_data[] = [
68+
'method' => 'product',
69+
'data' => Helpers::getProductData($product->ID)
70+
];
7171

7272
$product_variation = get_posts([
7373
'post_type' => ['product_variation'],
@@ -77,7 +77,10 @@ function execute()
7777
]);
7878

7979
foreach ($product_variation as $variation) {
80-
$product_data[] = $this->__get_product_data_simple($variation->ID);
80+
$product_data[] = [
81+
'method' => 'product',
82+
'data' => Helpers::getProductData($product->ID)
83+
];
8184
}
8285

8386
} else {

src/Controllers/RestRoutes_Controller.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use WP_REST_Response;
99
use WP_REST_Server;
1010

11+
use SmartPack\WMS\Helpers;
12+
1113
class RestRoutes_Controller extends WP_REST_Controller
1214
{
1315
const PLUGIN_PREFIX = 'smartpack-wms';
@@ -17,6 +19,8 @@ class RestRoutes_Controller extends WP_REST_Controller
1719
const ROUTES = [
1820
'stockChanged' => '/stock-changed',
1921
'orderChanged' => '/order-changed',
22+
'exportProducts' => '/export/products',
23+
'exportOrders' => '/export/orders',
2024
];
2125

2226
public static function get_route_namespace(): string
@@ -43,6 +47,26 @@ public function register_routes()
4347
// 'callback' => [$this, 'orderChanged']
4448
// ]
4549
// );
50+
51+
52+
register_rest_route(
53+
self::get_route_namespace(),
54+
self::ROUTES['exportProducts'],
55+
[
56+
'methods' => WP_REST_Server::READABLE,
57+
'callback' => [$this, 'exportProducts']
58+
]
59+
);
60+
61+
62+
// register_rest_route(
63+
// self::get_route_namespace(),
64+
// self::ROUTES['exportOrders'],
65+
// [
66+
// 'methods' => WP_REST_Server::CREATABLE,
67+
// 'callback' => [$this, 'exportOrders']
68+
// ]
69+
// );
4670
}
4771

4872
public function stockChanged(WP_REST_Request $request)
@@ -165,6 +189,40 @@ public function orderChanged(WP_REST_Request $request)
165189
]);
166190
}
167191

192+
public function exportProducts(WP_REST_Request $request) {
193+
$products = Helpers::getAllproducts();
194+
195+
$product_data = [];
196+
197+
foreach ($products as $product) {
198+
$product_sku = \get_post_meta($product->ID, '_sku', true);
199+
$woo_product = wc_get_product( $product->ID );
200+
201+
if ($woo_product->is_type('simple')) {
202+
$product_type = 'simple';
203+
$product_data[] = Helpers::getProductData($product->ID);
204+
} elseif ($woo_product->is_type('variable')) {
205+
$product_type = 'variable';
206+
$product_data[] = Helpers::getProductData($product->ID);
207+
208+
$product_variation = get_posts([
209+
'post_type' => ['product_variation'],
210+
'numberposts' => -1,
211+
'post_status' => 'publish',
212+
'post_parent' => $product->ID
213+
]);
214+
215+
foreach ($product_variation as $variation) {
216+
$product_data[] = Helpers::getProductData($variation->ID);
217+
}
218+
}
219+
}
220+
221+
return new WP_REST_Response([
222+
'content' => $product_data,
223+
]);
224+
}
225+
168226
public function init()
169227
{
170228
add_action('rest_api_init', function () {

src/helpers/helpers.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
class Helpers
1111
{
12+
static function getProtectedValue($obj, $name) {
13+
$array = (array)$obj;
14+
$prefix = chr(0).'*'.chr(0);
15+
return $array[$prefix.$name];
16+
}
17+
1218
static function wcGetOrderLineMeta(int $order_item_id, string $meta_key)
1319
{
1420
global $wpdb;
@@ -29,4 +35,36 @@ static function wcGetOrderLineMeta(int $order_item_id, string $meta_key)
2935

3036
return $meta_data;
3137
}
38+
39+
static function getAllproducts() {
40+
$products = get_posts([
41+
'post_type' => ['product'],
42+
'numberposts' => -1,
43+
'post_status' => 'publish',
44+
]);
45+
46+
return $products;
47+
}
48+
49+
static function getProductData($product_id) {
50+
$wc_product_data = \wc_get_product( $product_id );
51+
$image_id = $wc_product_data->get_image_id();
52+
$image_url = \wp_get_attachment_image_url( $image_id, 'full' );
53+
54+
$obj_product = self::getProtectedValue($wc_product_data, 'data');
55+
56+
$product_data = [
57+
'id' => (string) $product_id,
58+
'sku' => $obj_product['sku'],
59+
'title' => $obj_product['name'],
60+
'description' => $obj_product['description'],
61+
'cost' => $obj_product['price'],
62+
'vendor' => '',
63+
'manufacturer' => '',
64+
'weight' => $obj_product['weight'],
65+
'imageUrl' => $image_url
66+
];
67+
68+
return $product_data;
69+
}
3270
}

0 commit comments

Comments
 (0)