【问题标题】:Magento 1.6.2.0 sales orders custom attribute is not workingMagento 1.6.2.0 销售订单自定义属性不起作用
【发布时间】:2012-03-24 18:18:01
【问题描述】:

我已经花了一整天的时间来解决这个问题,但我认为我无法让它工作,因为将自定义 EAV 属性添加到订单的可能性已被删除。 至少,我注意到sales_order_entity 不见了。

好吧,我尝试做的是向销售订单添加一个自定义字段。我认为它与类别产品的工作方式相同,但看起来不行。 我做这一切的总体目的是因为我想跟踪谁将产品添加到目录中,并希望将某个订单与某个用户(而不是客户)关联起来。

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 表中有必填项。

我不知道是我做错了什么还是这不可能? 我还考虑通过创建额外的表来跟踪用户-订单|产品之间的关系来解决这种不同的方法。这将需要更多艰苦的工作。

【问题讨论】:

    标签: magento attributes


    【解决方案1】:

    我知道这是一篇旧帖子,但我在寻找相同问题的答案时遇到了它,所以想分享我的解决方案。

    我们以向销售订单添加属性为例。

    首先,如果您查看 core/Mage/Sales/etc/config.xml,Magento 不再使用 EAV 实体进行销售

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

    resouceModel 不再指向 sales_entity(EAV),它现在指向 sales_resource(flat)。如果查看 sales_resource 节点的子节点,您会发现 order 节点:

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

    这意味着 Mage_Sales_Model_Order 有一个 Mage_Sales_Model_Resource_Order 资源模型,其中有一个 sales_flat_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属性的代码sn-p:

    $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);
    

    【讨论】:

    • 这与向 sales_flat_order 表中添加新列相同。如果您查看 _addFlatAttribute 最终结果。
    【解决方案2】:

    对于版本 > 1.4.1.0,您必须在 sales_flat_order 表中创建一列。你可以看看这个帖子 is magento sales eav

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多