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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/js/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ initEmitter();
$(() => {
const {prestashop, Theme: {events}} = window;

// @TODO: Fix this on core.js side inside a major version of PrestaShop instead of minor
// For reference: https://github.com/PrestaShop/hummingbird/pull/418#discussion_r1061938669
document.querySelectorAll<HTMLInputElement>('[name="token"]').forEach((el) => {
el.value = prestashop.static_token;
});

initProductBehavior();
initQuickview();
initCheckout();
Expand Down
24 changes: 24 additions & 0 deletions templates/catalog/_partials/miniatures/product.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@
{/block}
</div>

{if $product.add_to_cart_url}
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
<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

<div class="quantity-button js-quantity-button">
{include file='components/qty-input.tpl'
attributes=[
"id"=>"quantity_wanted_{$product.id_product}",
"value"=>"1",
"min"=>"{if $product.quantity_wanted}{$product.minimal_quantity}{else}1{/if}"
]
marginHelper="mb-0"
}
</div>
<button data-button-action="add-to-cart" class="btn btn-primary ms-3">
<i class="material-icons">&#xe854;</i>
<span class="visually-hidden">{l s='Add to cart' d='Shop.Theme.Actions'}</span>
</button>
</form>
{else}
<a href="{$product.url}" class="btn btn-outline-primary mt-3">
{l s='See details' d='Shop.Theme.Actions'}
</a>
{/if}
</div>
</div>
{/block}
Expand Down
2 changes: 1 addition & 1 deletion templates/components/qty-input.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{assign var="append" value=["button"=>"increment", "icon"=>$increment_icon, "confirm_icon"=>$submit_icon]}
{/if}

<div class="input-group flex-nowrap mb-3">
<div class="input-group flex-nowrap{if isset($marginHelper)} {$marginHelper}{else} mb-3{/if}">
<button class="btn {$prepend.button} js-{$prepend.button}-button" type="button">
<i class="material-icons">&#x{$prepend.icon};</i>
<i class="material-icons confirmation d-none">&#x{$prepend.confirm_icon};</i>
Expand Down