我有一个Weight
属性:
Attribute Code: weight
Scope: general
Catalog Input Type for Store Owner: Text Field
Values Required: yes
Apply To: Simple Product, Bundle Product
Allow HTML Tags on Frontend: yes
并且我有一个CCD_ 2属性:
Attribute Code: general_weight
Scope: general
Catalog Input Type for Store Owner: Text Field
Values Required: no
Apply To: All Product Types
Use in Advanced Search: yes
Comparable on Front-end: yse
Allow HTML Tags on Frontend: yes
Visible on Product View Page on Front-end: yes
我想将CCD_ 3属性分配给CCD_。我的意思是,我希望Weight
属性的值(例如2000.0000)在产品保存时复制到General Weight
值中(但没有像2000这样的最后小数)。
我用这种方式对此进行了一个小的扩展:
我在app/etc/modules
中创建了Moh3n_Attvaluecopier.xml
文件,内容如下:
<?xml version="1.0"?>
<config>
<modules>
<Moh3n_Attvaluecopier>
<active>true</active>
<codePool>local</codePool>
</Moh3n_Attvaluecopier>
</modules>
</config>
和app/code/local/Moh3n/Attvaluecopier/etc
中的config.xml
文件,内容如下:
<?xml version="1.0"?>
<config>
<modules>
<Moh3n_Attvaluecopier>
<version>0.0.1</version>
</Moh3n_Attvaluecopier>
</modules>
<global>
<models>
<moh3nattvaluecopier>
<class>Attvaluecopier_Model</class>
</moh3nattvaluecopier>
</models>
<events>
<catalog_product_save_after>
<observers>
<moh3n_attvaluecopier_model_observer>
<type>singleton</type>
<class>Moh3n_Attvaluecopier_Model_Observer</class>
<method>copyAttribute</method>
</moh3n_attvaluecopier_model_observer>
</observers>
</catalog_product_save_after>
</events>
</global>
</config>
和CCD_ 12中的CCD_
<?php
class Moh3n_Attvaluecopier_Model_Observer
{
private $_processFlag; //to prevent infinite loop of event-catch situation
public function copyAttribute($observer){
if(!$this->_processFlag):
$this->_processFlag=true;
$_store = $observer->getStoreId();
$_product = $observer->getProduct();
$_productid = $_product->getId();
$attrA = $_product->getAttributeText('weight'); //get attribute A's value
$action = Mage::getModel('catalog/resource_product_action');
$action->updateAttributes(array($_productid), array('general_weight'=>$attrWeight),$_store); //assign attrA's value to attrB
$_product->save();
endif;}
}
我用一些数字填充weight
属性,比如2000.0000
,但保存产品后General Weight
中不会有任何变化。我的意思是值2000
不会复制到General Weight
属性,并且General Weight
不会显示任何内容。
这是您更新的观察器函数:
<?php
public function copyAttribute($observer){
if(!$this->_processFlag):
$this->_processFlag=true;
$_product = $observer->getProduct();
$_productid = $_product->getId();
$productModel = Mage::getModel('catalog/product');
$loadCurrentProduct = $productModel->load($_productid);
$loadCurrentProduct->setGeneralWeight(intval($loadCurrentProduct->getWeight()));
$loadCurrentProduct->save();
endif;
}