购物车控制器覆盖在magento 1.8中不起作用

时间:2014-05-19 17:37:02

标签: php magento

我必须尝试在magento中覆盖购物车控制器。但它似乎没有在magento 1.8中覆盖。我做了以下事情

添加了

等文件
Magento/app/code/local/MyNameSpace/MyCheckout/etc/config.xml
Magento/app/code/local/MyNameSpace/MyCheckout/controllers/CartController.php
Magento/app/etc/modules/MyNameSpace_MyCheckout.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>

CartController.php

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_Mycheckout_CartController extends Mage_Checkout_CartController
{
    /**
     * Add product to shopping cart action
     */
    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');

            /**
             * Check product availability
             */
            if (!$product) {
                $this->_goBack();
                return;
            }

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

            $cart->save();

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

            /**
             * @todo remove wishlist observer processAddToCart
             */
            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();
        }
    }
}

MyNameSpace_MyCheckout.xml

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

我发现"cartController.php"中没有文件Mage/Checkout/controllers/。在magento 1.8中,我只能在该目录中找到一个名为Action.php的文件,该文件只有一个抽象类。任何人都可以解释如何在magento 1.8中覆盖购物车控制器吗?

1 个答案:

答案 0 :(得分:1)

在config.xml文件中试用此代码

修改

<frontend>
    <routers>
        <mycheckout>
            <use>standard</use>
            <args>
               <module>MyNameSpace_Mycheckout</module>
               <frontName>mycheckout</frontName>
            </args>
        </mycheckout>
    </routers>
</frontend>
<global>
    <rewrite>
        <mycheckout> <!--unique tag-->
            <from><![CDATA[#^/checkout/cart/add#]]></from>
            <to>/mycheckout/cart/add</to>
        </mycheckout>
    </rewrite>
</global>

替换为此代码。

<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>