I have to fill programmatically attribute from two others. For example have attribute price_shippment
and have to fill it from shippment
+price
.
Any sugestions?
2 Answers
You can "autofill" this attribute when product is saved.
Add a extension with this code:
app/etc/modules/My_Module.xml
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<active>true</active>
<codePool>local</codePool>
</My_Module>
</modules>
</config>
app/code/local/My/Module/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<models>
<my_module>
<class>My_Module_Model</class>
</my_module>
</models>
<events>
<catalog_product_save_before>
<observers>
<fill_attribute>
<class>my_module/observer</class>
<method>fillAttribute</method>
</fill_attribute>
</observers>
</catalog_product_save_before>
</events>
</global>
</config>
app/code/local/My/Module/Model/Observer.php
class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
public function fillAttribute($observer)
{
$product = $observer->getProduct();
$price = $product->getPrice();
$product->setData('cena_z_przesylka_en', $price + $product->getKosztPrzesylkiEn());
$product->setData('cena_z_przesylka_de', $price + $product->getKosztPrzesylkiDe());
$product->setData('cena_z_przesylka_fr', $price + $product->getKosztPrzesylkiFr());
$product->setData('cena_z_przesylka_it', $price + $product->getKosztPrzesylkiIt());
$product->setData('cena_z_przesylka_es', $price + $product->getKosztPrzesylkiEs());
}
}
Edit: Massupdate
Run this code from Magentos root directory:
<?php
require_once('./app/Mage.php');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$action = new MyProductAction;
$action->setPrices();
class MyProductAction
{
public function setPrices()
{
$attributes = array(
'price',
'koszt_przesylki_en',
'koszt_przesylki_de',
'koszt_przesylki_fr',
'koszt_przesylki_it',
'koszt_przesylki_es',
);
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect($attributes, 'inner');
Mage::getSingleton('core/resource_iterator')->walk(
$products->getSelect(),
array(array($this, 'setPricesCallback'))
);
}
public function setPricesCallback($args)
{
var_dump($args);
Mage::getSingleton('catalog/product_action')->updateAttributes(
array($args['row']['entity_id']),
array(
'cena_z_przesylka_en' => $args['row']['price'] + $args['row']['koszt_przesylki_en'],
'cena_z_przesylka_de' => $args['row']['price'] + $args['row']['koszt_przesylki_de'],
'cena_z_przesylka_fr' => $args['row']['price'] + $args['row']['koszt_przesylki_fr'],
'cena_z_przesylka_it' => $args['row']['price'] + $args['row']['koszt_przesylki_it'],
'cena_z_przesylka_es' => $args['row']['price'] + $args['row']['koszt_przesylki_es']
),
null
);
}
}
Note: if it doesn't work, do the following first:
- open product grid
- select all
- update attributes
- submit
- check "change" checkboxes for "price_shippment" attributes but leave input field empty
- save
-
-
Have you also the other files that are requiered for an extension? This code is successfully tested.– sv3nCommented Jun 7, 2017 at 9:10
-
Added code for
My_Module.xml
... add these files and flush cache. Should work ;)– sv3nCommented Jun 7, 2017 at 9:15 -
still empty. Bellow listen my attributes ti fill it: cena_z_przesylka_en = koszt_przesylki_en + price cena_z_przesylka_de = koszt_przesylki_de + price cena_z_przesylka_fr = koszt_przesylki_fr + price cena_z_przesylka_it = koszt_przesylki_it + price cena_z_przesylka_es = koszt_przesylki_es + price– ZydolCommented Jun 7, 2017 at 9:23
-
Updated and tested ;) Please add
Mage::log('test', null, 'observer.log', true)
tofillAttribute
and check ifobserver.log
is generated invar/log
after product save.– sv3nCommented Jun 7, 2017 at 9:33
You can create a php
file with below code in you magento setup root folder and execute in browser.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect("*");
foreach ($products as $product) {
$product->setPriceShippment($product->getPrice() + $product->getShippment());
$product->save();
}
-
-
I think you are right, if there is no
default
store it will generate error– JohnCommented Jun 7, 2017 at 9:08