I am new in magento. I am writing a code like:
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort("entity_id", "DESC")
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setVisibility ( Mage::getSingleton( 'catalog/product_visibility')->getVisibleInSiteIds())
->setOrder($this->get_order(), $this->get_order_dir());
then again when using condition, by using the value of $products I am writing the code like:
if($size != ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->joinLeft(array('cpf'=>'catalog_product_flat_1'),'cpf.entity_id=e.entity_id',array('shhirt_size'))
->where('at_int.value=30 AND cpf.shhirt_size='.$size.' AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ')
;
}else if($size == ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->where('at_int.value=30 AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ' )
;
}
It is working fine. But here code is re-written. How to write the above code such a manner, only where clause would be in if part and else part.
Please help me. Thanks in advance.