Skip to content

Commit 6b3b043

Browse files
update with more data for product export
1 parent 2bb1e60 commit 6b3b043

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/Controllers/RestRoutes_Controller.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,36 +190,27 @@ public function orderChanged(WP_REST_Request $request)
190190
}
191191

192192
public function exportProducts(WP_REST_Request $request) {
193-
$products = Helpers::getAllproducts();
193+
$limit = (isset($_GET['limit']) ? (int) $_GET['limit'] : 100);
194+
$offset = (isset($_GET['offset']) ? (int) $_GET['offset'] : 0);
195+
196+
$products = Helpers::getAllproducts($limit, $offset);
194197

195198
$product_data = [];
196199

197200
foreach ($products as $product) {
198201
$product_sku = \get_post_meta($product->ID, '_sku', true);
199202
$woo_product = wc_get_product( $product->ID );
200203

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-
}
204+
$product_data[] = Helpers::getProductData($product->ID);
219205
}
220206

221207
return new WP_REST_Response([
222208
'content' => $product_data,
209+
'pagination' => [
210+
'limit' => $limit,
211+
'offset' => $offset,
212+
'found' => count($product_data)
213+
]
223214
]);
224215
}
225216

src/helpers/helpers.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ static function wcGetOrderLineMeta(int $order_item_id, string $meta_key)
3636
return $meta_data;
3737
}
3838

39-
static function getAllproducts() {
39+
static function getAllproducts($limit=100, $offset=0) {
4040
$products = get_posts([
41-
'post_type' => ['product'],
42-
'numberposts' => -1,
41+
'post_type' => ['product', 'product_variation'],
42+
'posts_per_page' => $limit,
43+
'offset' => $offset,
4344
'post_status' => 'publish',
4445
]);
45-
46+
4647
return $products;
4748
}
4849

0 commit comments

Comments
 (0)