Copy a product's attributes value to another attribute?
-
12-12-2019 - |
Question
I want to copy a product's attribute (weight) to another atribute (general_weight).
I found this:
Create an script at magento root update.php
and put the below 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 $eachproduct){
$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');
}
then run the script by browser or shell php program.
but this faces to an error:
Fatal error: Call to a member function getWeight() on a non-object in update.php on line 12
Solution
Replace
foreach($collection as $eachproduct){
with:
foreach($collection as $product){
OTHER TIPS
Your foreach
loop looks like this:
foreach($collection as $eachproduct){
but then you get the weight like this:
$val=$product->getWeight();
the variable names don't match.
There issue with product object name.you should change $eachproduct
to $product
t as product object variably name is not match that why this create error
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange