Get list of product attribute in Magento

# Get list of colors from Magento Installation Let’s create a file called getcolors.php on Magento root folder. require_once ( “app/Mage.php” ); umask(0); Mage::app(“default”); Mage::getSingleton(“core/session”, array(“name” => “frontend”)); $attribute = Mage::getSingleton(‘eav/config’)->getAttribute(‘catalog_product’, ‘color’); if ($attribute->usesSource()) { $options = $attribute->getSource()->getAllOptions(false); } print_r($options); When you excute the file getcolors.php either from shell or browser, It will print the […]

Change a product’s attribute value without loading the entire product model

Loading product in a collection and saving them again and again over will take lot of time. To make it quick there is an alternate solution available which uses the same function used by Update attributes action from the product grid: Mage::getModel(‘catalog/product_action’) ->updateAttributes(array($productId), array(‘attribute_code’=>’value’), 0);

Better way to load layout blocks outside of Magento

For loading Magento Layout Blocks externally or in a controller without any complexity, use this: From external file, we need to initiate Magento session: Mage::getSingleton(‘core/session’, array(‘name’=>’frontend’)); $session = Mage::getSingleton(‘customer/session’, array(‘name’=>’frontend’)); $layout = Mage::app()->getLayout(); $layout->getUpdate()->addHandle(‘default’)->load(); $layout->generateXml()->generateBlocks(); $top = $layout->getBlock(‘top.links’)->toHtml(); echo $top; From internal controller: $layout = Mage::app()->getLayout(); $layout->getUpdate()->addHandle(‘default’)->load(); $layout->generateXml()->generateBlocks(); $top = $layout->getBlock(‘top.links’)->toHtml(); echo $top;

Openresty : Programming with Magento/Mysql – Get Inventory of Products REST API

Skills Requried – LUA, nginx, mysql If you have nginx-openresty installed, open the vhost file of your domain, than follow this: Let your REST API path is http://<your-domain>.com/getstockserv?p=123450,12145 where p is the parameter where all product ids of Magento Products has been supplied. Now add this entry to the vhost file – location ^~ /getstockserv […]

How to add indexer in a Custom Module?

Follow these steps: config.xml: <index>                        <indexer> <cms_indexer> <model>module/cmsindexer</model> </cms_indexer> </indexer> </index> Model: app/code/local/Name/Module/Model/Cmsindexer.php Class Module_Model_CmsIndexer extends Mage_Index_Model_Indexer_Abstract { public function getName(){ return ‘Add CMS Cache’; } public function getDescription(){ return ‘Rebuild Cache Index for all CMS Pages’; } protected function _processEvent(Mage_Index_Model_Event $event){ // process } public function reindexAll(){ //Your Function to be executed } ?>

Automate Magento Image Cache – Don’t let your website down when catalog image cache is flushed!

Problem – How to remove Magento Image Cache selectively, when you need to replace existing images and Clicking “Flush Catalog Image Cache” on Magento Cache Management will empty the cache folder which consumes lot of cpu plus memory, and for replacing few images Magento will generate all product images (say for replacing 2 product images […]

Magento Inventory Management – Desktop Tool

Download Magento Desktop Tool allows you to easily manage inventory on your Magento Store.   This application allows Store Owners to update product stock on their website. It also allows user to setup a  Gmail(Google) SMTP A/c so that they will get a copy of uploaded stock csv file, which allows them to keep record […]

Add an External JS in Magento

To Add an External JS without any problem use this in XML Layout: [box type=”info”]<reference name=”head”><block type=”core/text” name=”google.cdn.jquery”> <action method=”setText”><text><![CDATA[<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”></script><script type=”text/javascript”>jQuery.noConflict();</script>]]></text></action></block></reference> The External JS will be added to the <head> tag.

Magento : Front controller reached 100 router match iterations

In Magento basic urls look like this ‘{name-of-the-module}/{controller}/{action}’ , If the url looks like this ‘{name-of-the-module}/{controller}/’ it’s equivalent to ‘{name-of-the-module}/{controller}/index’ , If the url looks like this ‘{name-of-the-module}/’ it’s equivalent to ‘{name-of-the-module}/index/index’. For categories and products there is a routing table (core_url_rewrite) in which are saved different rewrite rules in order to have an url […]

Zend_Db_Statement_Exception with message ‘SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry’

Exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry” while Catalog Url Indexing Error: Next exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘xxxx/xxx/xxx-‘ for key ‘UNQ_CORE_URL_REWRITE_REQUEST_PATH_STORE_ID” in /data/apache/beta/lib/Zend/Db/Statement/Pdo.php:234 Stack trace: Soln: – Find out the request path on Url Rewrites Data, there must be more than single entries for […]

Magento Show Out of Stock Configurable Options – Magento 1.5+

To show Out of Stock options in configurable products, we need to make these changes: Create a Local Module : Mindgeek_Outofstock /app/code/local/Mindgeek/Outofstock/Product/Block/Product/View/Type/Configurable.php /app/code/local/Mindgeek/Outofstock/etc/config.xml /app/code/etc/modules/Mindgeek_Outofstock.xml Rewrite /app/code/core/Mage/Catalog/Product/Block/Product/View/Type/Configurable.php <config> <modules> <mindgeek_outofstock> <version>0.1.0</version> </mindgeek_outofstock> </modules> <global> <blocks> <catalog> <rewrite> <product_view_type_configurable>Mindgeek_Outofstock_Block_Product_View_Type_Configurable </product_view_type_configurable> </rewrite> </catalog> </blocks> </global> </config> In /app/code/core/Mage/Catalog/Product/Block/Product/View/Type/Configurable.php class Mindgeek_Outofstock_Block_Product_View_Type_Configurable  extends Mage_Catalog_Block_Product_View_Type_Configurable { //Rewrite getAllowProducts() function: public function […]

Disable Magento Visitor Logs – For Better Performance

Disabling Magento Visitor Logs can’t possible from Admin. Disabling Visitor Logs helps in store performance as well as reduces sql queries while surfing the store which decreases MySQL Server load. It also causes Integrity Constraint Error sometimes. So to disable Visitor logs you need rewrite a Core Model: /app/code/core/Mage/Log/Model/Visitor.php – Mage_Log_Model_Visitor Find the below coding […]

Magento Load Related, Upsell and crosssell Products by Ajax using dynamic block creation

To load related products using Ajax just pass current product id by Ajax request and create a controller and add the following code to load the related products: [box] //Register the Current Magento Product Mage::register(‘product’, Mage::getModel(‘catalog/product’)->load($productId)); $block = Mage::app()->getLayout()->createBlock(‘catalog/product_list_related’)->setTemplate(‘catalog/product/list/related.phtml’); $message .= $block->toHTml(); return $message;[/box]

Magento – Displaying Additional Product Attributes in Catalog List

Firstly I added new method to class Mage_Catalog_Block_Product_View_Attributes: function setProduct($productId) { if (!Mage::registry(‘product’) && $productId) { $this->_product = Mage::getModel(‘catalog/product’)->load($productId); } } Then I’ve added to /app/design/frontend/*/*/template/catalog/product/list.phtml this: <table class=”info”> <?php $add = new Mage_Catalog_Block_Product_View_Attributes(); $add->setProduct($_product->getId()); $_additional = $add->getAdditionalData(); ?> <?php foreach ($_additional as $_data): ?> <tr> <th><?php echo $_data[‘label’] ?></th> <td><?php echo $_data[‘value’] ?></td> </tr> <?php endforeach; ?> </table>

Magento CONNECT ERROR: Can’t write to file: downloader/.cache/community/

Magento Connect in Magento 1.5 sometimes throws the following error message on attempt to install some extension (we have noticed this on BusinessDecision_Interaktingslider): CONNECT ERROR: Can’t write to file: downloader/.cache/community/BusinessDecision_Interaktingslider-1.0.6/app/code/community/BusinessDecision/Interaktingslider/sql/interaktingslider_setup/mysql4-install-1.0.6.php You have checked all permissions and sure that the destination is writeable, yet the Magento Connect cannot create this file. The reason is that function working with […]

Magento redirect simple product to parent configurable product

Add this code in _initProduct() method in IndexController or ProductController in /app/code/core/Catalog/Product/controllers (Create same file in your local folder), which redirects the simple product to parent configurable product. if($product->type_id==”simple”) { $configurable_product = Mage::getModel(‘catalog/product_type_configurable’); $parentIdArray = $configurable_product->getParentIdsByChild($product->getId()); if(isset($parentIdArray[0])) { $product = Mage::getModel(‘catalog/product’)->load($parentIdArray[0]); } } Note:

Adding a Cron Job in Magento

Add the following lines to your module’s config.xml file under etc folder. <crontab> <jobs>             <namespace_yourmodulename>                 <schedule><cron_expr>*/1 * * * *</cron_expr></schedule> <run><model>navision/observer::methodToCall</model></run> </namespace_yourmodulename> </jobs> </crontab>   Observer: <?php class Namespace_Yourmodulename_Model_Observer { public static function methodToCall(){ //Code to Execute Mage::log(“Cron Executed”); } } ?> Note: Magento root file cron.php needs to be added to crontab […]