1

how to add a prefix to a product attribute value?. i want to add a prefix to a product attribute. for example: i have an attribute named general_weight with a number value: 1000 . i want to add a prefix like: Kg to its value. i want it to be shown this way:

General Weight: 1000 Kg .

how to do that? i will thankful if you experts help me with this.

  • If it's a custom attribute why not use a text field that accepts the value 1000 kg – Sander Mangel Feb 20 '15 at 15:00
  • its a copy of weight att – Moh3n Feb 20 '15 at 15:15
  • I guss that you want this type of works.. if you will put the weight value of product is 100 then in general_weight attribute you want it value to 100KG – Amit Bera Feb 20 '15 at 15:30
  • @AmitBera yes exactly. i did copy the attribute value in this way link but the problem is prefix. – Moh3n Feb 20 '15 at 15:36
1

In this case you can use magento event/observer.There are lot of events are exiting in by whose you can set general_weight basic of weight.

As you have using this code.There some issue in your observer.

Don't need to load the product object Again

  <?php
class Moh3n_Attvaluecopier_Model_Observer
{
    public function copyAttribute($observer)
    {
        $finalTemWeight=$observer->getEvent()->getProduct()->getData('weight');
        $observer->getEvent()->getProduct()->setData('general_weight',$finalTemWeight.'KG');
        return $this;
    }
}
  • it gets this error: Parse error: syntax error, unexpected '$_product' (T_VARIABLE) in app/code/local/Moh3n/Attvaluecopier/Model/Observer.php on line 8 – Moh3n Feb 20 '15 at 15:47
  • please ckd. the code it............. – Amit Bera Feb 20 '15 at 15:51
  • again error :Parse error: syntax error, unexpected '$_product' (T_VARIABLE) in app/code/local/Moh3n/Attvaluecopier/Model/Observer.php on line 7 – Moh3n Feb 20 '15 at 15:54
  • i cannot understand d why it not save KG value – Amit Bera Feb 20 '15 at 16:08
  • oops! i made a mistake. it doesn't save whole value! – Moh3n Feb 20 '15 at 16:12
0

observer.php should be like this:

<?php

class Moh3n_Attvaluecopier_Model_Observer
{

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()));
        $weight = intval($loadCurrentProduct->getWeight());
        $gw = (string)$weight. " KG";
        $loadCurrentProduct->setGeneralWeight($gw);
        $loadCurrentProduct->save();
        endif;}
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.