简体   繁体   中英

Add To Cart Error, product redirection issue - Magento

Overview:

On my product called Test Product has 10 quantity.

在此处输入图片说明

So when I put 40 quantity and press add to cart. (Remember, the quantity of the product is 10, so it should prompt an error)

在此处输入图片说明

The output is correct, the user got notified by the system that they put greater than the quantity than the actual quantity of the product.

在此处输入图片说明

If you look at the URL closely, before the user click the add to cart, the URL is

在此处输入图片说明

When the error has been shown, the URL is

在此处输入图片说明

Meaning, magento removes the category link and redirects to the actual product link.

Question Is there any way for magento to redirect to the current category instead of the product link?

Both redirecting to the category or the product with category included in the url is not possible by default. You need to write a module for that.

Let's have a look where the redirection is done in the code, so the behaviour can be modified. Starting from the cart controller products are added to cart in Mage_Checkout_CartController::addAction() . The product is added with

$cart   = $this->_getCart();
...
$cart->addProduct($product, $params);

Having a closer look at Mage_Checkout_Model_Cart::addProduct() the redirection url for products with insufficient stock amount is set here:

/**
* String we can get if prepare process has error
*/
if (is_string($result)) {
$redirectUrl = ($product->hasOptionsValidationFail())
    ? $product->getUrlModel()->getUrl(
        $product,
        array('_query' => array('startcustomization' => 1))
    )
    : $product->getProductUrl();
$this->getCheckoutSession()->setRedirectUrl($redirectUrl);
if ($this->getCheckoutSession()->getUseNotice() === null) {
    $this->getCheckoutSession()->setUseNotice(true);
}
Mage::throwException($result);
}

The product here is loaded without the category information so the categories aren't part of this url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM