Skip to content

Commit 400ff38

Browse files
authored
feat: Sync all categories for MSF stores via API (#456)
- Remove the 250 category limit by paginating through all categories. - Aggregate all categories into a single array. - Log warnings and information for debugging. Previously, syncing categories for MSF stores via API was limited to 250. This change ensures all categories are synced.
1 parent fcd4959 commit 400ff38

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/BigCommerce/Import/Processors/Category_Import.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,57 @@ protected function get_fallback_terms() {
2929
// If GraphQL failed we are trying to fallback with REST API
3030
try {
3131
if ( Store_Settings::is_msf_on() ) {
32-
$data = $this->get_msf_categories( $this->catalog_api );
3332

34-
if ( ! empty( $data ) ) {
35-
return $data->getData();
33+
/* loop though all categories and return $allCategories */
34+
$allCategories = [];
35+
$currentPage = 1;
36+
$totalPages = 1;
37+
38+
do {
39+
40+
$params['page'] = $currentPage;
41+
42+
$data = $this->get_msf_categories( $this->catalog_api, $params);
43+
44+
if ( ! empty( $data ) ) {
45+
46+
$currentData = $data->getData();
47+
48+
if (is_array($currentData) && !empty($currentData)) {
49+
$allCategories = array_merge($allCategories, $currentData);
50+
} else {
51+
do_action('bigcommerce/log', Error_Log::WARNING, __('Current data is not an array or is empty.', 'bigcommerce'), []);
52+
}
53+
54+
if ( method_exists( $data, 'getMeta' ) ){
55+
$meta = $data->getMeta();
56+
if ( isset( $meta['pagination']['current_page'] ) && isset( $meta['pagination']['total_pages'] ) ) {
57+
$currentPage = $meta['pagination']['current_page'];
58+
$totalPages = $meta['pagination']['total_pages'];
59+
} else {
60+
do_action( 'bigcommerce/log', Error_Log::WARNING, __( 'Pagination information is missing in the response meta.', 'bigcommerce' ), [] );
61+
break; // Exit the loop
62+
}
63+
} else {
64+
do_action('bigcommerce/log', Error_Log::WARNING, __('getMeta method does not exist on the data object.', 'bigcommerce'), []);
65+
break; // Exit the loop
66+
}
67+
} else {
68+
do_action('bigcommerce/log', Error_Log::WARNING, __('No data returned from get_msf_categories.', 'bigcommerce'), []);
69+
break; // Exit the loop
70+
}
71+
72+
do_action( 'bigcommerce/log', Error_Log::INFO, __( "Category import Page $currentPage of $totalPages", 'bigcommerce' ), [] );
73+
$currentPage ++;
74+
75+
} while ( $currentPage <= $totalPages );
76+
77+
do_action( 'bigcommerce/log', Error_Log::INFO, __( "Total categories found: ".count($allCategories) , 'bigcommerce' ), [] );
78+
79+
if ( ! empty( $allCategories ) ) {
80+
return $allCategories;
3681
}
82+
3783
}
3884

3985
return $this->catalog_api->getCategoriesBatch()->getData();

0 commit comments

Comments
 (0)