This has been asked many times before but with no working answer.
I have multiple stores and some attributes have been overridden. I want to change these attributes to 'use default value' with a script.
Here is an image showing store views and 'use default value' checkboxes
http://dl.dropbox.com/u/3209649/storeviews-and-defaultvalues.png (not allowed to post images yet)
In app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php setData() is used with false for the second argument when 'Use Default Value' has been selected for any attributes.
/**
* Check "Use Default Value" checkboxes values
*/
if ($useDefaults = $this->getRequest()->getPost('use_default')) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
The following code attempts to set the 'name' attribute to 'use default values' for product 1 in store 3 using the same method.
require_once '../app/Mage.php';
Mage::app(3);
$product = Mage::getModel('catalog/product')->load(1);
$product->setData('name', false); # as used in ProductController.php
$product->save();
Using
$product->setData('name', 'anything');
correctly sets the 'name' attribute to 'anything' but false does not set it to 'use default value'
'Use Default Value' is not stored anywhere in the database so within the controller for the admin interface there must be another procedure that deletes the attribute row?
Related Links here -> http://pastebin.com/raw.php?i=j7fwu9H6
(not allowed to post links yet either)
This doesn't work because you need the current store being the admin store for this kind of operation.
To make a specific store view use the default value for a given attribute:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')
->load($product_id) // in your case: 1
->setStoreId($store_id) // in your case: 3
->setData($attr, false) // in your case: 'name'
->save();
Related
I have a list of custom options in magento config and I need to change them programmatically when an user clicks on "Save Config". In generally it's not a big problem to get it done using observer but I need to have one option like "Pack of options" and if user choose one of package appropriate options must be changed programatically. It's not a big deal too:
Mage::getConfig()->saveConfig('path/to/config/', 1, 'default', 0);
Mage::getConfig()->saveConfig('path/to/config2/', 1, 'default', 0);
But the problem is that I can't get it working for different stores, it's working for default scope only. Example:
Mage::getConfig()->saveConfig('path/to/config2/', 1, 'german', 0);
isn't working.
How can I update options programatically for certain stores only? So the user could check witch store he wants to apply options too?
Thanks for any help.
Try this:
$storeCode = 'german';
$store = Mage::getModel('core/store')->load($storeCode);
$path = 'path/to/config';
$config = Mage::getModel('core/config_data');
$config->setValue('Your value here');
$config->setPath($path);
$config->setScope('stores');
$config->setScopeId($store->getId());
$config->save();
Look at the definition of the function :
public function saveConfig($path, $value, $scope = 'default', $scopeId = 0)
The scope can actually take only 3 values : 'default', 'stores' or 'websites'.
Let's suppose your german store has 3 as Id, here is the code that would work :
$config = Mage::getModel('core/config');
$config->saveConfig('path/to/config/', 'value_to_set', 'stores', 3);
For a website-scoped config :
$config->saveConfig('path/to/config/', 'value_to_set', 'websites', $websiteId);
I've created (manually via Admin Panel) an attribute called att_by_website which is scoped as 'Website' and I'm want to get its several values.
How can I do this? I'm able to set different values via Admin Panel, but at this time I'm only able to get the default value.
When I execute $this->getProduct()->getData('att_by_website') it returns only the default value (where $this->getProduct() returns an instance of Mage_Catalog_Model_Product).
Thanks!
It may not be the best approach, but solved the problem:
$value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productID, 'att_by_website', $storeId);
Try this:
foreach (Mage::app()->getWebsites() as $website) {
$websiteId = $website->getWebsiteId();
$storeId = $website->getDefaultGroup()->getDefaultStoreId();
Mage::app()->setCurrentStore($storeId);
echo $this->getProduct()->getData('att_by_website');
}
I have created a new product attribute in Magento using the admin panel. But I can't find where to put a note for that attribute (that is the text that should display below the input field as extra info about it.)
Am I missing something, or is this not possible to implement in the admin panel?
This is an example of how a note on an attribute would look like:
I am using Magento 1.7 CE.
It's not possible through the admin Panel.
By the way, you really should add your attributes programatically. (if you database crash, your done for).
What you need is to modify the column "note" of the eav_attribute table.
You can modify it with the updateAttribute() command in an upgrade script. Example :
$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, 'color', 'note','my comment');
hmm this might be a bit late, but anyway, I had a similar problem today and solved it in the following intuitive way:
first my problem:
howto get in the extended-search-page the attributes in the a custom order?
for this I've found something here:
http://www.magentocommerce.com/boards/viewthread/11782/
-- they tell:
U might use the custom not used field ´note´ from the mysql table: ´eav_attribute´
therefor you can change in
app\code\core\Mage\CatalogSearch\Model\Advanced.php
the order-by
i.e.
from: ->setOrder('main_table.attribute_id', 'asc')
to: ->setOrder('main_table.note', 'asc')
But then, you still have the prob, how to edit the things properly, without edit anything directly by in a mysql-client; from now on, I have had your problem, my solution:
Model: get/set
in app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php
grep: class Mage_Eav_Model_Entity_Attribute_Abstract ex
add: best after search: "n getName"
/**
* Get attribute note
*
* #return string
*/
public function getNote()
{
return $this->_getData('note');
}
/**
* Set attribute note
*
* #param string $name
* #return Mage_Eav_Model_Entity_Attribute_Abstract
*/
public function setNote($note)
{
return $this->setData('note', $note);
}
Controler: set
in app/code/core/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php
(o.k. this is a shortcut, I think you can add some attributes to "note_text" somewhere, but I have no plan from magento)
search for: "default value" and extend:
from:
$defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
if ($defaultValueField) {
$data['default_value'] = $this->getRequest()->getParam($defaultValueField);
}
to:
$defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
if ($defaultValueField) {
$data['default_value'] = $this->getRequest()->getParam($defaultValueField);
$data['note'] = $this->getRequest()->getParam('note_text');
}
View:
in ./app/code/core/Mage/Eav/Block/Adminhtml/Attribute/Edit/Main/Abstract.php
after:
$fieldset->addField('default_value_text', 'text', array(
add:
$fieldset->addField('note_text', 'text', array(
'name' => 'note_text',
'label' => Mage::helper('eav')->__('Note Value'),
'title' => Mage::helper('eav')->__('Note Value'),
'value' => $attributeObject->getDefaultValue(),
));
finally I've initiallized the note-field in the mysql table by:
update eav_attribute set note=lpad(attribute_id,10,'0000000000') where attribute_id not in(84,100);
where attributes with the id 84 and 100 have had already some notes.
I have a multi-store setup and I am setting a Product's Attribute for a particular store to use the "Use Default Value" option - (ie to use the value in the Store View), as follows:
$_product = Mage::getModel('catalog/product');
$_product->load($productId);
$_product->setStoreId($storeId)->setName(false)->save();
This sets the Name attribute of storeId for $productId to use "Use Default Value"
Given that I have a lot of attributes to set I am trying to use:
Mage::getSingleton('catalog/product_action')->updateAttributes(array($productId), array('name' => false), $storeId);
But this is not setting the "Use Default Value" checkbox to true.
How can I use ->updateAttributes to set a store value to use the "Use Default Value" option?
Screenshot:
The "Use Default Value" flag isn't stored in the database anywhere.
Magento core uses that flag to do this when saving products:
/**
* Check "Use Default Value" checkboxes values
*/
if ($useDefaults = $this->getRequest()->getPost('use_default')) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
Before doing some other things.
I would look at Mage_Adminhtml_Catalog_ProductController (app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php) and learn how Magento core does it.
Specifically saveAction() and _initProductSave()
I hope this points you in the right direction.
Simply use 0 as the store ID (admin store) which is the same thing as default values in the Magento Admin.
Mage::getSingleton('catalog/product_action')
->updateAttributes(
array($productId),
array('name' => false),
0);
If you already set the store view scopes you will have to go in and recheck the use default values or it will override the attribute at the relevant scope.
There may be a way to programmatically set these. I'm uncertain.
I'm sorry if this question is trivial but I've been struggling to find what I am doing wrong here. I am trying to change the value of an attribute on a store view level but the default is also changed whereas it shouldn't be. Of course, this attribute is set up to be "store-view-scoped". To keep it simple, I've tried with the product name. No success.
Below are unsuccessful tests I've tried...
Do you see what I am doing wrong here?
Many thanks.
My tries :
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setStore(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
I even tried by adding the line below before the product model load...
Mage::app()->setCurrentStore(STORE_ID);
So here is the complete snippet to change attribute value for a specific product attribute on a specific store view. Example with the product name :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('my_new_product_name')->save();
And as an additional answer, one could be interested in changing the attribute value to the default one. In this case, the argument 'false' must be passed to the setAttribute :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName(false)->save();
You need to set the current store to admin at the top of your code block:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
note when loading product with data for some store view, also default values get loaded. saving such product will save default values as store values (thus unset "Use Default Value" for fields)
i ended up with following function to clean-up product data from default values
public static function _removeDefaults($item) {
static $attributeCodes = null;
if($attributeCodes == null) {
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
$attributeCodes = array();
foreach($attributes as $attribute) {
$ac = $attribute->getAttributeCode();
if(in_array($ac, array('sku','has_options','required_options','created_at','updated_at','category_ids'))) {
continue;
}
$attributeCodes[] = $ac;
}
}
foreach($attributeCodes as $ac) {
if(false == $item->getExistsStoreValueFlag($ac)) {
$item->unsetData($ac);
}
}
}
remember to send only product loaded for some store view