Saturday, April 28, 2012

Product Collection in Magento


In this blog, we will see some important function in magento product collection class.
Product Collection class in magento is Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. Lets look at the important functions
Get All Products of a category


$collection = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId($this->getStoreId())
            ->addCategoryFilter($category);

Tn this the addCategoryFilter() function, is used to get all products of a particular category. So, if you want to get all products of a certain category use this function.
Visibility Filter


$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);


The addVisibleFilterToCollection() adds visibility filter to a product collection i.e only products which are visible in frontend. The product which have “Not Visible Individually” selected in admin are removed.
Status Filter


$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);


This basically filters out products which are “Disabled”. Only “Enabled” products remain in the collection.
Add Product Price To Collection


$collection = Mage::getResourceModel('catalog/product_collection');
$collection ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents();


This adds the product prices, i.e base price, final price etc to the collection. Also, price after tax, if applicable.
Filter By Ids


$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter(array(1,2,3));
//$collection->addIdFilter(array(1,2,3),false);


This puts an id filter, only product with ids 1,2,3 remain in the collection. The function parameter is true/false, this means include/exclude products from collection.
Add Website ID to the collection


$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addWebsiteNamesToResult();


Wednesday, April 25, 2012

How to add Pinterest Button!


Use the Following Code to Use Pinterest Button to share your page on Facebook and Twitter simultaneously -

Tuesday, March 6, 2012

How to Set Registration Block on Home Page ?

I was not about to spend time coding up so the easiest thing to do was to place the call in a static block
{{block type="Mage_Customer_Block_Form_Register" template="customer/form/register.phtml"}}

Tuesday, February 14, 2012

Most Frequently Used Functions in Magento

1)Magento: How to call static block from template file?
We can Easily call a static block directly from template.
Let us suppose, you created a static block with the identifier aforankur_link.
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId(‘aforankur_link’)->toHTML();?>

2)Magento: How to call block directly from phtml file without defining in layout?
<?php echo $this->getLayout()->createBlock(‘newmodule/newblock’)->setTemplate(‘newmodule/newblock.phtml’)->toHtml(); ?>

3)Magento: Add Static Block to CMS Page
{{block type=”cms/block” block_id=”home-page-promo”}}

4)Magento :How to call a phtml file in magento cms page
{{block type=”core/template” name=”a-name” template=”cms/home.phtml”}}


5) Magento Store – Get Base URL in Static Block
Try adding this to your static block:
<a href=”{{store url=”"}}”>Link to Base URL</a>

6) Mageto  : Skin URL
<a href=”#”><img src=”{{skin url=’images/home/dog-playing.jpg’}}” alt=”"/></a>

7) Magento: get Url
<a href=”<?php echo $this->getUrl(‘about-us’)?>”>

Thursday, February 9, 2012

Magento - Display total weight of Products in Cart

Here is some code snippet need to put -

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$weight = 0;
foreach($items as $item) {
    $weight += ($item->getWeight() * $item->getQty()) ;
}
echo $weight;

Custom Code for Sending Email to Customer



$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

$order->loadByIncrementId($incrementId);

try
{
$order->sendNewOrderEmail();
} catch (Exception $ex) {  }

Wednesday, February 8, 2012

Magento | Use Mini-Login in Header or anywhere you want!

Add Mini-login at Header

Reach to following path and open the given xml file - 

app/design/frontend/default/default/layout/page.xml
  <block type="customer/form_login" name="mini_login" template="customer/form/mini.login.phtml" />
Then, you just have to call the module :
 <?php echo $this->getChildHtml('mini_login'?>

Thursday, February 2, 2012

Magento: Upgrading mysql setup of a module

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 inMyModule/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 necessary database changes. To do so,
---------------------------------------------------------

1) You need to create a new mysql upgrade file inside MyModule/sql/mymodule_setupfolder.
Let us suppose that you are going to change the version of your module from 0.1.0 to 0.1.1.
The mysql upgrade file name should be mysql4-upgrade-0.1.0-0.1.1.php
The upgrade format is like mysql4-upgrade-CURRENT_VERSION-UPGRADED_VERSION.php
2) Write necessary sql statements in the newly added mysql upgrade file.
3) You have to change the version in MyModule/etc/config.xml as well.
Change the version like 0.1.1. 0.1.1 is the new version for our module.
4) Reload your site. And you are done!