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

Handling Response.backUrl Undefined in AddToCart #95

Closed
wants to merge 4 commits into from

Conversation

Jmencab
Copy link

@Jmencab Jmencab commented Dec 9, 2024

Fixing bug on undefined backUrl parameter in the AddToCart pixel event.

Description (*)

Responding to issue 94 by taking the boolean definition out of the json and into the const. This replicates previous behavior but should default to true or false.

Previous behavior explanation:
In Magento, there is a configuration option called "Redirect to Cart After Add to Cart", which, when enabled, redirects users to the cart page after a product is added to the cart.
This functionality is implemented in script: /~https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js#L120
It checks for the backUrl in the response and, if present, redirects the user to the specified URL.
However, this behavior caused an issue with the addToCart event tracking. We use the ajax:addToCart event to trigger an ajax call that retrieves product information, which is then sent to Meta. Since AJAX calls are asynchronous by default, the user was being redirected to the cart page before the ajax call would complete.
To address this, I added the async property to the ajax call. When a backUrl is present in the response, the ajax call is made synchronous to ensure it completes fully before the redirection to the cart page occurs.

Story

Bug

94 <Cannot read properties of undefined (reading 'backUrl')>

Task

Fixed Issues (if relevant)

  1. magento-commerce/facebook-for-magento2#<issue_number>: Issue title

Manual testing scenarios (*)

There are two relevant variables: Redirect to Cart After Add to Cart (RedirectEnabled for short) and the backUrl value. Will test all 4 combinations of those two by toggling Redirect Enabled from local admin panel and then altering backUrl value from browser source file.

  1. RedirectEnabled = true, backUrl="http://44.245.119.204/": redirected to the homepage as expected and pixel addToCart recorded:
    image

  2. RedirectEnabled = true, backUrl=undefined or empty string; Redirected to cart (expected) and pixel addToCart recorded.

  3. RedirectEnabled = false, backUrl="http://44.245.119.204/": Stayed on PDP where I was. Makes sense, redirect is not enabled. AddToCart pixel event recorded.

  4. RedirectEnabled = false, backUrl=undefined or empty string; Same as above.

Questions or comments

Why sync if there is a backUrl and async if not? I am replicating the existing behavior but it does not make intuitive sense to me.

Checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

const swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
if (swatchData && swatchData.getProductId()) {
simpleProductId = swatchData.getProductId();
// check product data from swatch widget
Copy link
Author

Choose a reason for hiding this comment

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

Alignment change

if (configurableProduct) {
simpleProductId = configurableProduct.simpleProduct;
}
// else check product data from configurable options
Copy link
Author

Choose a reason for hiding this comment

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

alignment change

simpleProductId = productId;
}
const form_key = jQuery("[name='form_key']").val();
const runAsync = !data.response.backUrl;
Copy link
Author

Choose a reason for hiding this comment

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

main addition here

Copy link
Contributor

Choose a reason for hiding this comment

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

this will still result in an error if data.response.backUrl is undefined. add a check here, something like:

const runAsync = 'backUrl' in data.response ? !data.response.backUrl : true;

– returns true if data.response.backUrl is undefined or false

Copy link
Author

Choose a reason for hiding this comment

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

This is redundant because undefined checks are meant to be used for falsy value checking. To your point I added cont runAsync = !(data && data.response && data.response.backUrl) to cover our bases but explicitly checking for undefined like you have here is not only redundant but misses a case: backUrl could be set to empty string for example and that is functionally the same as it not being set at all. Just checking it the way I have it written would handle both cases.
<img width="1119" alt="Screenshot 2024-12-09 at 5 07 39 PM"
image
Screenshot 2024-12-09 at 5 42 02 PM
src="/~https://github.com/user-attachments/assets/af0ce705-1fbe-4f2f-bdb9-b839c9700522">

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot 2024-12-09 at 5 07 39 PM

},
type: 'get',
dataType: 'json',
async: runAsync,
Copy link
Author

Choose a reason for hiding this comment

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

using above defined const. looks bigger than it is because I fixed the alignment

@Jmencab Jmencab changed the title Taking out of json and into const for backUrl in case undefined Handling response.backUrl Undefined in AddToCart Dec 9, 2024
@Jmencab Jmencab changed the title Handling response.backUrl Undefined in AddToCart Handling Response.backUrl Undefined in AddToCart Dec 9, 2024
Copy link
Contributor

@zlik zlik left a comment

Choose a reason for hiding this comment

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

@Jmencab can you please address the comment below?

simpleProductId = productId;
}
const form_key = jQuery("[name='form_key']").val();
const runAsync = !data.response.backUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

this will still result in an error if data.response.backUrl is undefined. add a check here, something like:

const runAsync = 'backUrl' in data.response ? !data.response.backUrl : true;

– returns true if data.response.backUrl is undefined or false

Copy link
Contributor

@zlik zlik left a comment

Choose a reason for hiding this comment

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

lgtm! 🤝

@Jmencab
Copy link
Author

Jmencab commented Dec 10, 2024

@magento run all tests

Copy link
Contributor

@zlik zlik left a comment

Choose a reason for hiding this comment

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

lgtm!

@zlik
Copy link
Contributor

zlik commented Dec 11, 2024

@zlik zlik closed this Dec 11, 2024
magento-devops-reposync-svc pushed a commit that referenced this pull request Dec 11, 2024
PR #58 authored by @Jmencab: Handling Response.backUrl Undefined in AddToCart #95
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.

2 participants