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>

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

被折叠的 条评论
为什么被折叠?



