Skip to content
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

Ensure coupon brand restrictions are uploaded to Google Merchant Center. #2697

Merged
merged 10 commits into from
Dec 5, 2024

Conversation

ianlin
Copy link
Member

@ianlin ianlin commented Nov 27, 2024

Changes proposed in this Pull Request:

Closes #2693.

Since WooCommerce 9.4, WooCommerce Brands is now part of the WooCommerce core, the merchant can add brand restrictions when editing coupons, but G4W plugin does not upload the corresponding coupon brand restrictions to Google Merchant Center.

This PR adds coupon restrictions for brands when generating the coupon (promotions) payload for updating to Google Merchant Center.

Note that WC_Coupon class does not have a method to get the brand restrictions like other restrictions do. This PR uses get_post_meta() to get the brands from a coupon, then uses get_objects_in_term() to get a list of product IDs from that brand.

Refer to the following tables to better understand the data:

  • wp_postmeta
    • Store the brands for coupons, queried byget_post_meta()
    • Meta keys: product_brands and exclude_product_brands
  • wp_terms
    • Where the brand ID, brand name, and brand slug are stored
  • wp_term_taxonomy
    • Store the taxonomy for the brand (product_brand)
  • wp_term_relationships
    • Store the mapping of products and brand. Queried by get_objects_in_term()
    • Should specify the brand taxonomy product_brand in get_objects_in_term()

Updated 28th Nov

For querying brands from a coupon from post_meta, I found in class-wc-brands-brand-settings-manager.php I can use it like:

\WC_Brands_Brand_Settings_Manager::set_brand_settings_on_coupon( $coupon );
$brand_settings = \WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon );

However, since we do not expect all merchants update to latest WooCommerce, we also need to ensure backward compatibility for those who still use WC Brands plugin. The code above was available since WC Brands plugin version 1.6.65, so I lean towards to still querying post_meta manually as it's safest for all version and in core.

Screenshots:

Detailed test instructions:

  1. Build the project using this PR, and ensure WooCommerce >= 9.4 is installed on your site
  2. Enable Brands feature by updating two options wc_feature_woocommerce_brands_enabled and woocommerce_remote_variant_assignment
    wp option update wc_feature_woocommerce_brands_enabled 'yes'
    wp option update woocommerce_remote_variant_assignment 2
  3. Create some brands from Products > Brands, e.g.
    Screenshot 2024-11-27 at 14 58 11
  4. Add some products to each of the brand that just created, e.g.
Screen.Recording.2024-11-27.at.15.07.31.mov
  1. Create a coupon, add brand restrictions to it
    • Make sure your store country supports coupons, e.g. US, GB.
    • Make sure to choose Show coupon on Google in Channel visibility
    • Note that the inclusion and exclusion cannot be set at the same time, otherwise Google API will respond 400 bad request. It also applies to existing restrictions like products or product categories.
    • Screenshot 2024-11-27 at 15 14 06
  2. Save the coupon, go to WooCommerce > Status > Scheduled Actions, search for the job gla/jobs/update_coupon/process_item, run it manually if it is still pending to save time
  3. Wait for a while, go to Google Merchant Center, click Promotions from the left menu.
  4. Click on the coupon (promotion) you just uploaded, then click Next. Ensure that the filters display the product IDs associated with the brand set as a restriction.
Screen.Recording.2024-11-27.at.15.24.52.mov
  1. Play around the existing restrictions, make sure they are still working
  2. Disable the Brands in core feature (see step 2), make sure the PR won't break it
  3. Using WooCommerce beta tester to downgrade WC to 9.2, and install WC Brands plugin, make sure the PR still works.

Product added or removed from a brand

  1. Let's say now that the coupon brand restriction is synced to GMC after the above test.
  2. Go to the product editor, deselect Test Brand 1 and update the product
    Screenshot 2024-12-05 at 15 09 44
  3. Go to WooCommerce > Status > Scheduled Actions, search for the job gla/jobs/update_coupon/process_item, run it manually if it is still pending to save time
  4. Wait for a while, go to Google Merchant Center, click Promotions from the left menu.
  5. Click on the coupon (promotion) you just uploaded, then click Next. Ensure that the filters no longer include the product that was just excluded from the brand.
  6. Go to the product editor, add the brand Test Brand 1 back. You should see the product gets synced to GMC in the coupon filters again.
  7. Create a new product and add the brand Test Brand 1, you should see the product gets synced to GMC in the coupon filters again.

Additional details:

Changelog entry

Fix - Ensure coupon brand restrictions are uploaded to Google Merchant Center.

@ianlin ianlin requested a review from a team November 27, 2024 07:51
@ianlin ianlin self-assigned this Nov 27, 2024
@github-actions github-actions bot added type: bug The issue is a confirmed bug. changelog: fix Took care of something that wasn't working. labels Nov 27, 2024
@ianlin ianlin marked this pull request as draft November 27, 2024 08:21
@ianlin ianlin removed the request for review from a team November 27, 2024 08:21
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 90.62500% with 6 lines in your changes missing coverage. Please review.

Project coverage is 66.2%. Comparing base (7cd33a8) to head (280a3ab).
Report is 14 commits behind head on develop.

Files with missing lines Patch % Lines
src/Coupon/SyncerHooks.php 94.4% 2 Missing ⚠️
src/Coupon/WCCouponAdapter.php 92.3% 2 Missing ⚠️
...ternal/DependencyManagement/JobServiceProvider.php 0.0% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             develop   #2697      +/-   ##
============================================
+ Coverage       63.4%   66.2%    +2.8%     
- Complexity         0    4691    +4691     
============================================
  Files            340     479     +139     
  Lines           5210   19603   +14393     
  Branches        1275       0    -1275     
============================================
+ Hits            3305   12984    +9679     
- Misses          1730    6619    +4889     
+ Partials         175       0     -175     
Flag Coverage Δ
js-unit-tests ?
php-unit-tests 66.2% <90.6%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/Coupon/SyncerHooks.php 79.0% <94.4%> (ø)
src/Coupon/WCCouponAdapter.php 80.7% <92.3%> (ø)
...ternal/DependencyManagement/JobServiceProvider.php 0.0% <0.0%> (ø)

... and 816 files with indirect coverage changes

@ianlin ianlin requested a review from a team November 28, 2024 03:45
@ianlin ianlin marked this pull request as ready for review November 28, 2024 03:45
@ianlin
Copy link
Member Author

ianlin commented Nov 29, 2024

Thanks for the good catch @eason9487. I've updated the PR and it's ready for another round.

@ianlin ianlin requested a review from eason9487 November 29, 2024 08:23
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work. Tested that the brand restrictions of coupons can be converted to product IDs and then synced to Google Merchant Center.

However, just noticed there may be another concern. If a product is later created/updated to attach or detach a brand having associations with coupon restrictions, the brand restrictions (converted to associated product IDs) of those coupons won't be synced.

tests/Unit/Coupon/WCCouponAdapterTest.php Show resolved Hide resolved
@ianlin
Copy link
Member Author

ianlin commented Dec 5, 2024

If a product is later created/updated to attach or detach a brand having associations with coupon restrictions, the brand restrictions (converted to associated product IDs) of those coupons won't be synced.

Thanks for catching this case @eason9487. I've updated the code to take care of this scenario and have also updated the testing instruction in the PR.

However, I did not solve the case when the product is deleted/trashed, so when it happens the GMC promotion filters will still have the deleted product IDs included. This is align with the behaviour of the coupon product inclusion restriction (not the brand restriction) - i.e. when a coupon has an included product restriction, then the product gets deleted, GMC promotion filters still have that product.

@ianlin ianlin requested a review from eason9487 December 5, 2024 07:25
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the further update! Tested that coupon brand restrictions can be converted to associated product IDs and be synchronized to GMC. LGTM.

The mismatched code comment should not need another round.

tests/Unit/Coupon/SyncerHooksTest.php Outdated Show resolved Hide resolved
@ianlin ianlin merged commit 494e762 into develop Dec 5, 2024
12 checks passed
@ianlin ianlin deleted the fix/coupon-restrictions-for-brands branch December 5, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: fix Took care of something that wasn't working. type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coupon restrictions by brand are not supported
2 participants