0

i copy weight attribute value to another product attribute named general_weight by this php code:

<?php
require_once  "app/Mage.php";
Mage::app("admin");
umask();

/*
 Step2: get Product Collection filter by Saleable
*/
$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('weight');

foreach($collection as $product){
$val=$product->getWeight();
/*$product->getWeight() is not given the value then used 

$product->getResource()->getAttribute('weight')->getFrontend()->getValue($product-)

*/
$product->setGeneralWeight($val);
$product->getResource()->saveAttribute($product, 'general_weight');

}

my weight attribute has for decimals like: 90.0000 . i want to remove those decimals from general_weight attribute and add a suffix to it like (KG). at the end i want 90.0000 from weight attribute to be copied to general_weight attribute this way: 90 KG

1

In case your weight attribute is type of string:

$val = preg_replace('/\..*$/', 'KG', $product->getWeight());

or in case of float:

$val = floor($product->getWeight()) . 'KG';
  • 1
    May i know, which location i need to edit.... – zus Nov 22 '17 at 5:14
  • The location you output the weight. – user487772 Nov 22 '17 at 9:36
1

Assuming that all the values are float ending in '.0000' then try

$general_weight = $product->getWeight() * 1 . 'kg';
  • Sorry for being picky but this can produce something like 1.5kg in case weight attribute is stored as float while initial requirement was to drop decimal part. – user487772 Apr 27 '15 at 18:41
  • could you pls guide me by default, where i can change in my Magento file. – zus Nov 22 '17 at 12:12

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.