Magento: Adding attribute from MySql setup file

You can add attribute from Admin Panel -> Catalog -> Attributes -> Manage Attributes. You can also add attributes from mysql setup file of your module. MySql setup file is present inside “YourModule/sql/yourmodule_setup” directory. In the following example, the version of my module is 0.1.0.  I have added attribute for product. I have added a … Read more

Magento: Product Edit Warning: Invalid argument supplied for foreach()

Scenario: While editing product programatically from frontend. I was trying to change the status of the product with the following code. I was trying to disable the product. Status value 2 = Disabled. // Trying to disable the product // $product->getId() = PRODUCT ID Mage::getModel('catalog/product')->load($product->getId())->setStatus(2)->save(); Problem: Product could not be edited programatically from frontend. The … Read more

Magento: Upgrading mysql setup of a module

Suppose, you have a module called MyModule. Its version is 0.1.0. Now, you want to do some database changes for the module. You have the mysql setup file (mysql install file) mysql4-install-0.1.0.php in MyModule/sql/mymodule_setup folder of your module. You don’t need to make direct changes to database. You can upgrade your module to make your … Read more

Magento: Get all categories name and url of a product

This article shows how you can fetch all the categories related/associated with a particular product. $product->getCategoryIds() function gives array of category ids with which the product is associated to. We can loop through this array and load each category to get the category name and url. // Load product $productId = 1; // YOUR PRODUCT … Read more

Magento: Get store information

Magento allows to maintain multiple websites from single backend admin. Furthermore, each website can have multiple stores. Below is the code to get Magento store information like store id, store code, store name, etc. All of these functions can be found in class Mage_Core_Model_Store. Get store data array // Get all store data in an … Read more

Magento: Get height and width of Product Image

The following functions can be used to get product’s image height and width. Image functions are defined in Mage_Catalog_Helper_Image class. getOriginalWidth gives width of image. getOriginalHeigh or getOriginalHeight gives height of image. getOriginalSizeArray gives the height and width of an image in array. $_product = $this->getProduct(); $imageWidth = $this->helper('catalog/image')->init($_product, 'image')->getOriginalWidth(); if($imageWidth > 600) { $lightboxImage … Read more