-
Notifications
You must be signed in to change notification settings - Fork 9
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
Multiple Variations Support #235
base: trunk
Are you sure you want to change the base?
Conversation
…te the dynamic option meta
@dkotter over to you for code review. |
@qasumitbagthariya bumping this up in your queue to try and wrap it up asap. |
QA Update ✅I have verified this PR in the I tested the following on this branch: Square SOR Square_SOR.movWoocommerce SOR Woo-SOR.movTesting Environment
Steps to Test- As mentioned in the PR description.
|
$objects = $response->get_data()->getObjects() ? $response->get_data()->getObjects() : array(); | ||
|
||
foreach ( $objects as $object ) { | ||
$options_data[ $object->getId() ]['name'] = $object->getItemOptionData()->getDisplayName(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can any of the getters return null
values? If so, it would be nice to add some checks to avoid errors.
$attributes = $product->get_attributes(); | ||
|
||
$product_variation_ids = $product->get_children(); | ||
$catalog_variations = $item_data->getVariations() ? $item_data->getVariations() : array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since PHP 7.0 we can change this to:
$catalog_variations = $item_data->getVariations() ? $item_data->getVariations() : array(); | |
$catalog_variations = $item_data->getVariations() ?? array(); |
) { | ||
$options_ids = array(); | ||
$result = wc_square()->get_api()->retrieve_options_data(); | ||
$options_data = isset( $result[1] ) ? $result[1] : array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
$options_data = isset( $result[1] ) ? $result[1] : array(); | |
$options_data = $result[1] ?? array(); |
$variation_items = $product->get_attributes(); | ||
$variation_item_values = array(); | ||
|
||
if ( 1 === count( $attributes ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that these changes contain multiple "actions" that could be new methods to make reading easier. Basically, every time we have a comment here, the following code could be a new method named after the explanation. But this is optional
$new_cursor = isset( $result[2] ) ? $result[2] : null; | ||
|
||
if ( ! empty( $new_cursor ) ) { | ||
$this->set_attr( 'fetch_options_data_cursor', $new_cursor ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this line can be moved outside the condition, as $new_cursor
will be null per the default value above anyway.
@@ -1056,6 +1297,8 @@ protected function save_variations( $product_id, $data ) { | |||
$updated_attribute_keys = array(); | |||
|
|||
foreach ( $variation['attributes'] as $attribute_key => $attribute ) { | |||
// Set empty if attribute is to 'Any' to prevent it from being saved. | |||
$attribute['option'] = isset( $attribute['option'] ) && 'Any' !== $attribute['option'] ? $attribute['option'] : ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should move Any
to a new constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Some multiple optional suggestions 👍
Thanks @wjrosa. @faisal-alvi see if you can make those optional suggested changes above and then we can move this to the next step. |
All Submissions:
Changes proposed in this Pull Request:
This PR adds support for Multiple Variations.
Square and WooCommerce Attribute Types
Square options:
WooCommerce attributes:
Key Differences Between Dynamic and Static Options
Dynamic options can be shared across products and mirror WooCommerce’s taxonomy attributes, and variation names cannot be modified, as they are automatically generated by combining the attribute values. For example: “Red, Small.” Static options are unique to each item. If multiple options exist at Square, they must be dynamic.
Syncing Logic (Overview)
WooCommerce to Square Sync:
Square to WooCommerce Sync/Import:
Syncing Logic for WooCommerce and Square (Detail)
From WooCommerce to Square:
From Square to WooCommerce:
Square API Limitations and Solutions
Square’s API only provides the option ID used in variations, not the display name. However, to match WooCommerce taxonomy attributes or to create a custom attribute, we need the option's display name. To address this, we fetch all options via another Square API (discussion link) and store the option names and their IDs in a transient (
wc_square_options_data
) with a 1-day expiration. Thefetch_options_data()
function initiates this before any import or sync process.If a transient doesn’t have updated data, a sync may fail when creating an option with a name that already exists at Square, which would require a latest options data. This PR then stops the sync and sets the
woocommerce_square_refresh_sync_cycle
option to refresh the data.Avoiding Attribute Name Conflicts
If WooCommerce’s taxonomy attribute is used, this PR passes the taxonomy name (e.g., “pa_color” rather than “Color”) to Square, preventing conflicts with custom attributes of the same name. The “pa_” prefix helps WooCommerce users distinguish between taxonomy and custom attributes, avoiding potential conflicts.
New Option for Tracking
This PR introduces a new
_dynamic_options
option withtrue/false
values to track whether dynamic options are used for a product. This is not in use but may be useful in future.Notes for Merchants
Post-Tasks
Closes #6, Closes #31 and Closes #231.
Steps to test the changes in this Pull Request:
Changelog entry