Tuesday, February 21, 2012

Adding New option in catgeory sort by OR Adding Most viewed or most popluar option for sort by in category list page in Magento

Hi,

If you need to added new sort by option like most viewed etc.. follow the steps

override app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

in local folder and add or override only this 2 functions
Replace default Toolbar.php code by this code (be care full on class name it depends on your module name )

I took ex;- Most Viewed



<?php
/**
pradeep.kumarrcs67@gmail.com

*/
class Custommodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

/**
* Retrieve available Order fields list
*
* @return array
*/
public function getAvailableOrders()
{


$this->addOrderToAvailableOrders('mostviewd', 'Most Popular');

//echo $this->getCurrentOrder(); exit;
$this->removeOrderFromAvailableOrders('position');
$this->removeOrderFromAvailableOrders('relevance');
$this->setDefaultOrder('mostviewd');
$this->setDefaultDirection('asc');
$this->removeOrderFromAvailableOrders('entity_id');
krsort($this->_availableOrder);
return $this->_availableOrder;
}


public function setCollection($collection)
{
$this->_collection = $collection;

$this->_collection->setCurPage($this->getCurrentPage());

// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
if($this->getCurrentOrder()=='mostviewd') {
$this->_collection->getSelect()->
joinInner('report_event AS _table_views',
' _table_views.object_id = e.entity_id',
'COUNT(_table_views.event_id) AS views')->
group('e.entity_id')->order('views DESC');
}

$sql = $this->_collection->getSelectSql(true);

$this->_collection->getSelect()->reset()->from(
array('e' =>new Zend_Db_Expr("({$sql})")),
array('e' => "*")
);
$limit= $this->getDefaultPerPageValue();
$page = $this->getRequest()->getParam('p');

if(isset($page))
if(($page - 1) == 0)
$this->_collection->getSelect()->limit($limit);
else
$this->_collection->getSelect()->limit($limit, ($page - 1) * $limit);
else
$this->_collection->getSelect()->limit($limit);

$this->_collection->load();


// $this->_collection->printlogquery(true); exit;
return $this;
}


}


You can also check all attached file in

http://www.magentocommerce.com/boards/viewthread/273724/

Thursday, February 9, 2012

Displaying Price filter of catgeory or layer filter in magento programmatically

Hi,

If you need to display layer price filter, we can display in 2 type

one is calling default block but issue is some time price will not display if you click price filter or if cache is disabled
Default Price filter

public function priceHtmlnav($maincategoryId) {
$html ='';
$layer = Mage::getModel('catalog/layer');
$category = Mage::getModel('catalog/category')->load($maincategoryId);
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
$r=Mage::getModel('catalog/layer_filter_price')
->setLayer($layer);

$range = $r->getPriceRange();
$dbRanges = $r->getRangeItemCounts($range);
$data = array();

foreach ($dbRanges as $index=>$count) {
$data[] = array(
'label' => $this->_renderItemLabel($range, $index),
'value' => $index . ',' . $range,
'count' => $count,
);
}
foreach($data as $d){
$t=str_replace('<span class="price">', "", $d['label']);
$t=str_replace('</span>', "", $t);
$html .= '<li><a href="'.$category->getUrl().'?price='.$d['value']
.'">'.$t.'</a></li>';
}


return $html;
}

and call this funtion

$html=''
$html .= '
<h4>Price</h4>
<ul class="price_grid">';
$html .= $this->priceHtmlnav($maincategoryId);
$html .='</ul>';
echo $html;


To over the default issue i write custom code so even if cache is disabled the link will display



<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$storename='default';
function _renderItemLabel($range, $value)
{
$store = Mage::app()->getStore();
$fromPrice = $store->formatPrice(($value-1)*$range);
$toPrice = $store->formatPrice($value*$range);

return Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
}


$html = '<h4>Price</h4>
<ul class="price_grid">';


$layer = Mage::getModel('catalog/layer');
$category = Mage::getModel('catalog/category')->load(6);
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}


$r=Mage::getModel('catalog/layer_filter_price')
->setLayer($layer);

$range = $r->getPriceRange();
$dbRanges = $r->getRangeItemCounts($range);
$data = array();

foreach ($dbRanges as $index=>$count) {
$data[] = array(
'label' => _renderItemLabel($range, $index),
'value' => $index . ',' . $range,
'count' => $count,
);
}
foreach($data as $d){
$html .= '<li><a href="'.$category->getUrl().'?price='.$d['value']
.'">'.$d['label'].'</a></li>';
}
$html .='</ul>';

echo $html;



?>


You can check same post in my magento forum with file attached
http://www.magentocommerce.com/boards/viewthread/272901/

Monday, February 6, 2012

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4-0-1' for key 'PRIMARY', SQL error when updating products in Magento

Hi

When you edit products from back end

i\’e when updating products in admin you will get one sql error Integrity constraint violation: 1062 Duplicate or SQLSTATE[23000]

this error will happen if you install the module called simple organic for config products

so there is quick fix for this issue

Class: OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price
Method: public function catalogProductSave(Mage_Index_Model_Event $event)

file path is

app\code\community\OrganicInternet\SimpleConfigurableProducts\Catalog\Model\Resource\Eav\Mysql4\Product\Indexer\Price.php

Change:
$this->cloneIndexTable(true);
on line 58 to:
$this->clearTemporaryIndexTable();

Enjoy

Getting Associated products of bundle products or displaying simple products of bundle products

If you need to get Associated products sku of bundle products use below code


$product_id=7763;
$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load($product_id);
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);

$bundled_items = array();
foreach($selectionCollection as $option)
{
$bundled_items[] = Mage::getModel('catalog/product')->load($option->product_id)->getSku();
}

print_r($bundled_items);

Sunday, February 5, 2012

Reinstall or Re- install custom module in Magento

We can re-install the custom module in Magento

Delete the appropriate entry in the core_resource db table and delete the database table of your module.