产品详细页新增一个添加到购物车按钮

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

在产品详细页新增一个添加到购物车按钮,从而实现一个按钮添加到购物车而不跳转,一个按钮添加到购物并跳转。要实现如下功能,需要重写控制器 CartController.php .

第一步创建自己的代码空间,我这里用的是Mingyue.

1.首先在Mingyue创建新的模块,依次创建的文件如下:

/app/code/local/Mingyue/Shopping

/app/code/local/Mingyue/Shopping/etc

/app/code/local/Mingyue/Shopping/etc/config.xml

/app/code/local/Mingyue/Shopping/controllers

/app/code/local/Mingyue/Shopping/controllers/CartController.php

2.编辑/app/code/local/Mingyue/Shopping/etc/config.xml文件,加入如下代码:

0.1.0/shopping/cart/shopping/carttrueshopping/cart/addstandardMingyue_Shoppingshopping

3.改写自己的控制器

?_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}

$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');


if (!$product) {
$this->_goBack();
return;
}

$cart->addProduct($product, $params);
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}

$cart->save();

$this->_getSession()->setCartWasUpdated(true);


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')->htmlEscape($product->getName()));
$this->_getSession()->addSuccess($message);
}
if ($returnUrl = $this->getRequest()->getParam('return_url')) {
// clear layout messages in case of external url redirect
if ($this->_isUrlInternal($returnUrl)) {
$this->_getSession()->getMessages(true);
}
$this->getResponse()->setRedirect($returnUrl);
} elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
&& !$this->getRequest()->getParam('in_cart')
&& $backUrl = $this->_getRefererUrl()) {

$this->getResponse()->setRedirect($backUrl);
} else {
if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
$this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
}

if($this->getRequest()->getParam('noCheckoutYet')=="yes")
$this->getResponse()->setRedirect($this->_getRefererUrl());
else
$this->_redirect('checkout/cart');
}
}
}
catch (Mage_Core_Exception $e) {
if ($this->_getSession()->getUseNotice(true)) {
$this->_getSession()->addNotice($e->getMessage());
} else {
$messages = array_unique(explode("n", $e->getMessage()));
foreach ($messages as $message) {
$this->_getSession()->addError($message);
}
}

$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
$this->getResponse()->setRedirect($url);
} else {
$this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}
}
catch (Exception $e) {
$this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
$this->_goBack();
}
}
}

?>

4.在app/etc/modules/创建Mingyue_All.xml文件编辑如下:

truelocal

5.修改视图文件app/design/frontend/rwd/mingyue/layout/checkout.xml,在layout标签中,添加下面内容:

6.修改文件app/design/frontend/rwd/mingyue/template/catalog/product/view/addtocart.phtml,修改为如下:


getChildHtml('', true, true) ?>

至此成功,效果图如下:

转载请注明:(●–●) Hello.My Weicot » 产品详细页新增一个添加到购物车按钮

文章来源于互联网:产品详细页新增一个添加到购物车按钮

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。