【问题标题】:Magento 1.9 custom order attribute not saved (EAV_entity_text)Magento 1.9 自定义订单属性未保存(EAV_entity_text)
【发布时间】:2015-02-04 10:24:23
【问题描述】:

在我的客户 Magento 模块中,我向订单添加了一个自定义属性(听说),该属性在订单对象中设置(设置返回值后获取)但它似乎没有保存在数据库中.

我的模块/etc/config.xml

 <resources>
   <hearedfrom_setup>
     <setup>
        <module>Brainworx_Hearedfrom</module>
	    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
     </setup>
     <connection>
        <use>core_setup</use>
     </connection>
  </hearedfrom_setup>
</resources>

MySql安装

$installer = $this;

$installer->startSetup();

//get the entity type for orders
$sql = "SELECT entity_type_id FROM ".$this->getTable('eav_entity_type')." WHERE entity_type_code='order'";
Mage::Log("Hearedfrom: " .$sql);
$row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql);
   
$attribute  = array(
	'type'			=> 'text',
	'label'			=> 'Hearedfrom',
	'visible'		=> true,
	'required'		=> false,
	'user_defined'	=> true,
	'searchable'	=> true,
	'filterable'	=> true,
	'comparable'	=> false,
);
//add attribute for the sale/order
$installer->addAttribute($row['entity_type_id'], 'hearedfrom', $attribute);

$installer->endSetup();

我在新属性中保存值的观察者。

public function hookToOrderSaveEvent()
	{
		/**
		* NOTE:
		* Order has already been saved, now we simply add some stuff to it,
		* that will be saved to database. We add the stuff to Order object property
		* called "hearedfrom"
		*/
		$order = new Mage_Sales_Model_Order();
		$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
		$order->loadByIncrementId($incrementId);
		
		//Fetch the data from select box and throw it here
		$_hearedfrom_data = null;
		$_hearedfrom_data = Mage::getSingleton('core/session')->getBrainworxHearedfrom();
		//$this->_getRequest()->getPost(‘myCustomerOrderComment’, false);
		Mage::Log("Order brought by: ".$_hearedfrom_data);
		
		//Save fhc id to order obcject
		$order->setData(self::ORDER_ATTRIBUTE_FHC_ID, $_hearedfrom_data);
		Mage::Log("test: ".$order->getHearedfrom());
		$order->save();
	}

在我的日志中,我可以看到在我的 $order 中设置它之前和之后的值。 数据具有该值的条目。 在数据库中,我可以看到 EAV_ATTRIBUTE 中的一个实体链接到 EAV_ENTITY_TYPE 和 EAV_ENTITY_ATTRIBUTE。 但我希望保存的值在不包含任何记录的 EAV_ENTITY_TEXT 中。

重新加载下订单后,它不包含客户属性。

我在这里错过了什么?

谢谢

【问题讨论】:

    标签: magento entity-attribute-value


    【解决方案1】:

    您能在 sales_flat_order 表中看到您的属性吗?

    如果不是,我认为您的问题是使用目录设置而不是销售设置。尝试改用 Mage_Sales_Model_Resource_Setup 安装类。改为添加这样的属性:

    $installer->getConnection()->addColumn($installer->getTable('sales/order'), 'hearedfrom', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'nullable' => true,
    'default' => 0
    ));
    

    还清理 magento 缓存。我相信一些 SQL 查询即使全部被禁用也会被缓存。当订单保存到数据库时,DESCRIBE SQL 查询将确定哪些属性保存在数据库中。 我最近遇到了一个问题,describe 查询没有包含我的新列,因此没有保存该值。

    【讨论】:

      【解决方案2】:

      通过将我的处理程序挂钩到 order_save_after_event 而不是 order_save_event 解决了这个问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多