【问题标题】:Overriding Mage_Wishlist_IndexController::addAction() in Magento在 Magento 中覆盖 Mage_Wishlist_IndexController::addAction()
【发布时间】:2014-09-09 08:51:23
【问题描述】:

我无法覆盖 Magento 的核心控制器之一,即愿望清单索引控制器。当我在愿望清单中添加产品时,我需要 Magento 重定向回产品页面而不是愿望清单。这是我到目前为止所做的

  1. 在 app/code/local 中创建了一个名为 MyCompany 的文件夹,其中包含一个名为 Coreextensions 的子文件夹。
  2. 在 Coreextensions 文件夹中,我创建了 etc/config.xml,内容如下:

不,此实例旨在在生产之外或在 RDS 免费使用层下使用

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_Coreextensions>
            <version>0.1.0</version>
        </MyCompany_Coreextensions>
    </modules>
 
    <frontend>
        <routers>
            <wishlist>
                <args>
                    <modules>
                        <MyCompany_Coreextensions before="Mage_Wishlist">
                            MyCompany_Coreextensions_Wishlist
                        </MyCompany_Coreextensions>
                    </modules>
                </args>
            </wishlist>
        </routers>
    </frontend>
</config>
  1. 在 Coreextensions 文件夹中,我创建了 controllers/Wishlist/IndexController.php,如下所示:
  <?php
    /* Stay on product page after adding to wishlist */
  require_once(Mage::getModuleDir('controllers','Mage_Wishlist').DS.'IndexController.php');
    
    class MyCompany_Coreextensions_Wishlist_IndexController extends Mage_Wishlist_IndexController
    {
      
    /**
     * Add the item to wish list
     *
     * @return Mage_Core_Controller_Varien_Action|void
     */
    protected function _addItemToWishList()
    {
        $wishlist = $this->_getWishlist();
        if (!$wishlist) {
            return $this->norouteAction();
        }
    
        $session = Mage::getSingleton('customer/session');
    
        $productId = (int)$this->getRequest()->getParam('product');
        if (!$productId) {
            $this->_redirect('*/');
            return;
        }
    
        $product = Mage::getModel('catalog/product')->load($productId);
        if (!$product->getId() || !$product->isVisibleInCatalog()) {
            $session->addError($this->__('Cannot specify product.'));
            $this->_redirect('*/');
            return;
        }
    
        try {
            $requestParams = $this->getRequest()->getParams();
            if ($session->getBeforeWishlistRequest()) {
                $requestParams = $session->getBeforeWishlistRequest();
                $session->unsBeforeWishlistRequest();
            }
            $buyRequest = new Varien_Object($requestParams);
    
            $result = $wishlist->addNewItem($product, $buyRequest);
            if (is_string($result)) {
                Mage::throwException($result);
            }
            $wishlist->save();
    
            Mage::dispatchEvent(
                'wishlist_add_product',
                array(
                    'wishlist' => $wishlist,
                    'product' => $product,
                    'item' => $result
                )
            );
    
            $referer = $session->getBeforeWishlistUrl();
            if ($referer) {
                $session->setBeforeWishlistUrl(null);
            } else {
                $referer = $this->_getRefererUrl();
            }
    
            /**
             *  Set referer to avoid referring to the compare popup window
             */
            $session->setAddActionReferer($referer);
    
            Mage::helper('wishlist')->calculate();
    
            $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.',
                $product->getName(), Mage::helper('core')->escapeUrl($referer));
            $session->addSuccess($message);
        } catch (Mage_Core_Exception $e) {
            $session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
        }
        catch (Exception $e) {
            $session->addError($this->__('An error occurred while adding item to wishlist.'));
        }
    
        //$this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
        $this->_redirectReferer();
    }
    
    }
  1. 在 app/etc/modules 中,我创建了 MyCompany_Coreextensions.xml,如下所示:

     <?xml version="1.0"?>
     <!--we need to enable this module as any other if-->
     <!--you wish to do it as standalone module extension-->
     <config>
         <modules>
             <MyCompany_Coreextensions>
                 <active>true</active>
                 <codepool>local</codepool>
             </MyCompany_Coreextensions>
         </modules>
     </config>
    

当然,这行不通,让我发疯。如果我在 Core 文件中进行更改,它会按我的意愿工作,但我不想更改 Core 文件......让我说是的,我确实清除了缓存!

【问题讨论】:

    标签: magento controller overriding overloading core


    【解决方案1】:

    如果你使用 Magento EE,你应该替换:

    <MyCompany_Coreextensions before="Mage_Wishlist">
    

    与:

    <MyCompany_Coreextensions before="Enterprise_Wishlist">
    

    【讨论】:

      【解决方案2】:

      你的步骤是对的,但只有两件事:

      1. 您必须在 Controller 类声明的第一个添加这一行 require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php';
      2. 你的 Controller 类的名字必须没有'_Wishlist' 所以MyCompany_Coreextensions_IndexController而不是MyCompany_Coreextensions_Wishlist_IndexController(你也必须把它改成config.xml -> &lt;MyCompany_Coreextensions before="Mage_Wishlist"&gt;MyCompany_Coreextensions&lt;/MyCompany_Coreextensions&gt;而不是@ 987654325@)

      【讨论】:

        【解决方案3】:

        此外,您还没有“包含”或“需要”愿望清单的原始(核心)IndexController,您必须在模块的索引控制器文件中执行如下操作:

        require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php';
        

        上述代码应放在模块的 IndexController.php 文件中的任何类声明之前。

        一旦您刷新 magento 缓存,然后尝试将产品添加到愿望清单,您将被重定向到以前的 url。

        【讨论】:

          【解决方案4】:
          < codepool >local< /codepool >
          

          如果还是不明白,就得把codepool改成codePool

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-02
            • 2014-12-31
            • 1970-01-01
            • 1970-01-01
            • 2013-10-15
            • 2012-11-27
            • 2011-02-25
            • 1970-01-01
            相关资源
            最近更新 更多