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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

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.

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 92.30769% with 2 lines in your changes missing coverage. Please review.

Project coverage is 65.8%. Comparing base (16ff73f) to head (1e73c90).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
src/Coupon/WCCouponAdapter.php 92.3% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             develop   #2697     +/-   ##
===========================================
+ Coverage       65.1%   65.8%   +0.8%     
- Complexity      4658    4665      +7     
===========================================
  Files            812     477    -335     
  Lines          24578   19456   -5122     
  Branches        1254       0   -1254     
===========================================
- Hits           15990   12807   -3183     
+ Misses          8416    6649   -1767     
+ Partials         172       0    -172     
Flag Coverage Δ
js-unit-tests ?
php-unit-tests 65.8% <92.3%> (+<0.1%) ⬆️

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

Files with missing lines Coverage Δ
src/Coupon/WCCouponAdapter.php 80.7% <92.3%> (+1.4%) ⬆️

... and 335 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.

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.

@@ -258,6 +259,61 @@ public function test_product_type_restrictions() {
$this->assertEquals( [ 'Alpha Category > Beta Category' ], $adapted_coupon->getProductTypeExclusion() );
}

public function test_brand_restrictions() {
if ( version_compare( WC_VERSION, '9.4', '<' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggest adding a compatibility code comment to make it easier to search for compatibility codes in the future.

// compatibility-code "WC < 9.4" -- additional inline description

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