如何将产品的属性值复制到Magento中的另一个属性? [英] How to copy a product's attributes value to another attribute in Magento?

查看:93
本文介绍了如何将产品的属性值复制到Magento中的另一个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Magento中是否可以通过编程将属性X的值分配给属性Y?

Is there a way in Magento where I can assign Attribute X's value to Attribute Y programmatically ?

到目前为止,我已经尝试过了.我使用以下设置创建了Y属性:

So far I have tried this. I created the Y attribute with following settings:

$setup->addAttribute('catalog_product','myAttribute',array(
                   'group'=>'General',
                   'input'=>'label',
                   'type'=>'varchar',
                   'label'=>'Value of Attribute X',
                   'visible'=>1,
                   'backend'=>'beta/entity_attribute_backend_myattribute',
                   'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE
               ));

在我的后端模型中,我做到了:

In my backend model, I have done this:

class Namespace_Module_Model_Entity_Attribute_Backend_Myattribute extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    public function beforeSave($object){
        $attrCode = $this->getAttribute()->getAttributeCode();
        $object->setData($attrCode,'HAHAHA');
        parent::beforeSave($object);
        return $this;
    }
}

我可以在产品编辑"页面中看到"HAHAHA"作为属性值.我想将其更改为另一个属性的值.我该怎么做 ?如何从此类访问同一产品的另一个属性的值?

I am able to see "HAHAHA" as the attribute value in the Product Edit page. I want to change this to value of another attribute. How do I do it ? How do I access another attribute's value of the same product from this class ?

PS:实际上是我要实现的目标.属性X是具有100个选项的multiselect类型.因此,属性Y必须跟踪选择的X选项,并且Y在产品页面中以只读格式显示值.

PS: What I am actually trying to achieve is this. The attribute X is of type multiselect with 100s of options. So the attribute Y must keep track of the options selected of X, and Y shows the value in product page in readonly format.

推荐答案

我终于解决了这个问题.我采用了不同的方法,我使用了Observer. 这就是我所做的:

I have solved this finally. I went by a different approach, I used Observer. This is what I did:

我用以下代码创建了一个观察者:

I created an Observer with following code:

class Namespace_Module_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('attributeA'); //get attribute A's value

    $action = Mage::getModel('catalog/resource_product_action');
    $action->updateAttributes(array($_productid), array('attributeB'=>$attrA),$_store); //assign attrA's value to attrB
    $_product->save();
    endif;
}

我的config.xml像这样:

And my config.xml went like this:

<events>
            <catalog_product_save_after>
                <observers>
                    <namespace_module>
                        <type>singleton</type>
                        <class>Namespace_Module_Model_Observer</class>
                        <method>copyAttribute</method>
                    </namespace_module>
                </observers>
            </catalog_product_save_after>
        </events>

因此,基本上,我使用的是事件catalog_product_save_after,每次保存产品时都会触发该事件.在我的观察者中,我捕获了事件,获取attributeA的值并分配给attributeB,最后保存了我的产品.

So basically, I am using the event catalog_product_save_after which is fired whenever a product is saved. In my observer, I catch the event, get attributeA's value and assign to attributeB, and finally save my product.

就是这样!我不知道这是否是最好的方法,但是它确实有效!

That's it! I don't know if this is the best method, but it does work!

这篇关于如何将产品的属性值复制到Magento中的另一个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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