Remove attribute value from product - magento

We have attributes with a default value, even if the attribute is not in the products attribute set, the default value for these products are shown in layered navigation.
Example
My keyboards category gets Mouse attributes shown in layered navigation with the default attribute value. So all my keyboards get "Right handed".
How can i remove the attribute value for these products ?

Maybe something like this:
$catid = 'your cat. id';
$category = Mage::getModel('catalog/category');
$category->load($catid);
$prodCollection = $category->getProductCollection();
foreach ($prodCollection as $product) {
$prdIds[] = $product->getId(); ///Store all the product id in $prdIds array
}
foreach($prdIds as $product){
$attributeCode = "your attribute code";
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product);
$_product->setData($attributeCode, "")
->getResource()
->saveAttribute($_product, $attributeCode);
echo $_product->getName() . ' is succesfully corrected' . '<BR>'; ;
}
Please let me know if this helps you out.

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

Magento Show custom attribute dropdown option for a simple product in cart page

how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options

Getting Category Active Status from catalog/category model

I am trying to build an array of the active categories on level2.
The problem I have is that the model is returning categories that are no longer active and I do not see a way to filter them.
$storeId = Mage::app()->getStore()->getId();
$category = Mage::getModel('catalog/category')->setStoreId($storeId);
$categoryCollection = $category->getCollection();
$categoryCollectionIds = $categoryCollection->getAllIds();
$level2Categories = array();
foreach($categoryCollectionIds as $categoryId){
$category->load($categoryId);
if($category->getLevel() == '2'){
$level1Categories[$categoryId] = $category->getName();
}
}
echo "<pre>";
print_r($level1Categories);
echo "</pre>";
Any ideas on how to achieve this?
You can filter your category collection to display active categories using the method addIsActiveFilter()
$categoryCollection = $category->getCollection()->addIsActiveFilter();

Get attributes and its values using single Collection in Magento?

How to get attributes and its values using single Collection in Magento?
Right now i am using below
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter(4)
->addFieldToFilter('frontend_input','multiselect')
->addSetInfo()
->getData();
to get attribute and below code to get attribute value
$product = Mage::getModel('catalog/product');
$collection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeName);
My attributes code out put is like below
Color :
Black
Blue
Green
Brand :
Hp
Dell
Apple
Size :
12
14
16
Thanks,
Balan
How about this:
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)
->getAttributeCollection()
->addFieldToFilter('source_model', array('notnull' => true))
->addSetInfo();
foreach ($attributes as $attribute)
{
echo "{$attribute->getFrontendLabel()}:\n";
foreach ($attribute->getSource()->getAllOptions() as $option)
{
echo " {$option['label']}\n";
}
echo "\n";
}
By using the eav/config and eav/entity_type chances are you will be reusing an already loaded collection, which of course is more efficient then reloading the same data in new collections.
EDIT: Updated the answer to include the attribute collection filter suggested by Aleksandr Ryabov.

How to get all active attributes of products in Magento?

I am working in a new "Filter by Product" module in Magento, i have a situation where i should retrieve all attributes and their values. I Googled this and found the below code
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->load();
// ->addFieldToFilter('attribute_code', 'color')
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$attr = $attribute->getSource()->getAllOptions(true);
foreach ($attr as $att) {
echo " Label : ".$att['label']." Value : ".$att['value']."";
}
but this retrieves only the label and value of last attribute from list of all available attributes.
how to i get all the attributes? what am i doing wrong in this code?
Thanks,
Balan
Try this:
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
// Localize attribute label (if you need it)
$attributes->addStoreLabel(Mage::app()->getStore()->getId());
// Loop over all attributes
foreach ($attributes as $attr) {
/* #var $attr Mage_Eav_Model_Entity_Attribute */
// get the store label value
$label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
echo "Attribute: {$label}\n";
// If it is an attribute with predefined values
if ($attr->usesSource()) {
// Get all option values ans labels
$options = $attr->getSource()->getAllOptions();
// Output all option labels and values
foreach ($options as $option)
{
echo " {$option['label']} (Value {$option['value']})\n";
}
}
else
{
// Just for clarification of the debug code
echo " No select or multiselect attribute\n";
}
}
This is the first solution:
$products = Mage::getModel('catalog/product')->getCollection();
foreach($this->products as $product) {
$prod = Mage::getModel('catalog/product')->load($product->getId());
}
If you are using using the data from outside magento, you can use a class I made:
http://blog.alexparadise.com/magento-dbeav-class/
It's beta but that should work in your case.
Or get the concept of the EAV table... and make your own SQL query.

Resources