Skip to content

Commit

Permalink
Merge pull request #638 from tblivet/issue-522
Browse files Browse the repository at this point in the history
Fix: issue-522
  • Loading branch information
tblivet authored Jun 12, 2024
2 parents 251ae15 + ff7ea90 commit 824f925
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
37 changes: 36 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,43 @@ 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');

// Create the product link element
const productLink = document.createElement('a');
productLink.classList.add('alert-link');
productLink.setAttribute('href', productUrl);
productLink.textContent = productName;

// Create the alert message container
const alertMessage = document.createElement('span');
alertMessage.appendChild(productLink);
alertMessage.append(` ${alertText}`);

const alertMessageContainer = document.createElement('div');
alertMessageContainer.appendChild(alertMessage);

const alert = useAlert(alertMessageContainer.innerHTML, {
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:'htmlall':'UTF-8'}"
>
{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

0 comments on commit 824f925

Please sign in to comment.