2012-03-24 72 views
0

我已經花了整整一天的時間,我認爲我無法得到這個工作,因爲刪除了向訂單添加自定義EAV屬性的可能性。 至少,我注意到sales_order_entity丟失了。Magento 1.6.2.0銷售訂單自定義屬性不工作

那麼,我試圖做的是添加一個自定義字段的銷售訂單。我認爲它的工作方式與類別產品相同,但看起來不像。 我在做這一切時的總體觀點是因爲我想跟蹤誰在向產品目錄添加產品,並希望將特定訂單與特定用戶(而非客戶)聯繫起來。

public function getDefaultEntities() 
{ 
    return array(
     'catalog_product' => array(
      'entity_model'  => 'catalog/product', 
      'attribute_model' => 'catalog/resource_eav_attribute', 
      'table'    => 'catalog/product', 
      'additional_attribute_table' => 'catalog/eav_attribute', 
      'entity_attribute_collection' => 'catalog/product_attribute_collection', 
      'attributes'  => array(
       'seller_id' => array(
        'group'    => 'MyCustom', 
        'label'    => 'Seller ID', 
        'type'    => 'int', 
        'input'    => 'text', 
        'default'   => '0', 
        'class'    => '', 
        'backend'   => '', 
        'frontend'   => '', 
        'source'   => '', 
        'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
        'visible'   => false, 
        'required'   => true, 
        'user_defined'  => true, 
        'searchable'  => true, 
        'filterable'  => true, 
        'comparable'  => false, 
        'visible_on_front' => false, 
        'visible_in_advanced_search' => false, 
        'unique'   => false, 
       ), 
      ), 
     ), 
     'order' => array(
      'entity_model'  => 'sales/order', 
      'table'    => 'sales/order', 
      'increment_model' => 'eav/entity_increment_numeric', 
      'attributes'  => array(
       'seller_id' => array(
        'group'    => 'MyCustom', 
        'label'    => 'Seller ID', 
        'type'    => 'int', 
        'input'    => 'text', 
        'default'   => '0', 
        'class'    => '', 
        'backend'   => '', 
        'frontend'   => '', 
        'source'   => '', 
        'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
        'visible'   => false, 
        'required'   => true, 
        'user_defined'  => true, 
        'searchable'  => true, 
        'filterable'  => true, 
        'comparable'  => false, 
        'visible_on_front' => false, 
        'visible_in_advanced_search' => false, 
        'unique'   => false, 
       ), 
      ), 
     ), 
    ); 
} 

它適用於產品,但不適用於訂單。我已經要求在eav_attribute表中的條目。 我不知道我是否做錯了什麼,或者這是不可能做到的? 我也想過通過創建額外的表來追蹤用戶訂單|產品之間的關係來解決這種不同的方式。這需要更多的工作艱難。

任何想法? 謝謝。

回答

3

我知道這是一箇舊帖子,但當我在尋找相同問題的答案時遇到了它,所以認爲我會分享我的解決方案。

讓我們以銷售訂單的一個屬性爲例。

首先,Magento的不再是用戶EAV實體銷售,如果你看一下核心/法師/銷售/等/ config.xml中

<sales> 
     <class>Mage_Sales_Model</class> 
     <resourceModel>sales_resource</resourceModel> 
</sales> 

resouceModel在sales_entity(EAV),現在點不再分在sales_resource(單位)。如果你看一下sales_resource節點的孩子,你會發現順序節點:

<order> 
     <table>sales_flat_order</table> 
</order> 

這意味着Mage_Sales_Model_Order具有己sales_flat_order的表Mage_Sales_Model_Resource_Order的資源模型。

Magento的開發人員提供的類Mage_Sales_Model_Resource_Setup,它允許你在幾乎你會添加的屬性到EAV結構以同樣的方式屬性添加到這個新的「扁平化」結構。如果你看看裏面Mage_Sales_Model_Resource_Setup你會看到下面的功能:

/** 
* Add entity attribute. Overwrited for flat entities support 
* 
* @param int|string $entityTypeId 
* @param string $code 
* @param array $attr 
* @return Mage_Sales_Model_Resource_Setup 
*/ 
public function addAttribute($entityTypeId, $code, array $attr) 
{ 

    if (isset($this->_flatEntityTables[$entityTypeId]) && 
     $this->_flatTableExist($this->_flatEntityTables[$entityTypeId])) 
    { 
     $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr); 
     $this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId); 
    } else { 
     parent::addAttribute($entityTypeId, $code, $attr); 
    } 
    return $this; 
} 

有了這些信息,你現在應該看到,這是僅僅讓Mage_Sales_Model_Resource_Setup的一個實例,並調用其與有效參數公衆的addAttribute方法的情況下。加入franchise_id屬性到銷售訂單的

代碼片段:

$salesResourceSetupModel = Mage::getModel('sales/resource_setup', 'core_setup'); 

$data=array(
    'type'=>'int', 
    'input'=>'text', 
    'label'=>'Franchise', 
    'global'=> 1, 
    'is_required'=>'0', 
    'is_comparable'=>'0', 
    'is_searchable'=>'0', 
    'is_unique'=>'0', 
    'is_configurable'=>'0', 
    'user_defined'=>'1', 
    //whether it should be including in the sales order grid 
    'grid'=>1 
); 

//first param should relate to a key of the protected $_flatEntityTables array 
$salesResourceSetupModel->addAttribute('order', 'franchise_id', $data); 
+0

謝謝老兄,真的! – Ivo 2014-09-05 14:47:53

0

對於版本> 1.4.1.0,你必須創建在sales_flat_order表中的列。你可以看看這篇文章 is magento sales eav

相關問題