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

Feature/coupon syncer #1014

Conversation

chennyxie
Copy link
Contributor

@chennyxie chennyxie commented Sep 17, 2021

Closes #1008
GooglePromotionService is released in https://github.com/googleapis/google-api-php-client-services (v0.211.0 and above)
Note that createPromotion API works for create or update as long as using same promotion id. In addition, we use create with set effective dates as expired to disable the promotion.

Major files that are added:

  1. Add hooks for listen coupon create/update/trash/delete
  2. Add jobs to update/delete promotion on Google by calling CouponSyncer
  3. Add CouponHelper and CouponMetaHelper to manage coupon sync status
  4. Add CouponSyncer to manage direct call to GooglePromotionService.
  5. Add WCCouponAdapter to map WC_Coupon to Google promotion.
  6. Add coupon visibility metabox to provide visibility setting option in coupon edit page

Screenshots:

image

image

image

Detailed test instructions:

  1. Before mc account setup, go to coupon edit page, check setup is link displayed.
  2. Create or edit a coupon with select channel visibility as show on Google
  3. Sumit the change and wait for sync status shows it started to sync
  4. Trash the coupon and wait to confirm a request sent for disable promotion.

Changelog entry

@jconroy
Copy link
Member

jconroy commented Sep 21, 2021

Thanks @chennyxie !

@jconroy jconroy requested a review from a team September 21, 2021 04:02
@JPry
Copy link
Contributor

JPry commented Sep 21, 2021

Thanks for the PR @chennyxie!

I noticed in the list of commits, there seem to be numerous additional commits besides the ones that you made. I would recommend rebasing against the develop branch so that only your changes are part of this PR. I'd be happy to help out with that if needed.

@chennyxie chennyxie force-pushed the feature/coupon-syncer branch 2 times, most recently from 3a5eb0b to e335d33 Compare September 22, 2021 06:19
@chennyxie
Copy link
Contributor Author

Hi, JPry,
I have rebased my changes based on the current develop branch, and this pull request is requesting to merge into the branch "woocommerce:feature/1008-coupon-integration", which has several commits behind the current "develop".

Do you think if it will help to update this "woocommerce:feature/1008-coupon-integration" branch to the current develop, so that only my commits are shown in this PR? thanks

@JPry
Copy link
Contributor

JPry commented Sep 22, 2021

Do you think if it will help to update this "woocommerce:feature/1008-coupon-integration" branch to the current develop, so that only my commits are shown in this PR?

@chennyxie Yes, I think this PR should target the woocommerce:develop branch instead of woocommerce:feature/1008-coupon-integration

@jconroy
Copy link
Member

jconroy commented Sep 22, 2021

@JPry I set up this as a feature branch so that we can choose when to merge to develop and release

@JPry
Copy link
Contributor

JPry commented Sep 22, 2021

I set up this as a feature branch so that we can choose when to merge to develop and release

Ah, sorry, I wasn't aware of that. In that case, it definitely sounds like we should stick with the woocommerce:feature/1008-coupon-integration branch as a target.

That also means that the rebase should take place against the woocommerce:feature/1008-coupon-integration branch rather than the woocommerce:develop branch.

@chennyxie
Copy link
Contributor Author

Hi, I have rebased this PR again according to the comments above. Now it should be ready for review. Thanks

@jconroy
Copy link
Member

jconroy commented Sep 23, 2021

Thanks @chennyxie that looks better! Cheers 🍻

@chennyxie chennyxie removed their assignment Sep 23, 2021
Copy link
Contributor

@JPry JPry left a comment

Choose a reason for hiding this comment

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

@chennyxie I've gone through and tested this functionality on my test site. Setting up a coupon is working well, and I can see that they sync to Google is "pending". Do you know how long it would typically take for the sync to occur?

On a first look, the logic looks to be sound in your code. I'll take another pass tomorrow to see if there's anything else to adjust. Until then, the biggest thing I've noticed is related to the project code style. I've added a few suggestions below, but it looks like some of the files need more comprehensive adjustments. Could you please make changes based on running ./vendor/bin/phpcs?

composer.json Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/CouponChannelVisibilityMetaBox.php Outdated Show resolved Hide resolved
src/Admin/MetaBox/MetaBoxInterface.php Outdated Show resolved Hide resolved
src/Coupon/CouponHelper.php Outdated Show resolved Hide resolved
@jconroy
Copy link
Member

jconroy commented Oct 28, 2021

@JPry can you give the last few commits a once over to check they are all good. I noticed in 9f3b231#diff-55e830624fd3dfe830824f42db8d5cd251b37c1d637d837435cbd48f8d15e1dfL219 we might be losing some debugging

Then I think we can merge and get ready for release on Tuesday

*/
public function mark_as_synced(
WC_Coupon $coupon,
string $google_id,
?string $google_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

In which cases this can be a null value? Shouldn't all synced coupons have an ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to be compatible to the alpha version of content API and the id will be used in later version.

src/Coupon/CouponHelper.php Outdated Show resolved Hide resolved
src/Coupon/WCCouponAdapter.php Outdated Show resolved Hide resolved
src/Jobs/DeleteCoupon.php Outdated Show resolved Hide resolved
src/Product/WCProductAdapter.php Outdated Show resolved Hide resolved
@layoutd
Copy link
Contributor

layoutd commented Nov 2, 2021

During some usage testing, a 403 error popped up:
image

This happened when trying to sync a coupon with a linked, existing Merchant Center account (that is, not a new Merchant Center account created under our MCA). My best guess is that the MCA can't manipulate promotions for linked accounts (unlike for sub-accounts), and since the promotions endpoint has "Limited Access" – which the existing accounts don't have – the operations fail.

Does this sound correct? Is there any workaround for this? Or will this access limitation be removed once the Promotions feature goes public?

Copy link
Contributor

@nima-karimi nima-karimi left a comment

Choose a reason for hiding this comment

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

The code looks good to merge but I did leave a bunch of comments that need to be addressed in a subsequent PR. Justin has tested this and left a comment with the results here: #1014 (comment)

src/Product/WCProductAdapter.php Outdated Show resolved Hide resolved
*
* @return array
*/
public static function convert_product_types( $category_ids ): array {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably best to make these methods (this one and get_google_product_offer_id) protected if we're not using them anywhere else. Especially given that this class isn't a utility class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

convert_product_types and get_google_product_offer_id are also used in WCCouponAdapter, the 2 adapters share some product info conversion method to guarantee product attributes matches the coupon restrictions related to product ids or product categories. We can consider to create a utility for adapters in the followup improvements.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, okay then I would definitely vote for a utility class since that's not a functionality specific to the WCProductAdapter.

src/Product/WCProductAdapter.php Show resolved Hide resolved
src/Jobs/DeleteCoupon.php Outdated Show resolved Hide resolved
src/Google/InvalidCouponEntry.php Outdated Show resolved Hide resolved
src/Coupon/CouponSyncer.php Show resolved Hide resolved
@chennyxie
Copy link
Contributor Author

Hi, yes, as the Google documentation says, currently promotion API is open for limited access, so a MC account that not managed by WooCommerce MCA are not open to use this alpha version, which should be resolved once we open the API to public.

During some usage testing, a 403 error popped up: image

This happened when trying to sync a coupon with a linked, existing Merchant Center account (that is, not a new Merchant Center account created under our MCA). My best guess is that the MCA can't manipulate promotions for linked accounts (unlike for sub-accounts), and since the promotions endpoint has "Limited Access" – which the existing accounts don't have – the operations fail.

Does this sound correct? Is there any workaround for this? Or will this access limitation be removed once the Promotions feature goes public?

@jconroy
Copy link
Member

jconroy commented Nov 2, 2021

Hi, yes, as the Google documentation says, currently promotion API is open for limited access, so a MC account that not managed by WooCommerce MCA are not open to use this alpha version, which should be resolved once we open the API to public.

Thanks @chennyxie just needed to confirm as the e2e tests note that "we made this case by disabling promotion API access from Google side" and weren't sure if we still had some access disabled etc.

@chennyxie
Copy link
Contributor Author

Hi, yes, as the Google documentation says, currently promotion API is open for limited access, so a MC account that not managed by WooCommerce MCA are not open to use this alpha version, which should be resolved once we open the API to public.

Thanks @chennyxie just needed to confirm as the e2e tests note that "we made this case by disabling promotion API access from Google side" and weren't sure if we still had some access disabled etc.

Hi, the API access issue for the account linking of an existing MC account has been resolved now, be free to retry the test to verify it

@layoutd
Copy link
Contributor

layoutd commented Nov 4, 2021

Hi, the API access issue for the account linking of an existing MC account has been resolved now, be free to retry the test to verify it

Hi @chennyxie, I just ran a quick test and it looks like the issue with linked accounts is fixed - they go to Sent to Google status. 🎉

However, I'm seeing an issue with coupons created in stores targeting a non-US country. When I set up a store targeting somewhere else (Spain, for example), and then create a coupon, it just sticks on Pending for sync:
image

In the log, it shows the job running (gla/jobs/update_coupon/process_item) and the log indicates that the coupon is being skipped:

DEBUG Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponSyncer::update Skipping coupon (ID: 97) because it is not supported in main target country ES.

Should this message be displayed in the Coupon form (I thought I saw it at some point before)? Or should the coupon be updated even though the target country is non-US?

@chennyxie
Copy link
Contributor Author

Hi, the API access issue for the account linking of an existing MC account has been resolved now, be free to retry the test to verify it

Hi @chennyxie, I just ran a quick test and it looks like the issue with linked accounts is fixed - they go to Sent to Google status. 🎉

However, I'm seeing an issue with coupons created in stores targeting a non-US country. When I set up a store targeting somewhere else (Spain, for example), and then create a coupon, it just sticks on Pending for sync: image

In the log, it shows the job running (gla/jobs/update_coupon/process_item) and the log indicates that the coupon is being skipped:

DEBUG Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponSyncer::update Skipping coupon (ID: 97) because it is not supported in main target country ES.

Should this message be displayed in the Coupon form (I thought I saw it at some point before)? Or should the coupon be updated even though the target country is non-US?

Hi, thanks for catching this. Seems I missed one commit(during mistakenly click fix on this PR page and which the conflict with my local committed fix) with a force push, I have added the commit back

@jconroy
Copy link
Member

jconroy commented Nov 5, 2021

the API access issue for the account linking of an existing MC account has been resolved now, be free to retry the test to verify it

Thanks @chennyxie 👏

@layoutd layoutd mentioned this pull request Nov 8, 2021
@layoutd
Copy link
Contributor

layoutd commented Nov 8, 2021

👍🏻 I've done a bit more usage testing and functionally, it looks good:

  • Coupons targeting US are synced correctly for existing, USA and Global MC accounts
  • Coupons targeting non-US have syncing disabled
  • Different coupon types and details (restrictions, limits) are synced correctly
  • Coupons are deleted correctly
  • Coupons expire correctly

:shipit: I'm going to merge this PR into the feature/1008-coupon-integration branch as the first step towards releasing it in version 1.6.0 #1084. Thanks @chennyxie 👏🏻

@layoutd layoutd merged commit 23c1d29 into woocommerce:feature/1008-coupon-integration Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants