Magento:在sales/order_shipment模型上设置自定义属性 [英] Magento: Setting a custom attribute on the sales/order_shipment model

查看:111
本文介绍了Magento:在sales/order_shipment模型上设置自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个名为"vendorping"的EAV属性添加到sales/order_shipment模型中.为此,我在模块中创建了以下文件:

I'm trying to add an EAV attribute called "vendorping" to the sales/order_shipment model. To accomplish this, I created the following file in my module:

// app\code\local\Jb\Vendorping\sql\vendorping_setup\mysql4-install-0.1.0.php

$this->startSetup();

$sql = 'SELECT entity_type_id FROM `'.$this->getTable('eav_entity_type').'` WHERE entity_type_code = \'shipment\'';
$row = Mage::getSingleton('core/resource')
    ->getConnection('core_read')
    ->fetchRow($sql);
$entityTypeId = $row['entity_type_id'];

$c = array(
    'entity_type_id'  => $entityTypeId,
    'attribute_code'  => 'vendorping',
    'backend_type'    => 'int',
    'frontend_input'  => 'text',
    'is_global'       => '1',
    'is_visible'      => '0',
    'is_required'     => '0',
    'is_user_defined' => '0',
    'frontend_label'  => 'Vendor Confirmed',
    );
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id'], $c['attribute_code'])
    ->setStoreId(0)
    ->addData($c)
    ->save();

$this->endSetup();

现在,这工作正常-已成功添加此属性:

Now, this is working fine -- this attribute is successfully added:

mysql> mysql> SELECT * FROM eav_attribute WHERE attribute_code LIKE 'vendorping';
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
| attribute_id | entity_type_id | attribute_code | attribute_model | backend_model | backend_type | backend_table | frontend_model | frontend_input | frontend_label   | frontend_class | source_model | is_required | is_user_defined | default_value | is_unique | note |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
|          127 |              8 | vendorping     | NULL            | NULL          | int          | NULL          | NULL           | text           | Vendor Confirmed | NULL           | NULL         |           0 |               0 | NULL          |         0 |      |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
1 row in set (0.00 sec)

但是,如果我运行此控制器操作,似乎无法成功保存新属性:

But if I run this controller action, I can't seem to successfully save the new attribute:

// app\code\local\Jb\Vendorping\controllers\IndexController.php ===

class Jb_Vendorping_IndexController extends Mage_Core_Controller_Front_Action 
{
    public function pingAction()
    {
        // Get shipment
        $shipmentId = 1; // Set manually for now
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
        var_dump($shipment->getOrder()->getShippingDescription());
            // Outputs:
            // string(17) "Flat Rate - Fixed" [So the shipment exists]

        // Save "vendorping" field and save
        $shipment->setVendorping(1);
        $shipment->save();

        // Reload shipment from database
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

        // Check "vendorping" field
        var_dump($shipment->getVendorping());
            // Outputs:
            // NULL [Why??]
    }
}

推荐答案

此方法有效:

// ModuleNamespace/ModuleName/sql/vendorping_setup/mysql4-install-0.1.0.php

$this->startSetup();

if (version_compare(Mage::getVersion(), '1.4.1.0', '>=')) { // If sales data is stored flat
    $w = $this->_conn;
    $w->addColumn($this->getTable('sales_flat_shipment'), 'vendorping', 'int');
} else {
    $eav = new Mage_Eav_Model_Entity_Setup('sales_setup');
    $eav->addAttribute('shipment', 'vendorping', array('type' => 'int'));
}

$this->endSetup();

这篇关于Magento:在sales/order_shipment模型上设置自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆