woocommerce / woocommerce-square

Square POS and Payments Integration
https://woo.com/products/square
Other
11 stars 7 forks source link

Multiple Variations Support #235

Open faisal-alvi opened 1 week ago

faisal-alvi commented 1 week ago

All Submissions:


Changes proposed in this Pull Request:

This PR adds support for Multiple Variations.

Square and WooCommerce Attribute Types

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)

  1. WooCommerce to Square Sync:

    • Creates dynamic options if there are multiple WooCommerce attributes, regardless of the WooCommerce's attributes type.
    • For a single attribute, it creates a static option at Square.
  2. Square to WooCommerce Sync/Import:

    • Creates taxonomy attributes if the option slug matches an existing WooCommerce taxonomy attribute.
    • If only one option is received, it checks for an existing WooCommerce taxonomy attribute or creates a custom attribute if no match is found.

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. The fetch_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., “pacolor” 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 with true/false values to track whether dynamic options are used for a product. This is not in use but may be useful in future.

Pending Tasks

Notes for Merchants

Closes #6, Closes #31 and Closes #231.

Steps to test the changes in this Pull Request:

  1. Set WooCommerce as SOR.
  2. Create the following product types in WooCommerce:
    • Simple Product
    • Variable Product with:
      • 1 Custom Attribute
      • 1 Taxonomy Attribute
      • 2 Taxonomy Attributes
      • 2 Custom Attributes
      • 1 Custom + 1 Taxonomy Attribute
      • 2 Taxonomy Attributes + 1 Custom Attribute
  3. Confirm these products are auto-synced and correctly created in Square.
  4. Confirm the same for manual sync using the “Sync” button in settings.
  5. Import all products from Square to WooCommerce and verify they retain attributes without issues (e.g., taxonomy attribute not converted to custom).
  6. Make changes to the random products and confirm it syncs to the Square and Imports back to the Woo without and issues, few examples:
    1. add one more custom attribute to the “2 Taxonomy Attributes” product
    2. remove 1 taxonomy attribute from the “2 Taxonomy Attributes + 1 Custom Attribute” product
    3. add one more variation to any product
    4. remove one variation from any product
    5. change the dropdown of any variation in “1 Custom Attribute” product
    6. change the dropdown of all variations in “2 Taxonomy Attributes + 1 Custom Attribute” product
    7. add one more attribute value and create update the dropdown values of each variation
    8. etc.
  7. Set Square as SOR and perform all of the above steps (sorry :)) This time the chagnes from the point 6 should be made at Square.
  8. Important: Confirm no issues affect existing users by creating all product types in trunk and performing sync/import in the fix branch.

Changelog entry

Add - Multiple Variations Support