4

i want to copy a product's attribute (weight) to another atribute (general_weight).

i found this codes, but i don't know what to do with this codes? or where to copy them?

    $prod=Mage::getModel('catalog/product')->load($id);

    $val=$prod->getData('attribute_code');

assign the values to another attribute of that product with this code

    $prod->setAttreibuteCode($val);

    $prod->save();

i will thankful if you experts help me and explain by details.

** i want weight attribute value to be copied into general_weight always on product save.

1
  • why you need this program?
    – Amit Bera
    Commented Feb 4, 2015 at 8:24

3 Answers 3

0

Moh3n,i suggest that export an product from Admin in csv format. with sku,weight and then import that csv with change columns name to general_weight from weight.

pragmatically if want then follow below process:

 $prod=Mage::getModel('catalog/product')->load($id);

        $val=$prod->getData('weight');

        $prod->setGeneralWeight($val);

        $prod->save();

Update:

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 $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');

}

then run the script by browser or shell php program.

5
  • where should i copy this code?
    – Moh3n
    Commented Feb 4, 2015 at 9:30
  • can you please tell me how many product on your system >
    – Amit Bera
    Commented Feb 4, 2015 at 9:32
  • 1
    i want weight attribute to be always copied to general_weight on product save. does this code do that?
    – Moh3n
    Commented Feb 4, 2015 at 9:33
  • about 600 products
    – Moh3n
    Commented Feb 4, 2015 at 9:33
  • when i run 'update.php' with your code i face this error: 'Fatal error: Call to a member function getWeight() on a non-object in /home/dokanche/public_html/update_weight.php on line 12'
    – Moh3n
    Commented Apr 27, 2015 at 9:00
7

You can do this with a sql query.
But First you need to identify the attribute ids for your attributes and their types.

SELECT 
    attribute_id, attribute_code, backend_type
FROM 
    eav_attribute 
WHERE
    attribute_code IN ('weight', 'general_weight') AND
    entity_type_id = (SELECT 
                          entity_type_Id 
                      FROM 
                          eav_entity_type
                      WHERE
                          entity_type_code = 'catalog_product'
                     )

Let's say it turns out like this:

attribute_id|attribute_code|backend_type
101         |weight        |decimal
502         |general_weight|decimal

To determine the table for each attribute type you just append catalog_product_entity_ to the backend_type.
So in this case catalog_product_entity_decimal is for both of them (and they should be since the attributes should be the same type).

Now run this (backup your database before trying it).Replace 502 and 101 with the ids you got from the query above.:

INSERT INTO 
    `catalog_product_entity_decimal` -- the table for `general_weight` attribute
    (entity_type_id, attribute_id, store_id, entity_id, value)
SELECT entity_type_id, 502 as attribute_id, store_id, entity_id, value
FROM 
    `catalog_product_entity_decimal` -- the table for `weight` attribute
WHERE 
    attribute_id = 101 -- the id of the weight attribute
ON DUPLICATE KEY UPDATE
    value = VALUES(value)

Now rebuild your indexes.

3
  • Good and faster way
    – Amit Bera
    Commented Feb 4, 2015 at 9:02
  • @AmitBera. And risky :)
    – Marius
    Commented Feb 4, 2015 at 9:05
  • Yes.it is also risky
    – Amit Bera
    Commented Feb 4, 2015 at 9:05
0

i have a Weight attribute :

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

and i have a General Weight attribute :

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

i want to assign Weight attribute to General Weight without four decimals. i mean i want the value of Weight attribute( for example 2000.0000 to copy into General Weight value on product save (but without for last decimals like 2000).

i made an small extension for that in this way:

i created Moh3n_Attvaluecopier.xml file in app/etc/modules with this content:

<?xml version="1.0"?>
<config>
    <modules>
        <Moh3n_Attvaluecopier>
            <active>true</active>
            <codePool>local</codePool>
        </Moh3n_Attvaluecopier>
    </modules>
</config>

and config.xml file in app/code/local/Moh3n/Attvaluecopier/etc with this content:

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

and Observer.php file in app/code/local/Moh3n/Attvaluecopier/model with this content:

<?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()));
        $loadCurrentProduct->save();
        endif;}
    }
1
  • This code is so imperformant... Take a look at Amit his code to save the attribute, it's 10 times as fast, Marius his answer is even faster but is risky if you don't know what you are doing :). This is not the correct answer. Commented Apr 26, 2015 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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