How to change magento config programatically for different stores? - magento

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

Related

Create handle with 2 variables

I would like to create a layout specified for only simple product with attribute set id of 4.
currently my code is something like this:
public function initProductLayout(ResultPage $resultPage, $product, $params = null)
{
$resultPage->addPageLayoutHandles(
['attribute_set_id' => $product->getAttributeSetId()]
);
and i have this layout catalog_product_view_attribute_set_id_4.xml
its all good in the products page but my problem is that catalog_product_type_bundle.xml is not being used. So I would like to have a layout file which is like catalog_product_type_bundle_attribute_set_id_4.xml if it is possible.
It is possible to get the layout file you are talking about. To do this you should add this code in addition to one you already have
$resultPage->addPageLayoutHandles(['type_'.$product->getTypeId().'_attribute_set_id' => $product->getAttributeSetId()]);
As a result you will get this

Translating customer prefixes in Magento

I'm trying to translate the prefix like 'mr.' and 'mrs.' those are set in: System >> Configuration >> Customer Configuration >> Prefix Dropdown Options
I need it in English, Dutch and German. Each language is a separate storeview.
I've added the translation to multiple .csv files like the themes translate.csv and the Mage_Core.csv
The default <?php echo $this->__($prefix) ?> works on the frontend like the checkout. But in the backend and emails it isn't translated.
Any way to translate those?
I found two ways to fix this.
remove the build in prefix and add a new one with custom prefixes.
overrule the Mage_Customer_Helper_Data class and it's getNamePrefixOptions method.
I used Nr2
NR 1: is more work, you will need to update tempaltes. But it is easier to manage for a customer if needs to ad a new translation.
NR2: You can add more prefix values in the config. Then in the class make a switch which to use in which store/language. Adding a new language will probably need to add more code the the class.
What I used to overrule the getNamePrefixOptions
public function getNamePrefixOptions($store = null)
{
$pre_val = $this->_prepareNamePrefixSuffixOptions(
Mage::helper('customer/address')->getConfig('prefix_options', $store)
);
// Show all prefixes in the admin
if (Mage::app()->getStore()->isAdmin()) {
return $pre_val;
}
// Transform array keys to integers
$pre_val = array_values( $pre_val );
$lang_code = Mage::app()->getLocale()->getDefaultLocale();
$new_prefixes_values = array();
// Add language codes here, and add their prefix key's, count starts at 0
switch ( $lang_code ) {
case "nl_NL":
// Set key the same as the value, that's how Magetnto gives it at first
$new_prefixes_values = array( $pre_val[0] => $pre_val[0], $pre_val[1] => $pre_val[1]);
break;
case "de_DE":
$new_prefixes_values = array( $pre_val[2] => $pre_val[2], $pre_val[3] => $pre_val[3]);
break;
default: // other languages
$new_prefixes_values = array( $pre_val[0] => $pre_val[0], $pre_val[1] => $pre_val[1]);
break;
}
return $new_prefixes_values;
}
What I filled in the Prefix Dropdown Options setting: Dhr.;Mevr.;Herr;Frau
It is a bit hard coded but works and allows you to change the values

Magento - Get attribute value per website

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

Magento - how to add new product status

I'm trying to make new product statuses but i can't figure out how to do it and all the stuff set on the web is not consistent or simply talks about order status which i don't want to change.
What is your motivation to have new product statuses? I think it's little bit risky to change this part of app. I suggest you to add new attribute and use this one instead system product's attribute 'status', this attribute tells to system if product is enabled or disabled. I guess there is nothing between :)
Override class Mage_Catalog_Model_Product_Status to the local folder. Then open the file
\app\code\local\Mage\Catalog\Model\Product\Status.php
At the top of the file you can see the constants
const STATUS_ENABLED = 1;
const STATUS_DISABLED = 2;
Add your custom status below them, for example
const STATUS_SUSPENDED = 3;
Then edit the function getOptionArray
static public function getOptionArray()
{
return array(
self::STATUS_ENABLED => Mage::helper('catalog')->__('Enabled'),
self::STATUS_DISABLED => Mage::helper('catalog')->__('Disabled'),
self::STATUS_SUSPENDED => Mage::helper('catalog')->__('Suspended')
);
}
That's it. Don't forget to clear the cache.

Magento - Set product attribute to use default values

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();

Resources