Magento: Writing Custom Log Message

To be able to write your custom log message, you have to enable logging from Magento Admin. To enable logging, go to Magento Admin Panel -> System -> Configuration -> Developer -> Log Settings -> Enabled = Yes Then in your code, you can write the following: Mage::log("Your Log Message"); You can check it at … Read more

Magento: Load store specific product

Different stores can be set in Magento. A product can be made visible in selected stores. /** * get store id */ $storeId = Mage::app()->getStore()->getId(); /** * call the Magento catalog/product model * set the current store ID * load the product */ $product = Mage::getModel('catalog/product') ->setStoreId($storeId) ->load($key); Hope it helps. Thanks.

Magento: Get parent id of simple product associated to configurable product

Scenario: A simple product is associated with a configurable product. You have the id of the simple product. Now, you need the id of the configurable product with which the simple product is associated. Get parent product id, i.e. get id of configurable product from a simple product. Note: This method is deprecated after 1.4.2.0 … Read more

Magento: Resize Image

You can resize image with fixed height and variable width. Or, you can resize with fixed width and variable height. Following code shows how you do it in Magento. Fixed width of 600px <?php echo $this->helper('catalog/image')->init($_product, 'image') ->constrainOnly(TRUE) ->keepAspectRatio(TRUE) ->keepFrame(FALSE) ->resize(600,null) ?> Fixed height of 600px <?php echo $this->helper('catalog/image')->init($_product, 'image') ->constrainOnly(TRUE) ->keepAspectRatio(TRUE) ->keepFrame(FALSE) ->resize(null,600) ?> … Read more

Magento: Get current URL of the page

Getting URL of a current page you are visiting or working on is a common requirement in any web development project. There are various ways you can get the current URL of a page in Magento. Here are some :- From Mage_Core_Helper_Url class $currentUrl = Mage::helper('core/url')->getCurrentUrl(); From Mage_Core_Model_Store class $currentUrl = Mage::app()->getStore()->getCurrentUrl(false); If you want … Read more

Magento: Format Price

When you fetch product data, you get the price as integer. Now, you have to display the price with currency suffix. You can format price with the following code. Let $_finalPrice be the integer price fetched. To display price with currency symbol: $formattedPrice = Mage::helper('core')->currency($_finalPrice,true,false); // OR $formattedPrice = Mage::helper('core')->formatPrice($_finalPrice, false); To display price without … Read more

Magento: Get Set Unset Session

Here are code to Get, Set, and Unset Session in Magento. Set session with variable Name ‘testing_magento‘. The session value here is ‘hello‘. Mage::getSingleton(‘core/session’)->setTestingMagento(‘hello’); Get session ‘testing_magento’ $test = Mage::getSingleton(‘core/session’)->getTestingMagento(); Unset session Mage::getSingleton(‘core/session’)->setTestingMagento(); Note: Use customer or core session in frontend. Use adminhtml session in the backend. Core Session:- Mage::getSingleton(‘core/session’) Customer Session:- Mage::getSingleton(‘customer/session’) Admin Session:- … Read more