How to Redirect to Cart When Shipping Address Changes in Magento

onepage-checkout

I want to redirect to cart from onepage checkout.

When customer change address postcode of shipping that time i want to redirect to cart page.

in this i am matching the postcode of billing and shipping if its not same than i want to redirect to cart page
in Onepagecontroller.php I am changing this in saveShipping method:

if($pcode == 0){

        $this->_redirect('checkout/cart');
}

but its not redirect to cart page,How can i do this?

Best Answer

Magento saves all checkout step data using AJAX.

Magento has a feature to go back to the cart page whenever an AJAX request goes to onFailure state during any of the checkout steps.

.....
method:'get',
            onFailure:this.ajaxFailure.bind(this),
            onComplete: function(){
 ....
.....
ajaxFailure: function(){
        location.href = this.failureUrl;
    },
......

An AJAX response calls the onFailure() function whenever a header response isHTTP/1.1 500 Internal Server Error`.

 $this->getResponse()->clearHeaders()->setHeader('HTTP/1.1', '500 Internal Server Error')->sendResponse();
 exit;

Or use 403 (permission denied):

$this->getResponse()->clearHeaders()->setHeader('HTTP/1.1', '403 Forbidden')->sendResponse()->sendResponse();
exit;

...Or 503 (service unavailable):

$this->getResponse()->clearHeaders()->setHeader('HTTP/1.1', '503 Service Unavailable')->sendResponse();
exit;

In your case you need to send a 500/403/503 header response code to ensure that the Prototype AJAX onFailure is called.

saveShipping() function saves shipping data using AJAX` and the response is the JSON content.

On receipt of this response the checkout is going to move to the next step.

That means that $this->_redirect('checkout/cart'); is not being run.

Related Topic