如何在Magento中添加自定义字段?

时间:2012-07-31 09:59:08

标签: magento

我想在Magento的一个页面结帐中添加一个新的自定义字段。我用安装程序创建了一个模块:

$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('order', 'deliverydate', array(
    'position'      => 1,
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'Choose delivery date',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    'global'        => 1,
    'visible_on_front'  => 1,
));

$installer->endSetup();

我可以通过phpmyadmin看到该字段已添加到mage_eav_attribute表中。我还将此添加到shipping-method.phtml:

<div class="form-list field">
   <label for="deliverydate"><?php echo $this->__('Choose delivery date') ?></label>
       <div class="input-box">
      <input type="text" name="deliverydate" id="deliverydate" title="<?php echo $this->__('deliverydate') ?>" class="input-text" />
   </div>
</div>

当我下订单时,自定义字段将不会保存。如何使它工作?我已经用这种方式向客户添加了一些自定义字段,它们工作正常。我还可以在admin / customer上自动查看我的自定义客户字段,但无法在admin / sales_order中看到我的自定义订单字段。谢谢

* 修改

这就是我所做的:

我将observer.php添加到companyname / module / model / observer.php

class Company_Module_Model_Observer
{
    public function Deliverydate($observer){
        //get event data
        $event = $observer->getEvent();

                //get order
        $order = $event->getOrder();

        //set the country here
        $order->setDeliverydate('11.11.2012');
        //echo "observer";
        }   
}

这是我的config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Company_Module>
      <version>0.1.0</version>
    </Company_Module>
  </modules>
  <global>
  <resources>
    <Company_Module_setup>
     <setup>
      <module>Company_Module</module>
      <class>Company_Module_Model_Resource_Mysql4_Setup</class>
     </setup>
    </Company_Module_setup>
</resources>
       <events>
            <checkout_type_onepage_save_order>
                <observers>
                    <Company_Module_Model_Observer>
                        <type>singleton</type>
                        <class>Company_Module_Model_Observer</class>
                        <method>Deliverydate</method>
                    </Company_Module_Model_Observer>
                </observers>
            </checkout_type_onepage_save_order>
        </events>
  </global>
</config>

我认为我的观察者存在一些问题。我认为它没有解雇。

* 修改

我的观察员现在正在工作。文件名必须是Observer.php,而不是observer.php。

2 个答案:

答案 0 :(得分:13)

添加观察者:

<events>
    <checkout_type_onepage_save_order>
        <observers>
            <Savefield_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>Savefield</method>
            </Savefield_observer>
        </observers>
    </checkout_type_onepage_save_order>
</events>

mymodule / savefield / model / observer.php 文件中添加以下内容:

class Mymodule_Savefield_Model_Observer
{
    public function Savefield($observer){
        //get event data
        $event = $observer->getEvent();

        //get order
        $order = $event->getOrder();

        //set the variable here
        $order->setMyField($fieldVal);
    }   
}

答案 1 :(得分:0)

您可以添加一些事件观察器来捕获报价保存和加载事件。将以下代码插入config.xml标记内的模块<global>文件中。

<events>

<sales_quote_save_before> <!-- Event Called Before Quote Object is saved -->

<observers>

<save_before>

<type>singleton</type>

<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->

<method>saveQuoteBefore</method> <!-- name of function -->

</save_before>

</observers>

</sales_quote_save_before>

<sales_quote_save_after> <!-- Event called After Quote Object is saved -->

<observers>

<save_after>

<type>singleton</type>

<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->

<method>saveQuoteAfter</method> <!-- name of function -->

</save_after>

</observers>

</sales_quote_save_after>

<sales_quote_load_after> <!-- Event called when Quote Object is loaded -->

<observers>

<load_after>

<type>singleton</type>

<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->

<method>loadQuoteAfter</method> <!-- name of function -->

</load_after>

</observers>

</sales_quote_load_after>

<sales_model_service_quote_submit_after> <!-- Event called after order placed -->

<observers>

<sales_model_service_quote_submit_after>

<type>singleton</type>

<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->

<method>saveOrderAfter</method> <!-- name of function -->

</sales_model_service_quote_submit_after>

</observers>

</sales_model_service_quote_submit_after>

<sales_order_load_after> <!-- Event called after Order Object is loaded -->

<observers>

<sales_order_load_after>

<type>singleton</type>

<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->

<method>loadOrderAfter</method> <!-- name of function -->

</sales_order_load_after>

</observers>

</sales_order_load_after>

</events>

向结帐页面添加自定义字段涉及多个步骤,需要编辑多个文件。有关完整的分步指南,请访问以下教程。

http://www.demagento.com/tutorial-magento-add-custom-field-to-order-checkout-page/