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

Add an add to cart button to the product list #418

Merged
merged 7 commits into from
Jan 6, 2023

Conversation

NeOMakinG
Copy link
Contributor

@NeOMakinG NeOMakinG commented Jan 2, 2023

Questions Answers
Description? We want to add an add to cart button to the product list, if child themes wants to remove it, they can override product.tpl and remove that markup
Type? new feature
BC breaks? no
Deprecations? no
How to test? Go on product lists and try to add some products from there
Possible impacts? Product list

image

{if $product.add_to_cart_url}
<form action="{$urls.pages.cart}" method="post" class="d-flex align-items-center mt-3">
<input type="hidden" value="{$product.id_product}" name="id_product">
<input type="hidden" name="token" value="{$static_token}" />
Copy link
Contributor

@kpodemski kpodemski Jan 2, 2023

Choose a reason for hiding this comment

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

This may won't for the product lists on the homepage from the cached modules, there's a need to have this done via JS to ensure the token is correct.

CC @Oksydan

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, @kpodemski is right.
Block might be cached and we should make sure that token provided to request is correct @NeOMakinG.
We could simply replace static_token value with prestashop.static_token global variable here https://github.com/PrestaShop/PrestaShop/blob/develop/themes/_core/js/cart.js#L101 with use of https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams or regex replace (might be tricky).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Oksydan do you think it's possible to somehow manage it on the theme side?

Copy link
Contributor

@Oksydan Oksydan Jan 5, 2023

Choose a reason for hiding this comment

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

@NeOMakinG you can make some dirty tricks like:

document.querySelectorAll('[name="token"]').forEach(el => {
  el.value = prestashop.static_token
})

That's major disadvantage of using core.js 😕

EDIT:
I just realized that you can ofc create you own function that will handle add to cart process but that's some extra code to maintain with core.js changes. Still no recommended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NeOMakinG you can make some dirty tricks like:

document.querySelectorAll('[name="token"]').forEach(el => {
  el.value = prestashop.static_token
})

That's major disadvantage of using core.js 😕

EDIT: I just realized that you can ofc create you own function that will handle add to cart process but that's some extra code to maintain with core.js changes. Still no recommended.

I would probably prefer that dirty tricks over changing the corejs behavior on a minor version

@Hlavtox
Copy link
Contributor

Hlavtox commented Jan 3, 2023

We need to make sure that it works for products with limited stock. If you do this on current Prestashop, have 1 item in stock and you add 2 pieces, you get empty modal.

@NeOMakinG
Copy link
Contributor Author

We need to make sure that it works for products with limited stock. If you do this on current Prestashop, have 1 item in stock and you add 2 pieces, you get empty modal.

@nicosomb is taking over @mparvazi 's PR related to this

@SharakPL SharakPL merged commit 8b69e0c into PrestaShop:develop Jan 6, 2023
sudo-do added a commit to sudo-do/hummingbird that referenced this pull request Jan 28, 2023
…oduct-list"

This reverts commit 8b69e0c, reversing
changes made to f6e7702.
sudo-do added a commit to sudo-do/hummingbird that referenced this pull request Jan 28, 2023
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.

5 participants