Redirect Magento to checkout onepage after adding item to cart

前提是你 提交于 2019-12-18 17:37:14

问题


How can I redirect the user after it adds one item to the cart?
lets say I want him to choose one item and go to the checkout/onepage, how can I do that?


回答1:


You could create an observer listening for the checkout_cart_add_product_complete event and in there you could do something like the following

  public function addToCartComplete(Varien_Event_Observer $observer) {
    // Send the user to the Item added page
    $response = $observer->getResponse();
    $request = $observer->getRequest();
    $response->setRedirect(Mage::getUrl('checkout/onepage'));
    Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
}

Your config would look something like this

 <frontend>
    <events>
    <checkout_cart_add_product_complete>
      <observers>
        <packagename_modulename_observer>
          <type>singleton</type>
          <class>packagename_modulename/observer</class>
          <method>addToCartComplete</method>
        </packagename_modulename_observer>
      </observers>
      </checkout_cart_add_product_complete>
   </events>
   </frontend>



回答2:


Using checkout_cart_add_product_complete you will miss out on product addtocart success message check this

Mage::dispatchEvent('checkout_cart_add_product_complete',
                    array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
                );

if (!$this->_getSession()->getNoCartRedirect(true)) {
 if (!$cart->getQuote()->getHasError()) {
         $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
               $this->_getSession()->addSuccess($message);
     }
       $this->_goBack();
}

Again if you don't wont the session messages, just pass additional parameter from your product page

<input type="hidden" name="return_url" value="<?php echo $this->getUrl('checkout/onepage')?>"/>

No need for any other customization or module creation, its magento's default functionality



来源:https://stackoverflow.com/questions/4052302/redirect-magento-to-checkout-onepage-after-adding-item-to-cart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!