我有一家Magento商店,需要有两页,这些页面既显示了添加到商店的新产品,又要享受折扣。两者都显示同一问题,但我会为您提供后者的代码,因为我认为这是最有问题的。

问题是我可以成功地显示显示标准的产品列表,但我不能像其他类别一样使工具栏出现。另外,每当我从那里单击产品时,我都会在产品的详细信息页面上丢失侧边栏,好像我设置的XML规则不会适用于该页面,因为我是从上述促销页面开始的。

这是我用来获取具有促销活动的产品集合的类:

class Mage_Catalog_Block_Product_Promo extends Mage_Catalog_Block_Product_List{
protected $_productsCount = null;

const DEFAULT_PRODUCTS_COUNT = 5;

protected function _beforeToHtml()
{
    $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

    $collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
        ->addAttributeToFilter('special_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToSort('flash', 'desc')
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1)
    ;
    $this->setProductCollection($collection);

    $toolbar = $this->getToolbarBlock();
    $toolbar->setCollection($collection);
    $this->setChild('toolbar', $toolbar); 

    return parent::_beforeToHtml();
}

 public function setCollection($collection)
{
    $this->_productCollection = $collection;
    return $this;
}

public function setProductsCount($count)
{
    $this->_productsCount = $count;
    return $this;
}

public function getProductsCount()
{
    if (null === $this->_productsCount) {
        $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
    }
    return $this->_productsCount;
}
} 

这是我的PHTML,我可以在这里获得所有产品:

<?php $current_cat= Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId(); ?>

<?php 
$_productCollection = $this->getProductCollection();
$_helper = $this->helper('catalog/output');
$_collectionSize = $_productCollection->count() ?>
<div class="promos_new_list">
    <div class="countcat">
        <?php //$_productCollection = $this->getLoadedProductCollection(); 
        $count = $_productCollection->getSize(); 
        echo $count; ?> Produits 
    </div>
<?php echo $this->getToolbarHtml(); ?>
<?php $_columnCount = 4; ?>
<?php $i=0; 
foreach ($_productCollection as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ul class="products-grid">
    <?php endif ?>
        <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
            <?php include('view/labels.phtml')  ?>                
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(178,108); ?>" width="178" height="108" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <div id="productimgover<?php echo $_product->getId()?>" style="display: none;"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(64); ?>" width="64" height="64" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></div>
            <div class="moreinfo">
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <div id='productname<?php echo $_product->getId()?>' style='display:none'><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></div>
                <p class="product-desc">
                    <?$desc=$this->htmlEscape($_product->getShortDescription());?>
                    <?php echo Mage::helper('function')->unalinea($desc,30);?>
                </p>
                <?php echo $this->getPriceHtml($_product, true) ?>

                <div class="nowmore orangegradient">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">En savoir plus </a><span></span>
                </div>

                <div class="clear"></div>

            </div>
        </li>
    <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
    </ul>
    <?php endif ?>
<?php endforeach ?>
</div>

我不知道是否需要更多代码。一位同事说,当我这样做时,Magento没有正常工作,因为它没有活跃的类别,但我真的不明白为什么?你能在这里引导我吗?

有帮助吗?

解决方案

到目前为止,我已经使用以下方式躲过了工具栏问题:

<?php
$toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
$toolbar->setCollection($_productCollection);
echo $toolbar->toHtml();
?>

我不确定这是否是处理情况的正确方法,但现在对我有用。

许可以下: CC-BY-SA归因
scroll top