Magento - Get attribute value per website - magento

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

Related

retrieve all products and their respective attributes using getAdditionalData in magento

I've been looking all over the web on how I would go about retrieving filtered products and then get their respective attributes using getAdditionalData. Here's what I do:
$_collectionProduct=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('status', array('eq' =>1))
->addAttributeToFilter('attribute_set_id',9)
->addAttributeToSelect('*');
This works and retrieves all the filtered products. However, now I need their attributes so I am looping
foreach ($_collectionProduct as $products) {
$_additional = $product->getAdditionalData();
}
What happens is that $_additional returns NULL.
Another scenario I have tried is the following
foreach ($_collectionProduct as $_products) {
$product= Mage::getModel("catalog/product")->load($_product->getId());
$_additional = $product->getAdditionalData();
}
This example still displays NULL. Would really appreciate if somebody would have a solution to the dilemma. Thanks.
I actually figured out why it wasn't working. It appears that getAdditionalData() is not really attached to a specific product. The product view is calling this function from core file. The actual function I needed to use was getAttributes().

How to programmatically disable product for all store-view in Magento?

I want to disable product programmatically for all store view. Please help me
I tried with the following... but no luck
$storeId = 0;
Mage::getModel('catalog/product_status')->updateProductStatus($product_id, $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
Firstly $storeId=0 is default store id for admin if you want disable product for all store view then you can set $storeId=Mage:app()->getStoreId()// this is for current store id
after that you can disable all product
$product_id=1;
$storeId=Mage::app()->getStoreId();
Mage::getModel('catalog/product_status')->updateProductStatus($product_id, $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
EDIT
This is for all store view i think this is the dirty way to achieve this
<?php
$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val)
{
$_storeId[] = Mage::app()->getStore($_eachStoreId)->getId();
}
for($i=0;$i<count($_storeId);$i++)
{
$product_id=1;
$storeId=$_storeId[$i];
Mage::getModel('catalog/product_status')->updateProductStatus($product_id, $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
}
?>
Let me know if you have any query
By default the scope of status attribute is set to store , if we will set it to global under manage attributes , than we can update status for all store views with below code.
$loadproduct = Mage::getModel("catalog/product")->load("product_id");
$loadproduct->setStatus(2);
$loadproduct->save();
thanks
There's a much shorter way than in the answer of Keyur Shah:
foreach (Mage::app()->getStores() as $store) {
Mage::getModel('catalog/product_status')->updateProductStatus($productId, $store->getId(), Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
}

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

Programmatically change product attribute at store view level

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

Magento attributes in attribute set

can someone please tell me how to check if an attribute has been created?
I have a block which displays products based on a given attribute.
The thing is if the attribute has not been created, I'm getting an error on the frontend.
Is there a way to check if the attribute exists?
Thanks.
Hopefully you can get lots of info about attributes and attributes set in the following article:
http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/
Thanks
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
Try above code which means check the status that is specified attribute available in magento ( if you know the id ).
Try this as well :
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront()) {
$value = $attribute->getFrontend()->getValue($product);
// do something with $value here
}
}

Resources