0

How to make yes or no confirmation popup after clicking the clear shopping cart. Now it's clearing the cart without confirmation.

/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**  @var $block \Magento\Checkout\Block\Cart */
<?php $mergedCells = ($this->helper('Magento\Tax\Helper\Data')->displayCartBothPrices() ? 2 : 1); ?>
<?php echo $block->getChildHtml('form_before') ?>
<form action="<?php /* @escapeNotVerified */ echo $block->getUrl('checkout/cart/updatePost') ?>"
      method="post"
      id="form-validate"
      data-mage-init='{"validation":{}}'
      class="form form-cart">
<?php echo $block->getBlockHtml('formkey'); ?>
<div class="cart table-wrapper<?php echo $mergedCells == 2 ? ' detailed' : ''; ?>">
    <table id="shopping-cart-table"
           class="cart items data table"
           data-mage-init='{"shoppingCart":{"emptyCartButton": "action.clear",
           "updateCartActionContainer": "#update_cart_action_container"}}'>
        <caption role="heading" aria-level="2" class="table-caption"><?php /* @escapeNotVerified */ echo __('Shopping Cart Items') ?></caption>
        <thead>
            <tr>
                <th class="col item" scope="col"><span><?php /* @escapeNotVerified */ echo __('Item') ?></span></th>
                <th class="col price" scope="col"><span><?php /* @escapeNotVerified */ echo __('Price') ?></span></th>
                <th class="col qty" scope="col"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></th>
                <!--<th class="col subtotal" scope="col"><span><!--?php /* @escapeNotVerified */ echo __('Subtotal') ?></span></th>-->
            </tr>
        </thead>
        <?php foreach ($block->getItems() as $_item): ?>
            <?php echo $block->getItemHtml($_item) ?>
        <?php endforeach ?>
    </table>
</div>
<div class="cart main actions">
    <?php if ($block->getContinueShoppingUrl()): ?>
        <a class="action continue"
           href="<?php echo $block->escapeUrl($block->getContinueShoppingUrl()) ?>"
           title="<?php echo $block->escapeHtml(__('Continue Shopping')); ?>">
            <span><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?></span>
        </a>
    <?php endif; ?>
    <button type="submit"
            name="update_cart_action"
            data-cart-empty=""
            value="empty_cart"
            title="<?php echo $block->escapeHtml(__('Clear Shopping Cart')); ?>"
            class="action clear" id="empty_cart_button">
        <span><?php /* @escapeNotVerified */ echo __('Clear Shopping Cart'); ?></span>
    </button>
    <button type="submit"
            name="update_cart_action"
            data-cart-item-update=""
            value="update_qty"
            title="<?php echo $block->escapeHtml(__('Update Shopping Cart')); ?>"
            class="action update">
        <span><?php /* @escapeNotVerified */ echo __('Update Shopping Cart'); ?></span>
    </button>
    <input type="hidden" value="" id="update_cart_action_container" data-cart-item-update=""/>
</div>
</form>
<?php echo $block->getChildHtml('checkout.cart.order.actions') ?>
<?php echo $block->getChildHtml('shopping.cart.table.after'); ?>
0

You can directly use confirm on onClick event of clear shopping cart button like below:

<button type="submit"
        name="update_cart_action"
        data-cart-empty=""
        value="empty_cart"
        title="<?php echo $block->escapeHtml(__('Clear Shopping Cart')); ?>"
        class="action clear" id="empty_cart_button"
        onclick="confirm("Are you sure want to clear cart");"
        >
    <span><?php /* @escapeNotVerified */ echo __('Clear Shopping Cart'); ?></span>
</button>

I hope this will help you.

0

@hardik's answer works but you still need to add:

onclick="if(confirm('Are you sure you want to clear cart?')){}else{
    event.preventDefault()};"

To his answer to stop the button from still submitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.