自定义单页控制器不会覆盖

时间:2016-04-01 08:38:29

标签: php magento

我正在使用magneto 1.6,我正在尝试使用本地池中的自定义控制器重写核心Onepagecontroller。

它的工作到目前为止,但是当我安装其他支付模块时,也使用自定义Onepagecontroller,'它不能正常工作'。

Magneto没有读取我的自定义OnepageController,磁铁只是从其他模块读取自定义OnepageController

这是我的文件结构和代码:

我的自定义OnepageController: \ app \ code \ local \ Sprint \ Migs \ controllers \ Checkout \ OnepageController.php

config.xml文件: \ app \ code \ local \ Sprint \ Migs \ etc \ config.xml

    <?xml version="1.0"?>
<config>
    <modules>
      <Sprint_Migs>
          <version>1.0.0</version>
      </Sprint_Migs>
    </modules>
    <global>
      <models>
          <migs>
            <class>Sprint_Migs_Model</class>
          </migs>
      </models>
    <helpers>
      <migs>
        <class>Sprint_Migs_Helper</class>
      </migs>
    </helpers>
    <blocks>
          <migs>
            <class>Sprint_Migs_Block</class>
          </migs>
      </blocks>
      <resources>
            <migs_setup>
                <setup>
                    <module>Sprint_Migs</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </migs_setup>
            <migs_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </migs_write>
            <migs_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </migs_read>
        </resources>
    </global>
    <default>
      <payment>
          <migs>
            <model>migs/migs</model>
            <active>1</active>
            <order_status>pending</order_status>
            <title>VISA / MasterCard</title>
            <allowspecific>0</allowspecific>
            <sort_order>1</sort_order>
          </migs>
      </payment>
  </default>
    <frontend>
      <layout>
            <updates>
                <migs module="Sprint_Migs">
                    <file>migs.xml</file>
                </migs>
            </updates>
        </layout>
      <routers>
          <migs>
            <use>standard</use>
            <args>
                <module>Sprint_Migs</module>
                <frontName>migs</frontName>
            </args>
          </migs>
          <checkout>
                <use>standard</use>
                <args>
                    <modules>
                        <Sprint_Migs_Checkout before="Mage_Checkout">Sprint_Migs_Checkout</Sprint_Migs_Checkout>
                    </modules>
                </args>
            </checkout>
      </routers>
    </frontend>
</config>

这是我的自定义单页控制器

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'OnepageController.php';
class Sprint_Migs_Checkout_OnepageController extends Mage_Checkout_OnepageController {

    public function saveOrderAction()
    {
        if ($this->_expireAjax()) {
            return;
        }

        $result = array();
        try {
            if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
                $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
                if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
                    $result['success'] = false;
                    $result['error'] = true;
                    $result['error_messages'] = $this->__('Please agree to all the terms and conditions before placing the order.');
                    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                    return;
                }
            }
            if ($data = $this->getRequest()->getPost('payment', false)) {
                if ($data['method'] == 'migs'){
                    $fields = array();
                    $fields['quoteID'] = Mage::getSingleton('checkout/session')->getQuoteId();
                    $fields['siteID'] = $data['site_id'];

                    $write = Mage::getSingleton( 'core/resource' )->getConnection( 'core_write' );
                    $table = Mage::getSingleton( 'core/resource' )->getTableName( 'sprint_migs' );
                    $write->insert($table, $fields);        
                }
                $this->getOnepage()->getQuote()->getPayment()->importData($data);
            }
            $this->getOnepage()->saveOrder();


            $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
            $result['success'] = true;
            $result['error']   = false;
        } catch (Mage_Payment_Model_Info_Exception $e) {
            $message = $e->getMessage();
            if( !empty($message) ) {
                $result['error_messages'] = $message;
            }
            $result['goto_section'] = 'payment';
            $result['update_section'] = array(
                'name' => 'payment-method',
                'html' => $this->_getPaymentMethodsHtml()
            );
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
            Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
            $result['success'] = false;
            $result['error'] = true;
            $result['error_messages'] = $e->getMessage();

            if ($gotoSection = $this->getOnepage()->getCheckout()->getGotoSection()) {
                $result['goto_section'] = $gotoSection;
                $this->getOnepage()->getCheckout()->setGotoSection(null);
            }

            if ($updateSection = $this->getOnepage()->getCheckout()->getUpdateSection()) {
                if (isset($this->_sectionUpdateFunctions[$updateSection])) {
                    $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
                    $result['update_section'] = array(
                        'name' => $updateSection,
                        'html' => $this->$updateSectionFunction()
                    );
                }
                $this->getOnepage()->getCheckout()->setUpdateSection(null);
            }
        } catch (Exception $e) {
            Mage::logException($e);
            Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
            $result['success']  = false;
            $result['error']    = true;
            $result['error_messages'] = $this->__('There was an error processing your order. Please contact us or try again later.');
        }
        $this->getOnepage()->getQuote()->save();

        if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }   
}

2 个答案:

答案 0 :(得分:1)

您在config.xml中做得很好,但最好删除结帐时的<use>standard</use>节点。

另外,您是否在课前要求原始控制器?它应该是这样的:

<?php

require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'OnepageController.php';

class Sprint_Migs_Checkout_OnepageController 
           extends Mage_Checkout_OnepageController
{
    /* Your New / Overrided Functions */
}

答案 1 :(得分:1)

问题解决了,我使用观察者事件也捕获自定义字段并保存到自定义表

但我只是想知道,使用观察者事件和覆盖一个页面控制器之间有什么不同?