0

I want to redirect customer using controller when button is clicked in one of my phtml files.

So far I got, controller:

public function execute(){
    return $this->addToCart();
}

public function addToCart(){
    // some logic
    $this->_redirect('checkout/cart/index');
}

The above is working fine (redirecting) when you access the controller via URL (www.mydomain.com/batchorder/index/addtocart).

And here is my .phtml file:

$("#addToCart").click(function(){
let controllerUrl = "<?php echo $block-getUrl('batchorder/index/addtocart'); ?>"
$.post(controllerUrl,
{
"products": JSON.stringify(sideCart.SideCartProducts)
})

The post request returns my logic as it should, however, it is not redirecting. Anyone can help?

0

try replace "post" to this:

window.location.href=controllerUrl;

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.