Magento - Save a Product without setting "Use Default Value" - magento

I have a multi-store setup - StoreA, StoreB and StoreC. And within a controller (using the StoreA url) I'm editing a product as follows:
$_product = new Mage_Catalog_Model_Product();
$_product->load($productId);
$_product->setData('attribute1','somevalue');
$_product->save();
If I then go to the Admin / Edit Product for that particular product, I find that attribute1 has been correctly set BUT I also find that if I select StoreA in the "Choose Shop View" I find that all the "Use Default Value" check-boxes (for StoreA) have been set to false.
Questions:
How do I modify the code above so the "Use Default Value"
check-boxes for StoreA remain true
Once that the above code has been executed (and the "Use Default
Value" for StoreA set to false), how do I revert the "Use Default
Value" values back to true for StoreA?
Edit:
Added screenshot:

I'm not entirely clear on the 'Use Default Value' check-boxes you are seeing, but there are two things I noticed.
Using The Factory Pattern
Your code:
$_product = new Mage_Catalog_Model_Product();
Use the factory pattern, which is standard in Magento:
$_product = Mage::getModel('catalog/product');
That in itself isn't the problem, but it's something to keep in mind.
Updating Only The Product Attribute
Next, if you are only saving a specific attribute it would be quicker (and potentially avoid your problem) if you update only that attribute. Like so:
$attribute = array('attribute_code' => 'attribute_value');
Mage::getSingleton('catalog/product_action')
->updateAttributes($_product->getId(), $attribute, 0);
Reference for the updateAttributes() method. My reasoning here is that it's possible default values are being added by saving the entire product, and not just a specific attribute.
If that doesn't help, perhaps a screenshot of what you are seeing could better help me visualize the issue.

Related

Product attribute as link on other page

I have product with attribute address - where is product places. It is dropdown list. I want this atttribute will be a link on other site page with map and some other info about this address. But when i use html code in editor of attribute options it's not working. At the same time i checked property "Allow HTML Tags on Frontend" - it has "Yes" value. How can i do this?
Here is how my attribute looks like. and it seams to work:
For the attribute settings I just set to Yes Allow HTML Tags on Frontend and Visible on Product View Page on Front-end. The rest is set to No.
Here is how it looks like in the backend when editing a product.
And here is how looks in the frontend. And the link actually works.
I'm using CE-1.7.0.2.
Maybe the version is the problem. You can also try reindexing after you add the attribute.
[EDIT]
It seams that for 1.9 something changed.
See these lines in the attribute controller _filterPostData method:
foreach ($data['option']['value'] as $key => $values) {
$data['option']['value'][$key] = array_map(array($helperCatalog, 'stripTags'), $values);
}
You can view them here also: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php#L167
The filter method looked differently in 1.8 and before: https://github.com/OpenMage/magento-mirror/blob/magento-1.8/app/code/core/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php#L153. It didn't have the strip tags on the options. Just the label.
I guess you can try to override the Mage_Adminhtml_Catalog_Product_AttributeController::_filterPostData method and make it look like it does in 1.8. But I don't know the risks.

Magento, how to set up language from code

I was wondering, if there is any way to set mangeto language for specific area from Code?
What i mean is lets say i want a custom page to be in Spanish while other pages in my website in English.
Set cookie onChange event of custom drop in following manner
$cookie = Mage::getSingleton('core/cookie');
$cookie->set('selectedlang', 'spanish' ,time()+86400,'/');
and then make a if else statement
if($cookie->get('selectedlang')=='spanish')
//Show your spanish content
else
//Show your default content

Magento Can't mass update attributes for Required Field Error

I’m hoping someone can shed some light on this:
I’m using the search facility in Magento to find products with a certain attribute, and then selecting those products and using the “update attributes” feature to add data to (update) a different attribute field.
In some cases, this works fine, the new attribute data is recorded and all is well. On other selections when I try and save the new attribute data it comes up with the red warning “this is a required field”. Sure enough, the field it specifies IS a required field, but as I’m not updating that particular field I’ve not ticked the check box underneath it and this field should be ignored.
If I turn off the “required field” option for the highlighted fields then the new attribute data gets saved BUT: It wipes the data in loads of the other attributes, meaning I lose valuable data from my product attributes. It seems as if it works the first couple of times I use it in a day then it spits its dummy out and comes up with the required field issue.
I’ve tried this with both Google Chrome and Firefox. As I’ve said though, sometimes it will work , sometimes it won’t . I’m selecting 20-100 products at a time. I’ve even tried selecting just one product, and can still get the same error on that one product.
I’m on Magento 1.7.0.2
I did have a similar problem with attribute data getting wiped last year when we were on 1.6 but I seem to remember it was a Magento bug which was fixed when we upgraded to a newer version.
Can anyone shed any light on why this might be happening? There’s nothing coming up in the search engines for it and its really slowing me down!
The problem is that the attributes are NOT getting disabled by default in the update attributes page, hence require a value.
Why?
Most likely due to a faulty attribute failing somewhere above your attribute.
Option 1. As I don't know what attribute or the attribute problem, hack js/mage/adminhtml/product.js and change this function to ignore the faulty attribute.
function disableFieldEditMode(fieldContainer) {
try {
$(fieldContainer).disabled = true;
if ($(fieldContainer + '_hidden')) {
$(fieldContainer + '_hidden').disabled = true;
}
}
catch(exception) {
}
}
Option 2. Remove the faulty attribute from the product (You might need to remove restrictions ).
update eav_attribute set is_user_defined = 1 where attribute_code like 'foo_bar';
Option 3. Fix the faulty attribute. In my case the offending module was AW_Onsale.
it attempted to render a nonexistent frontend model: onsale/system_entity_form_element_position
select attribute_code, frontend_model from eav_attribute where attribute_code like '%aw_os_%';
aw_os_product_position | onsale/entity_attribute_frontend_position
update eav_attribute set frontend_model = null where attribute_code like '%aw_os_product_position%';
update eav_attribute set frontend_model = null where attribute_code like '%aw_os_category_position%';

How to change load layout in Joomla view?

By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.
P.S.
Tried load loadTemplate and setLayout methods with no luck.
Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax
$this->setLayout('dafault:lol');
parent::display($tpl);
By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.
By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.
However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol
You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.
<?php
# ...
function display($tpl = null) {
# ...
# Edit : Set the default layout to 'lol'
$layout = JRequest::getWord('layout', '');
if (empty($layout)) $this->setLayout("lol");
// Display the view
parent::display($tpl);
}
# ...
I keep coming back to this and I've yet to find a satisfying solution.
What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php
If $tpl is empty or "" then tmpl/default.php is displayed by default.
If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php
I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php
None have worked for me.

Magento - get store translated custom attribute label in "checkout/cart.phtml"

In the shoppin cart; "checkout/cart.phtml" page, I want to add a column that contains a custom attribute. Working with a multilingual Website, I have to show the translated label of each Store.
I have tried this code:
$attributeLabel = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('length')
->getFirstItem()
->getFrontend_label();
echo "<pre>"; var_dump($attributeLabel); echo "</pre>";
but this shows the attribute admin label.
How can I get the translated labels for earch store ?
Thanks.
you will need to get the resource. Simply pull it from the product (or from the resource model). Then get the Attribute Object and instead of getting the frontend label you will need to get the store label.
$_product->getResource()->getAttribute('attributecode')->getStoreLabel();
alternatively
Mage::getModel('catalog/resource_product')->getAttribute($_attribute)->getStoreLabel();
also IMPORTANT: your attribute needs to have a Value Configured for that Language. (this is what got me here ;) )
getFrontendLabel() or getData('frontend_label'). You're confusing syntaxes.

Resources