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

Fix: issue-522 #638

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 26 additions & 1 deletion src/js/components/UseHandleCartAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* file that was distributed with this source code.
*/

import SelectorsMap from '@constants/selectors-map';
import useAlert from './useAlert';

const handleCartAction = (event: Event): void => {
event.stopPropagation();
event.preventDefault();
Expand Down Expand Up @@ -33,11 +36,33 @@ const sendCartRefreshRequest = (target: HTMLElement): void => {
body: formData,
})
.then((resp: Response) => {
// Refresh cart preview
// Refresh cart preview
prestashop.emit(events.updateCart, {
reason: dataset,
resp,
});

// Show product removal success alert
if (target && target.getAttribute('data-link-action') === SelectorsMap.cart.deleteLinkAction) {
const alertPlaceholder = document.querySelector(SelectorsMap.cart.alertPlaceholder);
const productUrl = target.getAttribute('data-product-url');
const productName = target.getAttribute('data-product-name');

if (alertPlaceholder && productUrl && productName) {
const alertText = alertPlaceholder.getAttribute('data-alert');
const productLink = `<a class="alert-link" href="${productUrl}">${productName}</a>`;
const alertMessage = `${productLink} ${alertText}`;

if (alertMessage) {
const alert = useAlert(alertMessage, {
type: 'success',
selector: SelectorsMap.cart.alertPlaceholder,
});

alert.show();
}
}
}
})
.catch((err) => {
const errorData = err as Response;
Expand Down
1 change: 1 addition & 0 deletions src/js/constants/selectors-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const cart = {
productQuantity: '.cart__items .js-quantity-button',
productItem: '.cart__item',
removeFromCartLink: '.remove-from-cart',
alertPlaceholder: '.js-cart-update-alert',
};

export const blockcart = {
Expand Down
4 changes: 0 additions & 4 deletions src/scss/core/components/_alert.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.alert {
&.fade:not(.show) {
@extend .visually-hidden;
}

p,
ul,
ol {
Expand Down
5 changes: 4 additions & 1 deletion templates/checkout/_partials/cart-detailed-product-line.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@
<a class="remove-from-cart" rel="nofollow" href="{$product.remove_from_cart_url}"
data-link-action="delete-from-cart" data-id-product="{$product.id_product|escape:'javascript'}"
data-id-product-attribute="{$product.id_product_attribute|escape:'javascript'}"
data-id-customization="{$product.id_customization|escape:'javascript'}">
data-id-customization="{$product.id_customization|escape:'javascript'}"
data-product-url="{$product.url|escape:'javascript'}"
data-product-name="{$product.name|escape:'javascript'}"
>
{l s='Remove' d='Shop.Theme.Checkout'}
</a>
{/if}
Expand Down
2 changes: 2 additions & 0 deletions templates/checkout/cart.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<!-- cart products detailed -->
<div class="cart-container mb-3">
<div class="js-cart-update-alert" data-alert="{l s='has been removed from the cart.' d='Shop.Theme.Actions' js=1}"></div>

{block name='cart_overview'}
{include file='checkout/_partials/cart-detailed.tpl' cart=$cart}
{/block}
Expand Down
Loading