将产品属性值分配给magento中的另一个产品属性

时间:2015-02-05 16:04:21

标签: javascript php html xml magento

我有一个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

我有一个General Weight属性:

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

我想将Weight属性分配给General Weight,而不是四位小数。我的意思是我希望Weight属性的值(例如2000.0000复制到产品保存的General Weight值(但没有像2000这样的最后小数)。

我以这种方式做了一个小扩展:

我使用以下内容在Moh3n_Attvaluecopier.xml中创建了app/etc/modules个文件:

<?xml version="1.0"?>
<config>
    <modules>
        <Moh3n_Attvaluecopier>
            <active>true</active>
            <codePool>local</codePool>
        </Moh3n_Attvaluecopier>
    </modules>
</config>
含有此内容的config.xml中的

app/code/local/Moh3n/Attvaluecopier/etc文件:

<?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>
含有此内容的Observer.php中的

app/code/local/Moh3n/Attvaluecopier/model文件:

<?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也不会显示任何内容。

1 个答案:

答案 0 :(得分:0)

以下是您更新的观察员功能:

<?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;
}