2012-10-23 160 views
1

我想向sales/order添加一個屬性,即Mage_Sales_Model_Order,以存儲一些額外的數據。Magento如何將新屬性添加到銷售/訂單模型?

我發現銷售/訂單的資源使用正常表。當我保存訂單時,它只保存與表格中的列匹配的字段。

什麼是正確的方式來添加此屬性?

+2

http://stackoverflow.com/questions/4378394/magento-adding-a-new-列到銷售單位報價項目和銷售平訂單項目可能有助於引導您朝正確的方向發展。 – B00MER

回答

3

我會推薦下面幾個答案的混合。你肯定希望通過config xml將這段代碼放入安裝腳本中,但是如果你想利用addAttribute函數,你想要使用Mage_Eav_Model_Entity_Setup作爲安裝類。所以,你的配置XML將是這個樣子......

<config> 
... 
    <resources> 
     <modulename_setup> 
      <setup> 
       <module>Your_Module</module> 
       <class>Mage_Eav_Model_Entity_Setup</class> 
      </setup>    
     </modulename_setup>   
    </resources> 
... 
</config> 

和你的安裝腳本應該是這個樣子

$installer = $this; 
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */ 

$installer->startSetup(); 

$installer->addAttribute('sales_order', 'attributename', array(
      'group'    => 'General', 
      'label'    => 'Label frontend', 
      'note'    => '', 
      'type'    => 'string', //backend_type 
      'input'    => 'text', //frontend_input 
      'frontend_class' => '', 
      'source'   => '', 
      'backend'   => '', 
      'frontend'   => '', 
      'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE, 
      'required'   => false, 
      'visible_on_front' => false, 
      'apply_to'   => 'simple', 
      'is_configurable' => false, 
      'used_in_product_listing' => false, 
      'sort_order'  => 5, 
     )); 

$installer->endSetup(); 

請記住,這是未經測試的代碼,你可能需要調整一些的屬性選項讓它以你需要的方式運行。

0

要在訂單中添加新屬性,請使用此代碼,但僅限一次。獲取任何文件並將代碼放在那裏,執行一次並刪除代碼。

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 

     $setup->addAttribute('sales_order', 'attributename', array(
      'group'    => 'General', 
      'label'    => 'Label frontend', 
      'note'    => '', 
      'type'    => 'string', //backend_type 
      'input'    => 'text', //frontend_input 
      'frontend_class' => '', 
      'source'   => '', 
      'backend'   => '', 
      'frontend'   => '', 
      'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE, 
      'required'   => false, 
      'visible_on_front' => false, 
      'apply_to'   => 'simple', 
      'is_configurable' => false, 
      'used_in_product_listing' => false, 
      'sort_order'  => 5, 
     )); 
+0

我希望我不必確切地知道這段代碼必須在升級腳本中(因此,沒有Setup類的明確的即時通訊)... –

+0

@Bixi我知道這不是最好的方式或權利方法來做到這一點。但工作,沒有問題,我已經嘗試過。但你是對的,這個腳本在升級腳本上是最好的,但可以在任何地方工作。 – Guerra

+1

是的,但它是一次性腳本... Magento提供了一些方法來完成這些自動數據庫數據庫安裝/升級(請參閱Magenta小姐的回覆)。你應該修改你的數據庫只在升級腳本..持久性和版本控制 –

0

按照以下步驟添加要訂購的自定義屬性。

編輯文件persistent/checkout/onepage/billing.phtml並添加以下代碼只是這條線之上<?php if ($this->canShip()): ?>

<li class="fields"> 
    <div class="field"> 
     <label for="billing:ssn" class="required"><em>*</em><?php echo $this->__('SSN') ?></label> 
     <div class="input-box"> 
      <input type="text" name="custom[ssn]" value="<?php echo $this->htmlEscape($this->getQuote()->getSsn()) ?>" title="<?php echo $this->__('SSN') ?>" class="input-text required-entry" id="billing:ssn" /> 
     </div> 
    </div> 
</li> 

這將文本字段添加到billing.phtml

接下來你需要創建兩個表。銷售的'sales_quote_custom'和訂單的'sales_order_custom'。打開phpmyadmin並確認是否創建了報價單和訂單表。打開你的模塊​​3210文件,並把在

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

相關問題