【问题标题】:assign a product attribute value to another product attribute in magento将产品属性值分配给magento中的另一个产品属性
【发布时间】:2015-02-05 16:04:20
【问题描述】:

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

我为此做了一个小扩展:

我在app/etc/modules 中创建了Moh3n_Attvaluecopier.xml 文件,内容如下:

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

我用2000.0000 之类的数字填充weight 属性,但保存产品后General Weight 不会发生任何变化。我的意思是2000 的值不会复制到General Weight 属性,General Weight 不会显示任何东西。

【问题讨论】:

    标签: javascript php html xml magento


    【解决方案1】:

    这是你更新后的观察者函数:

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

    【讨论】:

    • 解析错误:语法错误,app/code/local/Moh3n/Attvaluecopier/Model/Observer.php 第 3 行中出现意外的“公共”(T_PUBLIC)。你的代码出现了这个错误。
    • 我刚刚修改了函数,你需要在你之前的函数中替换它。我认为您已经跳过了在函数之前声明的类和私有变量
    • @MihirBhende 有点相同的问题,但对于 magento 2。请看一下并提出建议,我真的陷入了困境。 magento.stackexchange.com/questions/339316/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    相关资源
    最近更新 更多