magento产品成功添加到购物车后跳转到不同页面 添加 add to cart 按钮

1 添加产品到购物车成功后是跳转到购物车页面或不跳转。这个在后台可以设置
magento产品成功添加到购物车后跳转到不同页面  添加 add to cart 按钮
system -》 configuration -》 After Adding a Product Redirect to
Shopping Cart
– Yes/No”
这个是设置成功添加产品后是跳转到购物车页面,还是不跳转

2 修改默认的跳转页面
可以在app\design\frontend\default\theme173\template\catalog\product\view.phtml

<input type="hidden" name="return_url"
value="yoururl" />
将url替换成要跳转的页面的url就可以了。

3 添加一个add to cart的按钮
两个按钮可以一个自动跳转到购物车页面,一个不跳转,中文网站中,一般有一个立即购买(跳转到购物车页面),一个添加到购物车(不跳转停留在原来页面)。

解决这个的办法就是重写控制器Checkout-CartController

1)创建模块目录和文件

  1. Magento/app/code/local/MyNameSpace/MyCheckout/etc/config.xml
  2. Magento/app/code/local/MyNameSpace/MyCheckout/controllers/CartController.php
  3. Magento/app/etc/modules/MyNameSpace_MyCheckout.xml

2)编辑Magento/app/code/local/MyNameSpace/MyCheckout/etc/config.xml

config.xml文件
<?xml version="1.0"?>
<config>
    <modules>
        <MyNameSpace_Mycheckout>
            <version>0.1.0</version>
        </MyNameSpace_Mycheckout>
    </modules>
    <frontend>
        <routers>
            <MyNameSpace_Mycheckout>
                <use>standard</use>
                <args>
                    <module>MyNameSpace_Mycheckout</module>
                    <frontName>mycheckout</frontName>
                </args>
            </MyNameSpace_Mycheckout>
        </routers>
    </frontend>
    <global>
        <routers>
            <checkout>
                <rewrite>
                    <cart>
                        <to>mycheckout/cart</to>
                        <override_actions>true</override_actions>
                        <actions>
                            <add>
                                <to>mycheckout/cart/add</to>
                            </add>
                        </actions>
                    </cart>
                </rewrite>
            </checkout>
        </routers>
    </global>
</config>

3)编辑

/controllers/CartController.php
Magento/app/code/local/MyNameSpace/MyCheckout/controllers/CartController.php
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_Mycheckout_CartController
extends Mage_Checkout_CartController
{
    
    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)编辑文件

Magento/app/etc/modules/MyNameSpace_MyCheckout.xml

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

5)编辑文件

Magento/app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml

<camelweb_mycheckout_cart_index>
    <update
handle="checkout_cart_index"/>
</camelweb_mycheckout_cart_index>

6)

在模版文件中添加 “Add to cart”按钮
view/addtocart.phtml
<?php $_product =
$this->getProduct()
?>
 
<?php if($_product->isSaleable()):
?>
    <div
class="add-to-cart">
        <?php
if(!$_product->isGrouped()):
?>
        <label
for="qty"><?php
echo $this->__('Qty:')
?></label>
        <input
type=
"text" name="qty"
id="qty" maxlength="12"
value="<?php echo
$this->getMinimalQty($_product)
?>"
title="<?php echo
$this->__('Qty') ?>"

class="input-text
qty"
/>
        <?php
endif;
?>
        <button
type=
"button" title="<?php echo
$this->__('Add to Cart') ?>"

class="button
btn-cart"
onclick="productAddToCartForm.submit()"><span><span><?php
echo $this->__('Add to Cart')
?></span></span></button>
        <br
/>

        <!--自定义添加的add
to cart的按钮-->
        <a
href=
"<?php echo
Mage::getUrl('checkout/cart/add', array('product' =>
$_product->getId(),
'noCheckoutYet'=>'yes'))
?>"
>Add to cart
2</a>
        <?php
echo $this->getChildHtml('', true, true)
?>
    </div>
<?php endif;
?>


7)清下缓存,然后后台设置下跳转或不跳转,或者根据本文的第二方法,添加下代码,让默认的不跳转,我们重新添加的add to cart
按钮不跳转

上一篇:easyui combotree下拉框多选赋值


下一篇:python基础之面向对象高级编程