最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

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

Magento 资料整理 mingyue 5614浏览

在产品详细页新增一个添加到购物车按钮,从而实现一个按钮添加到购物车而不跳转,一个按钮添加到购物并跳转。要实现如下功能,需要重写控制器 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文件,加入如下代码:

<?xml version="1.0"?>
<config>
<modules>
<Mingyue_Shopping>
<version>0.1.0</version>
</Mingyue_Shopping>
</modules>
<global>
<rewrite>
<Mingyue_Shopping_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<to>/shopping/cart/</to>
</Mingyue_Shopping_cart>
</rewrite>
<routers>
<checkout>
<rewrite>
<cart>
<to>shopping/cart</to>
<override_actions>true</override_actions>
<actions>
<add>
<to>shopping/cart/add</to>
</add>
</actions>
</cart>
</rewrite>
</checkout>
</routers>
</global>
<frontend>
<routers>
<Mingyue_Shopping>
<use>standard</use>
<args>
<module>Mingyue_Shopping</module>
<frontName>shopping</frontName>
</args>
</Mingyue_Shopping>
</routers>
</frontend>
</config>

3.改写自己的控制器

?<?php

require_once 'Mage/Checkout/controllers/CartController.php';
class Mingyue_Shopping_CartController extends Mage_Checkout_CartController
{
#覆写indexAction方法
public function indexAction()
{
# Just to make sure
// echo ('成功重写购物车!');
parent::indexAction();
}
public function addAction()
{
$cart?? = $this->_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文件编辑如下:

<?xml version="1.0"?>
<config>
<modules>
<Mingyue_Shopping>
<active>true</active>
<codePool>local</codePool>
</Mingyue_Shopping>
</modules>
</config>

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


<Mingyue_shopping_cart_index>

<update handle="checkout_cart_index"/>

</Mingyue_shopping_cart_index>

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


<div class="buy">
<div class="buy_link">
<button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="buynow" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $this->__('Buy it now') ?></span></span></button>

<!--新增添加到购物车-->
<a type="button" title="<?php echo $buttonTitle;?>" onclick="productAddToCartForm.submit(this)" class="addcart button btn-cart" href="<?php echo Mage::getUrl('checkout/cart/add', array('product' => $_product->getId(), 'noCheckoutYet'=>'yes')) ?>"><span><span><i></i><?php echo $buttonTitle?></span></span></a>

</div>
<?php echo $this->getChildHtml('', true, true) ?>
</div>

至此成功,效果图如下:

addtocart

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

蜀ICP备15020253号-1