-
Notifications
You must be signed in to change notification settings - Fork 18
Fix Categories were being stripped from products during manual sync. #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 9 commits
ca90fc4
4405de2
fb820a1
c642353
a658062
b1bed68
9f38a7b
400cc34
558aa0f
1ccb8de
8a059d9
258c50b
db96b16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,4 +263,54 @@ public static function get_square_category_id( $catalog_item ) { | |
|
|
||
| return $catalog_category_id; | ||
| } | ||
|
|
||
| /** | ||
| * Get the WooCommerce category IDs from a catalog item. | ||
| * | ||
| * @param \Square\Models\CatalogItem $catalog_item the catalog item object | ||
| * @return array | ||
| */ | ||
| public static function get_category_ids_from_catalog_item( $catalog_item ) { | ||
| $category_ids = array(); | ||
| $missing_category_ids = array(); | ||
|
|
||
| if ( empty( $catalog_item ) ) { | ||
| return $category_ids; | ||
| } | ||
|
|
||
| if ( $catalog_item->getCategories() && is_array( $catalog_item->getCategories() ) ) { | ||
|
iamdharmesh marked this conversation as resolved.
|
||
| foreach ( $catalog_item->getCategories() as $category ) { | ||
| if ( $category instanceof \Square\Models\CatalogObjectCategory ) { | ||
| $category_id = self::get_category_id_by_square_id( $category->getId() ); | ||
| if ( $category_id ) { | ||
| $category_ids[] = $category_id; | ||
| } else { | ||
| $missing_category_ids[] = $category->getId(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
iamdharmesh marked this conversation as resolved.
|
||
|
|
||
| // Fetch and import missing categories. | ||
| if ( ! empty( $missing_category_ids ) ) { | ||
| try { | ||
| $response = wc_square()->get_api()->batch_retrieve_catalog_objects( $missing_category_ids ); | ||
|
iamdharmesh marked this conversation as resolved.
|
||
| if ( $response->get_data() instanceof \Square\Models\BatchRetrieveCatalogObjectsResponse ) { | ||
| $missing_categories = $response->get_data()->getObjects(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We log exception if we didn't get a proper response, but not logging for the empty array? Could you please provide more information on what we want to do here? Thanks |
||
| if ( $missing_categories && is_array( $missing_categories ) ) { | ||
| foreach ( $missing_categories as $missing_category ) { | ||
| $imported_category_id = self::import_or_update( $missing_category ); | ||
| if ( $imported_category_id ) { | ||
| $category_ids[] = $imported_category_id; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch ( \Exception $e ) { | ||
| wc_square()->log( 'Error fetching missing categories for product ' . $catalog_item->getName() . ': ' . $e->getMessage() ); | ||
| } | ||
| } | ||
|
|
||
| return array_unique( $category_ids ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.