Magento属性值自定义字段:admin和frontend read

时间:2017-01-10 02:12:13

标签: magento magento-1.9

我已成功将产品属性值选项自定义字段中的数据写入自定义表。这就是我的表现:

应用/代码/本地/ MyModule的/控制的/ etc / config.xml中

<events>
    <catalog_entity_attribute_save_before>
        <observers>
            <control_catalog_product_attribute_save_before>
                <type>singleton</type>
                <class>control/catalog_product_observer</class>
                <method>saveAttributeValueDescription</method>
            </control_catalog_product_attribute_save_before>
        </observers>
    </catalog_entity_attribute_save_before>
<events>

<entities>
    <productattributesvalues>
        <table>mymodule_product_attributes_values_description</table>
    </productattributesvalues>
</entities>

应用/代码/本地/ MyModule的/控制/型号/目录/产品/ Observer.php

public function saveAttributeValueDescription($observer)
{
    $attribute = $observer->getEvent()->getAttribute();
    $attribute_id = $attribute->getAttributeId();
    $option = $attribute->getOption();
    $description = $option["description"];
    $valueDescriptionModel = Mage::getModel('control/productattributesvalues');
    $deleteCollection = $valueDescriptionModel->getCollection()->addFieldToFilter('attribute_id', array('eq' => $attribute_id));
    foreach($deleteCollection as $coll)
        $valueDescriptionModel->load($coll->getDescriptionId())->delete();
    $valueDescriptionModel->unsetData();
    foreach ($description as $option_id => $option_description) {
        if ("" !== $option_description) {
            $valueDescriptionModel
                ->setAttributeId($attribute_id)
                ->setOptionId($option_id)
                ->setDescription($option_description)
                ->save()
                ->unsetData();
        }
    }
    return $this;
}

应用/代码/本地/ MyModule的/控制/型号/ Mysql4 / Productattributesvalues.php

class MyModule_Control_Model_Mysql4_Productattributesvalues extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('control/productattributesvalues', 'description_id');
    }
}

应用/代码/本地/ MyModule的/控制/型号/ Mysql4 / Productattributesvalues / Collection.php

class MyModule_Control_Model_Mysql4_Productattributesvalues_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('control/productattributesvalues');
    }
}

自定义表格

CREATE TABLE IF NOT EXISTS `mymodule_product_attributes_values_description` (
    `description_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `attribute_id` int(11) unsigned NOT NULL,
    `option_id` int(11) unsigned NOT NULL,
    `description` text NOT NULL,
    PRIMARY KEY (`description_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

目录/产品/属性/ options.phtml

// line 88 added:
<th><?php echo Mage::helper('catalog')->__('Description') ?></th>
// line 101:
<td class="a-left"><input class="input-text" type="text" name="option[description][{{id}}]" value="{{description}}" /></td>
// line 126:
'<td><input class="input-text" type="text" name="option[description][{{id}}]" value="{{description}}" /><\/td>'+

现在我不知道如何在 catalog / product / attribute / options.phtml 和前端产品视图页面中阅读该数据......

我也会做观察路线吗?如果是这样,怎么办?有人能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

知道了! :)

应用/代码/本地/ MyModule的/控制/型号/ Productattributesvalues.php

public function getAttributeValueDescription($attributeId, $optionId)
{
  $descriptionCollection = $this->getCollection()
    ->addFieldToFilter('attribute_id', array('eq' => $attributeId))
    ->addFieldToFilter('option_id',    array('eq' => $optionId));

  $descriptionArray = $descriptionCollection->getData();

  if (isset($descriptionArray[0]["description"]))
    return $descriptionArray[0]["description"];
  else
    return;
}

<强>目录/产品/属性/ options.phtml

// line 90 added:
<?php $optionValues = $this->getOptionValues() ?>

<?php foreach ($optionValues as &$val): ?>
  <?php $val->_data["description"] = Mage::getModel('control/productattributesvalues')->getAttributeValueDescription($this->getRequest()->getParam('attribute_id'), $val->id) ?>
<?php endforeach ?>
// replaced all $this->getOptionValues() for $optionValues