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
}
}
Related
I'm using Magento 2, when I try add product in Backend, in the Configurations tab, I have created Configuration and I saw there three attributes.
How can I get them in a module on frontend?
I see they are stored in eav_attribute table but I dont know which SQL can be do it, because it has no conditional column
Thank so much!
Use can use below code to get attribute in your module frontend.
$attribute = $objectManager->create('\Magento\Eav\Model\Config')->getAttribute('catalog_product', 'color');
$colorAttributeId= $attribute->getAttributeId();
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
$colors[$option['value']] = strtolower($option['label']);
}
print_r($colors);
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);
}
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');
}
How to check if attribute exist in product attribute set?
I need to know if a product has an attribute for its set of attributes.
I get the attribute with:
$attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg();
If attribut exist in product attribute set, $attrPricekg display: set value for the product
or 0 if no value set for the product.
If the attribute does not exist in product attribute set, $attrPricekg display 0. This is my problem.. I need to avoid this, I want to check that the attribute does not exist for that product.
Thanks.
now I'll provide an answer which works regardless!
$product = Mage::getModel('catalog/product')->load(16);
$eavConfig = Mage::getModel('eav/config');
/* #var $eavConfig Mage_Eav_Model_Config */
$attributes = $eavConfig->getEntityAttributeCodes(
Mage_Catalog_Model_Product::ENTITY,
$product
);
if (in_array('pricekg',$attributes)) {
// your logic
}
To check if a specific attribute exists in product, it should return true even if the attribute has the value 'null'.
One way that works is:
$attr = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product',$code);
if (null!==$attr->getId())
{
//attribute exists code here
}
It can also of course be written in one line:
if(null!===Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product','attributecode_to_look_for')->getId()) {
//'attributecode_to_look_for' exists code here
}
Found it and modified a bit on:
https://github.com/astorm/Pulsestorm/issues/3
May be this way is better for you:
$attribute = Mage::getModel('catalog/product')->load($productId)->getResource()->getAttribute($attributeCode);
if ($attribute && $attribute->getId()) { ... }
Also you may try
$attributes = $product->getAttributes();
But you may check all in attribute collection:
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product')
->getTypeId();
$attributeId = 5;
$attributeSetName = 'Default';
$attributeSetId = Mage::getModel('eav/entity_attribute')
->getCollection()
->addFieldToFilter('entity_type_id', $entityTypeId)
->addFieldToFilter('attribute_set_name', $attributeSetName)
->addFieldToFilter('attribute_id', $attributeId)
->getFirstItem();
May be source code need some corrections, but I think you will understand the idea.
See some more examples here, also - http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/
EDIT: this is not the correct answer.
$product->offsetExists('pricekg');
See Varien_Object::offsetExists() (link).
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