Friday, November 23, 2012
Create custom pagination in magento
Hope you known about block, xml in Magento.
Here is an extracted code that would help you to create pagination with custom collection in magento.
>> First paste the following code in your block class (e.g. product.php)
<?php
class example extends parentclass
{
private $_itemPerPage = 2;
private $_pageFrame = 8;
private $_curPage = 1;
public function getCollection($collection = 'null')
{
if($collection != 'null'){
$page = $this->getRequest()->getParam('p');
if($page) $this->_curPage = $page;
$collection->setCurPage($this->_curPage);
$collection->setPageSize($this->_itemPerPage);
return $collection;
}
}
public function getPagerHtml($collection = 'null')
{
$html = false;
if($collection == 'null') return;
if($collection->count() > $this->_itemPerPage)
{
$curPage = $this->getRequest()->getParam('p');
$pager = (int)($collection->count() / $this->_itemPerPage);
$count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
$url = $this->getPagerUrl();
$start = 1;
$end = $this->_pageFrame;
$html .= '<ol>';
if(isset($curPage) && $curPage != 1){
$start = $curPage - 1;
$end = $start + $this->_pageFrame;
}else{
$end = $start + $this->_pageFrame;
}
if($end > $count){
$start = $count - ($this->_pageFrame-1);
}else{
$count = $end-1;
}
for($i = $start; $i<=$count; $i++)
{
if($i >= 1){
if($curPage){
$html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}else{
$html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}
}
}
$html .= '</ol>';
}
return $html;
}
public function getPagerUrl() // You need to change this function as per your url.
{
$cur_url = mage::helper('core/url')->getCurrentUrl();
$new_url = preg_replace('/\&p=.*/', '', $cur_url);
return $new_url;
}
}
?>
>> Now you have to modify your frontend phtml page. (e.g. custom.phtml)
<?php
$_collections = $this->getProductCollection(); // Default Collection on which you'll implement pagination.
$_productCollection = $this->getCollection($_collections); // calling the function that have been created in block page.
foreach ($_productCollection as $_product):
------------------ Your code here ------------
endforeach;
?>
<?php if($this->getPagerHtml($this->getProductCollection())):?> // pass the default collection as parameter
<div class="pages">
<span><?php echo $this->__('Page : ');?></span>
<?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
Friday, November 9, 2012
get product quantity/qty in Magento
>> $_product is your product object. = Mage::getModel('catalog/product')->load('product_id');
<?php
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$qty = $stock->getQty();
print_r( $stock->getData()); // To get other stock information.
?>
Thursday, November 8, 2012
How to redirect in magento
>> Pass the url in this function :
public function redirect($url)
{
return Mage::app()->getFrontController()
->getResponse()
->setRedirect($url);
}
Tuesday, November 6, 2012
Change default sort by order for one category in Magento
>> If you want to change default sort by value "position" to "name", change admin category setting. see the screen-shot.
you can change default direction by providing the following code in Custom Layout Update section in admin category "Custom Design" Tab
<reference name="product_list_toolbar">
<action method="setDefaultDirection"><dir>desc</dir></action>
</reference>
>> Add "created_at" in sort by dropdown.
update eav_attribute set frontend_label = "Date" where attribute_code = "created_at" AND entity_type_id = 4; // Ensure about the entity_type_id
update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id = (select attribute_id from eav_attribute where attribute_code = "created_at" AND entity_type_id = 4);
Now You can see the Date in admin default sort by configuration.
Admin=>System=>Configuration=>Catalog=>Frontend=> change value here "Product Listing Sort by"
>> If you want to add another attribute, go to the attribute setting and set "yes" for use for default sort by, see the screen-shot.
Admin=>Attributes=>Manage Attributes
Thursday, October 25, 2012
add custom product view page for custom product type in magento
>> Suppose you've created a new Product Type "custom". I've posted how to add new product type in magento. You want to use a different template for those product. follow the instruction ::
You can directly update in the catalog.xml or you can create a custom module by using Module Creator.
<layout>
<PRODUCT_TYPE_custom>
<reference name="product.info">
<action method="setTemplate">
<template>path/to/your/view.phtml</template>
</action>
</reference>
</PRODUCT_TYPE_custom>
</layout>
How to add custom product type in Magento
>> Basically You can't create a product type from Admin. You need to modify config.xml and sql in your module.
config.xml
<global>
<catalog>
<product>
<type>
<classified translate="label" module="vendor">
<label>Classified</label>
<model>vendor/product_type_classified</model>
<composite>0</composite>
<index_priority>50</index_priority>
<is_qty>1</is_qty>
</classified>
</type>
</product>
</catalog>
</global>
Here classified is my custom product type and vendor is my custom module.
Now you need to update price type attributes for new type : To run your sql file of your module for next time you need to delete the row created for the module "vendor" in core_resource tabl
e. We will discuss later for update Sql.
<?php
$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$fieldList = array(
'price',
'special_price',
'special_from_date',
'special_to_date',
'minimal_price',
'cost',
'tier_price',
'weight',
'tax_class_id'
);
// make these attributes applicable to downloadable products
foreach ($fieldList as $field) {
$applyTo = split(',', $setup->getAttribute('catalog_product', $field, 'apply_to'));
if (!in_array('classified', $applyTo)) {
$applyTo[] = 'classified';
$setup->updateAttribute('catalog_product', $field, 'apply_to', join(',', $applyTo));
}
}
$installer->endSetup();
?>
Wednesday, October 17, 2012
How to get attribute set name in Magento
>> $prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
>> To get the attributeset name in cart page see the following..
app/design/frontend/base/default/template/checkout/cart/item/default.phtml (or in your theme folder.)
Here you can see the cart item object $_item = $this->getItem(); at line 28. Then you have to load the product with product id.
<?php
$_product = Mage::getModel('catalog/product')->load($_item->getProductId());
$prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
?>
Subscribe to:
Comments (Atom)