0

我正在做一个 magento 定制站点我需要将某些产品详细信息添加到数据库中,所以我在 Mage 结帐控制器中使用了函数 addAction(),为了使其成为一个模块,我必须覆盖 cartController 的 Addaction()

我已经引用了 stackoverflow 的最重要的控制器问题,但没有使用它使用 Mage 结帐控制器中的编码

我用过下面的代码,

code\local\SmartGrowth\CompatibleWith\etc

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <SmartGrowth_CompatibleWith>
            <version>0.1.0</version>
        </SmartGrowth_CompatibleWith>
    </modules>
<!-- Configure our module's behavior in the global scope -->
 <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_view>SmartGrowth_CompatibleWith_Block_CompatibleWith</product_view>
                </rewrite>
            </catalog>
        </blocks>

    </global>

    <frontend>
        <routers>
            <checkout>
               <use>standard</use>

                    <modules>
                        <SmartGrowth_CompatibleWith_Checkout  before="Mage_Checkout">SmartGrowth_CompatibleWith_Checkout</SmartGrowth_CompatibleWith_Checkout>
                    </modules>

            </checkout>
        </routers>

    </frontend>

</config>

app\code\local\SmartGrowth\CompatibleWith\controllers\Checkout

  require_once 'Mage/Checkout/controllers/CartController.php';
class SmartGrowth_CompatibleWith_Checkout_CartController extends Mage_Core_Controller_Front_Action
{

    public function addAction()
    {

        $cart   = $this->_getCart();

        $params = $this->getRequest()->getParams();
        $postData = Mage::app()->getRequest()->getPost();

            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();
            $cart_id = $cart->getQuote()->getId();

            $product_id=Mage::helper('core')->escapeHtml($product->getId());

           $write = Mage::getSingleton('core/resource')->getConnection('core_write');
           $readresult=$write->query("SELECT item_id FROM  mage_sales_flat_quote_item WHERE quote_id='$cart_id' AND product_id='$product_id'");
           $row = $readresult->fetch() ;
            $item_id=$row['item_id'];
             $brand = Mage::getSingleton('core/session')->getMyModelbrandData();

            if($postData['check_value']==1)
            {
                $myData   = Mage::getSingleton('core/session')->getMyModelTypeData();
               $myDataver = Mage::getSingleton('core/session')->getMyModelVersionData();

            }else if($postData['check_value']==2)
            {
                 $model_type = $postData['model_type']; 
                 $myDataver  = Mage::getSingleton('core/session')->getMyModelVersionData();

            }else if($postData['check_value']==3)
            {
                $model_version =$postData['model_version'];
                $myData   = Mage::getSingleton('core/session')->getMyModelTypeData();

            }else if($postData['check_value']==4)
            {
                $model_type    =$postData['model_type']; 
                $model_version =$postData['model_version'];
            }

            /*if($postData['model_type']!=''&& $postData['model_version']!='')
            {
                $model_type    =$postData['model_type']; 
                $model_version =$postData['model_version'];
            }else
            {
            $myData = Mage::getSingleton('core/session')->getMyModelTypeData();
            $myDataver = Mage::getSingleton('core/session')->getMyModelVersionData();   
            }*/
            if($myData !=''):
              // now $write is an instance of Zend_Db_Adapter_Abstract
              $readresult=$write->query("SELECT * FROM mage_eav_attribute_option_value WHERE option_id='$myData'");
              $row = $readresult->fetch() ;
               $model_type=$row['value'];
               endif;

               if($myDataver !=''):
                $readresult1=$write->query("SELECT * FROM mage_eav_attribute_option_value WHERE option_id='$myDataver'");
                $row1 = $readresult1->fetch() ;
                $model_version=$row1['value'];
                endif;

            $write->beginTransaction();  
            $fields = array();  
            $fields['model_type']    = $model_type;  
            $fields['model_version'] = $model_version;  
            $fields['brand']         = $brand;
            $where = $write->quoteInto('item_id =?', $item_id);  
            $write->update('mage_sales_flat_quote_item', $fields, $where);  
            $write->commit();

            /**
             * @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')->escapeHtml($product->getName()));
                    $this->_getSession()->addSuccess($message);
                }
                $this->_goBack();
            }
        } catch (Mage_Core_Exception $e) {
            if ($this->_getSession()->getUseNotice(true)) {
                $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $this->_getSession()->addError(Mage::helper('core')->escapeHtml($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.'));
            Mage::logException($e);
            $this->_goBack();
        }
    }
}
4

1 回答 1

0

而不是使用覆盖方法您可以为 magento 事件编写观察者。

代码\本地\SmartGrowth\CompatibleWith\etc

 <frontend>
             <events>
                <checkout_cart_add_product_complete>
                    <observers>
                       <SmartGrowth_CompatibleWith_Model_Observer>
                          <type>singleton</type>
                          <class>SmartGrowth_CompatibleWith_Model_Observer</class>
                          <method>cartproductcomplete</method>
                       </SmartGrowth_CompatibleWith_Model_Observer>
                   </observers>
                </checkout_cart_add_product_complete>
            </events>
          </frontend>

并在 code\local\SmartGrowth\CompatibleWith\Model\Observer.php 中编写观察者代码

class SmartGrowth_CompatibleWith_Model_Observer {

public function cartproductcomplete() {

//Database operation
}


}

使用事件比使用覆盖方法更好

于 2013-01-29T04:54:21.970 回答