Archive for December, 2012

Join custom table to product collection in magento

$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'), 'schema_name_if_different');


For Example,


$collection = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('status')
                ->addAttributeToSelect('visibility')
                ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->addAttributeToFilter('visibility', array('neq' => 1));

$collection->getSelect()->join(array('mep' => "mage_brand_product"), "e.entity_id = mep.product_id", array('mep.*'));

Add small label image on New products Image in Magento

<?php  $now = date('Y-m-d');
 $newsFrom= substr($_product->getData('news_from_date'),0,10);
 $newsTo=  substr($_product->getData('news_to_date'),0,10);

if (($now>=$newsFrom && $now<=$newsTo)||($now>=$newsFrom && $newsFrom!='' && $newsTo=='' ))
{?><img alt="" src="<?php echo $this->getSkinUrl('images/icon-new.png');?>"><?php }?>

Custom Breadcrumbs for particular CMS page

Paste this code in CMS Page > Design > Page layout > Layout Update XML

 

<reference name="top.bar">
<action method="unsetChild"><alias>breadcrumbs</alias></action>
<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs">
    <action method="addCrumb">
        <crumbName>home</crumbName>
        <crumbInfo><label>Home page</label><title>Home page</title><link>/</link></crumbInfo>
    </action>
    <action method="addCrumb">
        <crumbName>myparentpage</crumbName>
        <crumbInfo><label>My Parent Page</label><title>My Parent Page</title><link>/myparentpage/</link></crumbInfo>
    </action>
    <action method="addCrumb">
        <crumbName>mysubpage</crumbName>
        <crumbInfo><label>My Sub Page</label><title>My Sub Page</title></crumbInfo>
    </action>
</block>
</reference>

 

Magento Date and Time and also with after 14 days date and time when order is created

$magento_date =  date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
$format = 'Y-m-d H:i:s';
$date = $_order->getCreatedAtFormated();
$fourteen_date = date($format,strtotime('14 days' .$date));