Show Pagination in Category Listing Page - magento

I have created a custom phtml file for categories in which i am listing child categories of parent category.
I want to show pagination tool bar in that category listing is there anyway to do that?

you can set collection in _preparelayout function as below code in your block file
protected function _prepareLayout()
{
parent::_prepareLayout();
$toolbar = $this->getLayout()->createBlock('page/html_pager');
if ($toolbar) {
$toolbar->setAvailableLimit(array(10=>10,20=>20,'all'=>'All'));
$toolbar->setCollection($this->getCollection());
$this->setChild('toolbar', $toolbar);
}
return $this;
}
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
and call in your phtml file by
$this->getToolbarHtml();

Related

eager loading multi level categories

i have a categories table with parent_id to store categories and subcategories. this categories can be multi level which means a category can have subcategories and each subcategory can have subcategories and so on. and it is dynamic so the levels number in not limited. i defined a function in Category model with name as below:
public function childs()
{
return $this->hasMany(Category::class, 'parent_id');
}
Now i want to eager load the categories with subcategories. the code
$cats = Category::with('childs')->get();
works greate but it gets just one level of subcategories and i want to eager load all levels. something like
$cats = Category:with('childs')->with('childs')... ->get();
is there any way to do that? to get all category levels?
if there isn't, how to get all levels of subcategories in one collection without eager loading?
You could use this to load all subcategories
class Category extends Model
{
public function children()
{
return $this->hasMany($this, 'parent_id');
}
public function childrenTree()
{
return $this->children()->with('childrenTree');
}
}
Just call your relation withing your relation. I mean recursively.
Try this code:
class Category extends Model
{
public function subCategory()
{
return $this->hasMany(Category::class, 'parent_id')->with('subCategory');
}
}

Magento 1.9 custom admin grid load inside itself

I have 2 grids in the same module (and i need to keep them in the same module).
When i click on the top of the column the grid load itself inside itself.
Below is my code:
Myname_Blink_Adminhtml_BlinkController
public function keywordsAction()
{
$this->loadLayout();
$this->_setActiveMenu('blink/keywords');
$this->_addContent($this->getLayout()->createBlock('Myname_Blink_Block_Adminhtml_Keywords_Grid'));
$this->renderLayout();
}
my block file : Myname_Blink_Block_Adminhtml_Keywords_Grid extends
class Myname_Blink_Block_Adminhtml_Keywords_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('keywords_grid');
$this->setDefaultSort('keywords_id');
$this->setDefaultDir('ASC');
//$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
As suggested to this post: Multiple grid in Magento admin
I removed the files:
=>Myname_Blink_Block_Adminhtml_Keywords
=>app\design\adminhtml\default\default\layout\myname\blink.xml
Maybe something goes wrong with AJAX call.
Did you try this one:
http://davemacaulay.com/fix-issue-with-magento-adminhtml-grid-ajax-call-containing-the-whole-page/
public function keywordsAction()
{
if($this->getRequest()->isXmlHttpRequest()) {
$this->getResponse()->setBody($this->getLayout()->createBlock('Myname_Blink_Block_Adminhtml_Keywords_Grid')->toHtml());
return $this;
}
$this->loadLayout();
$this->renderLayout();
}
Good luck!!!

Magento custom cache issue on product view page

We added the custom cache for product view page.
We rewrite the view.php file and write below code :
<?php
/**
* Rewriting Product View block
*/
class XXX_YYY_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
protected function _construct()
{
$this->addData(array(
//'cache_key' =>
//'cache_lifetime' =>
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()),
));
}
public function getCacheKey()
{
if (!$this->hasData('cache_key')) {
//$cacheKey = LAYOUTNAME_STORE+ID_PRODUCT+ID
$cacheKey = $this->getNameInLayout().'_STORE'.Mage::app()->getStore()->getId().'_PRODUCT'.$this->getProduct()->getId();
//.'_'.Mage::getDesign()->getPackageName().'_'.Mage::getDesign()->getTheme('template'). //_PACKAGE_THEME ?
$this->setCacheKey($cacheKey);
}
return $this->getData('cache_key');
}
public function getCacheLifetime()
{ //to prevent sub-blocks caching
if($this->getNameInLayout()!='product.info') return null;
//return false; //false creates default lifetime (7200)
return 9999999999;
}
}
?>
After the rewrite code added the add to cart functionality and global messages not working in product view page.
Help to fix this issue.
Thanks

how to define: Cartegory and subcategory in laravel 5.2

I have a database table like this:
I want to make a category and subcategory like this.
I have followed the steps in the link, but I face problem same as manojsharma20 on that page.
Subcategory also shows as category.
How do I avoid subcategories displaying as a category.
Here is my working example,
Suppose your Model name is Category
/*---------------------------------------------------------
* Relationship with same table, means recursive key
* --------------------------------------------------------
*/
//using this relation, you will get parent children
public function cat_childs(){
return $this->hasMany('App\Category', 'parent_id', 'id');
}
//using this relation, it will tell you, who is the parent of children
public function cat_parent(){
return $this->belongsTo('App\Category', 'parent_id', 'id');
}
so using this method you will get the parent and its children
$list = \App\Category::with('cat_childs')->all();
//this will show you parent and its children
echo "<pre>";
print_r($list);
echo "</pre>";
Suppose your Model name is Category
Create a function on Category model
public function childs()
{
return $this->hasMany('App\Category', 'parent_id', 'id');
}
using above method on your controller
$categories = Category::with('childs')->where('parent_id',0)->get();

Magento add pager toolbar to wishlist

Is it possible to use the catalog collection pager for the wishlist, and if so how can I implement this into the wishlist?
danny (OP) already self-answered the question.
Quote:
Ok, i've found the solution here but i'll post it here too for a better code highlighting:
Create a new modul and overwrite the wishlist block located in: code/core/Mage/Wishlist/Block/Customer/Wishlist.php
and add the following to your Wishlist.php
class Company_Wishlist_Block_Customer_Wishlist extends Mage_Wishlist_Block_Customer_Wishlist
{
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()
->createBlock('page/html_pager', 'wishlist.customer.pager')
->setCollection($this->getWishlist());
$this->setChild('pager', $pager);
$this->getWishlist()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
now add <?php echo $this->getPagerHtml(); ?> to the start and/or end of the view.phtml located in: app/design/frontend/default/your_theme/template/wishlist/view.phtml. that should do the trick.
Note: It's absolutely OK to self-answer your own question. Please just post it as an real answer, but not in a question or comment. Posting as real answer helps to keep the "Unanswered" list more clear (avoids making other people wasting their time).
you do not need to create a new module.just create (with folder) in your local : app\code\local\Mage\Wishlist\Block\Customer\Wishlist.php.and enter the following code on Wishlist.php
<?php class Mage_Wishlist_Block_Customer_Wishlist extends Mage_Wishlist_Block_Abstract {
/**
* Preparing global layout
*
* #return Mage_Wishlist_Block_Customer_Wishlist
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'wishlist.customer.pager');
$pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));
$pager->setCollection($this->getWishlist());
$this->setChild('pager', $pager);
$this->getWishlist()->load();
return $this;
}
/**
* Pager HTML
*
* #return HTML
*/
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
After that add following code in /app/design/frontend/base/default/template/wishlist/view.phtml
<?php echo $this->getPagerHtml(); ?>
after title div and after formkey in end of view.phtml
: image example
tested on Magento ver. 1.9.0.1

Resources