0

I have a requirement to clear completed form fields in Magento shopping cart if the cart is emptied. At the moment I have enabled persistent shopping cart but if the cart is emptied after customer info has been entered in the billing address fields etc I need to somehow clear the form also.

I have tried adding:

//session_destroy();

To my EmpyCart controller but it doesn't work

<?php

class MyModule_EmptyCart_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        //Get cart helper
        $cartHelper = Mage::helper('checkout/cart');

        //Get all items from cart
        $items = $cartHelper->getCart()->getItems();

        //Loop through all of cart items
        foreach ($items as $item) {
            $itemId = $item->getItemId();
            //Remove items, one by one
            $cartHelper->getCart()->removeItem($itemId)->save();
        }

        //Redirect back to cart or wherever ou wish
        $this->_redirect('checkout/cart');
            Mage::getSingleton('checkout/session')->clear();
    }

    public function addAllAction()
    {
        //Get cart helper

        $cartHelper = Mage::helper('checkout/cart');

        //Get all items from cart
        $items = $cartHelper->getCart()->getItems();

        //Loop through all of cart items
        foreach ($items as $item) {
            $itemId = $item->getItemId();
            //Remove items, one by one
            $cartHelper->getCart()->removeItem($itemId)->save();
        }

        //Redirect back to cart or wherever ou wish
        $this->_redirect('checkout/cart');
            Mage::getSingleton('checkout/session')->clear();
    }
}

Is there a simpler way to empty the fields without disabling persistence?

3

Try clearing the checkout session.

Mage::getSingleton('checkout/session')->clear();
| improve this answer | |
  • Right first time! – user1704524 Jun 6 '13 at 20:05

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.