-
Notifications
You must be signed in to change notification settings - Fork 215
Fix: Wait for the product to be added to cart before redirecting customer to the cart page #6466
Conversation
Script Dependencies ReportThe
This comment was automatically generated by the |
|
Size Change: +23 B (0%) Total Size: 862 kB
ℹ️ View Unchanged
|
gigitux
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this PR!
I have some questions. Could you take a look?
| } else { | ||
| buttonProps.onClick = () => { | ||
| addToCart(); | ||
| dispatchStoreEvent( 'cart-add-item', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that the problem here is that addToCart returns a Promise and we are not waiting for it.
I commented the new code, and edited the function onClick, in this way:
buttonProps.onClick = async () => {
await addToCart();
dispatchStoreEvent( 'cart-add-item', {
product,
} );
const { cartRedirectAfterAdd } = getSetting( 'productsSettings' );
if ( cartRedirectAfterAdd ) {
window.location.href = CART_URL;
}
setClickedAddToCart( true );
};
}
It seems that works. Can you check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a great solution! Thanks so much! I updated the PR per your suggestion!
gigitux
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing comments! LGTM 🚢
Fixes #6390
This PR fixes the empty cart after adding a product to the cart issue by properly waiting for the product to be added to the cart before redirecting the customer to the cart page. By doing so, the cart will always contain the newly added products.
Accessibility
prefers-reduced-motionOther Checks
Screenshots
Testing
Automated Tests
User Facing Testing
WooCommerce Visibility
Performance Impact
Changelog