Add sort by Saleability And Use Custom paging number override core product list

本文深入探讨了使用PHP构建高级产品集合和自定义分页功能的技术,包括产品集合的获取、排序、分页实现及自定义限制设置,以及为特定页面定制的产品列表展示方式。

1 . create a collection.php to get data

 

<?php
class Bysoft_Mycatalog_Block_Apparel_Collection extends Mage_Core_Block_Template
{

	public function __construct()
	{
		parent::__construct();
		$collection = Mage::getModel('catalog/product')->getCollection();
		$this->setCollection($collection);
		if ($this->getCurrentOrder()) {
			if($this->getCurrentOrder() == 'saleability') {
				$this->getCollection()->getSelect()->
				joinLeft('sales_flat_order_item AS sfoi',
						'e.entity_id = sfoi.product_id',
						'SUM(sfoi.qty_ordered) AS ordered_qty')->
						group('e.entity_id')->order('ordered_qty ' . $this->getCurrentDirectionReverse());
				}
				else {
				$this->getCollection()->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
				}
		}
		return $this;
	}

	protected function _prepareLayout()
	{
		parent::_prepareLayout();

		$toolbar = $this->getToolbarBlock();

		// called prepare sortable parameters
		$collection = $this->getCollection();

		// use sortable parameters
		if ($orders = $this->getAvailableOrders()) {
			$toolbar->setAvailableOrders($orders);
		}
		if ($sort = $this->getSortBy()) {
			$toolbar->setDefaultOrder($sort);
		}
		if ($dir = $this->getDefaultDirection()) {
			$toolbar->setDefaultDirection($dir);
		}
		$toolbar->setCollection($collection);

		$this->setChild('toolbar', $toolbar);
		$this->getCollection()->load();
		return $this;
	}
	public function getDefaultDirection(){
		return 'asc';
	}
	public function getAvailableOrders(){
		return array('created_time'=> 'Created Time','price'=>'Price','salesablity'=>'Saleability');
	}
	public function getSortBy(){
		return 'collection_id';
	}
	public function getToolbarBlock()
	{
		$block = $this->getLayout()->createBlock('mycatalog/apparel_toolbar', microtime());
		return $block;
	}
	public function getMode()
	{
		return $this->getChild('toolbar')->getCurrentMode();
	}

	public function getToolbarHtml()
	{
		return $this->getChildHtml('toolbar');
	}
}

 2.  to override toolbar class , set custom limit per page

<?php
class Bysoft_Mycatalog_Block_Apparel_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar{
	public function getPagerHtml()
	{
		$pagerBlock = $this->getLayout()->createBlock('page/html_pager');

		if ($pagerBlock instanceof Varien_Object) {

			/* @var $pagerBlock Mage_Page_Block_Html_Pager */
			$pagerBlock->setAvailableLimit($this->getAvailableLimit());
			
			$pagerBlock->setUseContainer(false)
			->setShowPerPage(false)
			->setShowAmounts(false)
			->setLimitVarName($this->getLimitVarName())
			->setPageVarName($this->getPageVarName())
			->setCollection($this->getCollection());
			return $pagerBlock->toHtml();
		}
		return '';
	}
	
	protected function _getAvailableLimit($mode='')
	{
		return array(36=>36,72=>72,108=>108,'all'=>'all');
	}
}

 3. for custom list phtml page

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div class="page-title">
    <h1><?php echo $this->__('My Collection') ?></h1>
</div>
<?php echo $this->getToolbarHtml(); ?>
<?php if($collection->getSize()): ?>
<?php if(1 || $this->getMode() == 'list'){ ?>
<div class="product-list-div">
<ul class="list-4-column-ul column-1 clearfix">
<?php $i = 0;?>
	<?php foreach ($collection as $_product):?>
	<?php ++$i;?>
	<?php $_product = Mage::getModel('catalog/product')->load($_product->getId());?>
	<?php  $prod_img_src = (string)Mage::helper('catalog/image')->init($_product, 'image');?>
	<?php $lazy_load_img = $this->getSkinUrl('images/lazy-load.gif');?>
		<li>
		<div class="prod-img">
			<a href="<?php echo $_product->getProductUrl() ;?>" >
				<img class="lazy"  data-original="<?php echo $prod_img_src;?>"/>
			</a>
		</div>
		
		<div class="prod-info  pc-info">
			<div class="layer_info">
				<p class="product-manufacturer"><?php echo  $_product->getAttributeText('manufacturer');?></p>
				<p class="product-name"><?php echo $_product->getData('name');?></p>
			</div>
		</div>
		
		</li>
		
		<?php if ($i%4 == 0 ):?>
		</ul>
		<ul class="list-4-column-ul column-<?php echo ($i/4)+1 ?> clearfix" >
		<?php endif;?>
		
	<?php endforeach;?>
</ul>
</div>
<script type="text/javascript">decorateTable('my-custom-table');</script>
<?php }else{ ?>
	<!-- List Mode HTML Here -->
<?php } ?>
<?php echo $this->getToolbarHtml(); ?>
<?php else: ?>
    <p><?php echo $this->__('The collection is empty.'); ?></p>
<?php endif ?>


<script>
jQuery(document).ready(function($){
	jQuery.extend(jQuery.browser,{Mobile : navigator.userAgent.toLowerCase().match(/mobile/i)});
    

		if($(window).width()<768){
			$(".product-list-div").carouFredSel({
				items: 1,
				prev: "#prev",
				next:"#next",
				auto: false
			});
			//alert($(".product-list-div ul").width());
			$("img.lazy").lazyload({
				threshold:0,
				event:'click'
			});
			var $src = $('.product-list-div ul.column-1 img').attr('data-original');
			$('.product-list-div ul.column-1 img').attr('src',$src);
			
			$('#prev').click(function(){
				var $img = $('.product-list-div ul:first').prev().find('img');
				$img.each(function(){//triggerHandler()方法不会触发事件的默认行为,但只会影响第一个匹配的元素
					if($(this).attr('src')!= $(this).attr('data-original')){
						$(this).triggerHandler( "click" ); 
					}
				})
			})
			$('#next').click(function(){
				var $img = $('.product-list-div ul:first').next().find('img');
				$img.each(function(){
					if($(this).attr('src')!= $(this).attr('data-original')){
						$(this).triggerHandler( "click" );
					}
				})
				
			})
		}else{
			$("img.lazy").lazyload({
				threshold:-50
			});
		}
	
})
</script>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值