Magento add/remove product attribute programatically
Magento add product attribute using sql setup file in your module. Also assign your custom attribute to attribute set Default and group General programatically.
Below code will add your new attribute in Manage Products edit screen at the end of General tab with dropdown values Yes/No. It will not display in frontend website but you can change visibible on front to 1 if you wish. Note that it will assign to Default attribute set only but you can change it to whatever as per your requirement.
$model = Mage::getResourceModel('catalog/setup','catalog_setup'); | |
$data=array( | |
'type'=>'int', | |
'input'=>'boolean', //for Yes/No dropdown | |
'sort_order'=>50, | |
'label'=>'CUSTOM ATTRIBUTE CODE LABEL', | |
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, | |
'required'=>'0', | |
'comparable'=>'0', | |
'searchable'=>'0', | |
'is_configurable'=>'1', | |
'user_defined'=>'1', | |
'visible_on_front' => 0, //want to show on frontend? | |
'visible_in_advanced_search' => 0, | |
'is_html_allowed_on_front' => 0, | |
'required'=> 0, | |
'unique'=>false, | |
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable | |
'is_configurable' => false | |
); | |
$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data); | |
$model->addAttributeToSet( | |
'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE' | |
); //Default = attribute set, General = attribute group |
To remove the product attribute using the setup file, use below code instead:
$model = Mage::getResourceModel('catalog/setup','catalog_setup'); | |
$model->removeAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE'); |
Magento delete empty categories and sub-categories
Remove all empty categories and sub-categories in Magento. When there are empty categories, the website shows empty page in those categories in frontend. Create a file in the magento root, I will name it rmvEmptyCats.php, with following code:
require "app/Mage.php"; | |
umask(0); | |
Mage::app(); | |
$categoryCollection = Mage::getModel('catalog/category')->getCollection() | |
->addFieldToFilter('level', array('gteq' => 2)); //greater than root category id | |
foreach($categoryCollection as $category) { | |
if ($category->getProductCount() === 0) { | |
$category->delete(); | |
} | |
} | |
echo 'Empty Categories Deleted!'; |
Now you can easily run it by navigating to http://loca.lho.st/rmvEmptyCats.php and wait for the message Empty Categories Deleted!
Note that this is going to DELETE those categories with zero product count.
Magento get all categories of a product
Get all the categories a product belongs to in Magento. Below code will get you all the categories with details the product is attached to. Product can be shown under more than one category, so you may get more than one category ID. Either get the category collection from product, or get all the category IDs and then load them using catalog category collection model.
//$_product = Mage::getModel('catalog/product')->load($productID); |
First way,
$catCollection = $_product->getCategoryCollection(); | |
foreach($catCollection as $cat){ | |
print_r($cat->getData()); | |
//echo $cat->getName(); | |
//echo $cat->getUrl(); | |
} |
Another way, Continue reading »
Magento products not showing in categories
If your Magento products are not showing up in frontend category pages, that can be because one or more of the below points are not done correctly.
The products must have following set correctly to appear in the category pages in frontend. You can check all the below things in Manage Products screen of any individual product.
– Status must be Enabled (under General tab)
– Visiblility should be Catalog OR Catalog, Search (under General tab)
– Stock Qty (quantity) should be greater than Qty for Item’s Status to Become Out of Stock (under Inventory tab)
– Stock Availability must be IN STOCK
– Category should be assigned (under Categories tab)
– Website must be assigned (under Websites tab)
Clear the cache, re-index the indexes related to product (Product prices, Product flat data and Category products) and everything should work as expected.
This post is written to address the following issue:
magento products not displaying in categories
magento products not showing in categories
magento items not showing in categories
magento products not showing up in categories
magento products not showing under categories
magento products not displaying on category page
magento product images not showing in category
magento configurable product not showing in category
magento new product not showing in category
If you are unable to see Product Images in frontend category pages, check this blog post to resolve it: http://ka.lpe.sh/2013/02/26/magento-cant-see-product-images-in-category-page/
Magento get list of all product attributes
Get list of all the product attributes defined in Magento. This will fetch you an array with all the attribute codes as a key AND all the attribute details including attribute code as a value. You can limit this array by just attribute code and attribute label as per your need. I will show you all the possible attribute information you can fetch defined for the product.
$attrib_data = array(); $allAttributeCodes = array(); | |
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')->getItems(); | |
foreach ($attributes as $attribute){ | |
$attrib_data[$attribute->getAttributeCode()] = $attribute->getData(); | |
//$allAttributeCodes[] = $attribute->getAttributeCode(); | |
} |
Sample Output: Continue reading »
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)