代码之家  ›  专栏  ›  技术社区  ›  Elamurugan

Magento自定义块

  •  7
  • Elamurugan  · 技术社区  · 15 年前

    这是我为展示流行产品而添加的,

    // Magento layout
    $magento_block = Mage::getSingleton('core/layout');
    $productsHtml = $magento_block->createBlock('catalog/product');
    $productsHtml->setTemplate('catalog/product/popular.phtml');
    echo $productsHtml ->toHTML();
    

    在popular.phtml下

    <?php   
    
        $_productCollection = Mage::getModel('catalog/product')->getCollection()
        ->addPriceData()
        ->addAttributeToSort('ordered_qty', 'DESC')
        ->addAttributeToSort('name', 'ASC')
        ->setPageSize($limit)
        ->setPage($p, $limit)       
        ->addAttributeToSelect(array('entity_id', 'entity_type_id', 'attribute_set_id', 'type_id', 'sku', 'category_ids', 'created_at', 'updated_at','has_options', 'sync', 'name', 'stock_status', 'wc_review_iwc_rating', 'wc_review_wa_rating', 'wc_review_bh_rating', 'small_image', 'status', 'pre_arrival', 'description', 'short_description', 'price', 'is_salable', 'stock_item', 'gift_message_available', 'featured'));
    
    ?>
    

    流行.phtml 或者通过创建块布局功能),哪里错了?有人能告诉我吗。

    3 回复  |  直到 15 年前
        1
  •  10
  •   Vinai    15 年前

    尝试创建一个Mage_Catalog_Block_Product_List块,并自己设置流行产品的集合。

    $collection = Mage::getModel('catalog/product')->addAttributeToFilter('your popular products');
    // do not load the collection yet, otherwise the toolbar will not work
    
    $listBlock = Mage::getSingleton('core/layout')->createBlock('catalog/product_list');
    $listBlock->setCollection($collection)->setTemplate('your/alternative/catalog/product/list.phtml');
    

    产品列表块总是初始化工具栏块本身。

    编辑: 下面是Magento 1.4.1.1中示例前端操作的工作示例:

    public function productListAction()
    {
    
        $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*');
    
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
    
        $this->loadLayout();
    
        $listBlock = $this->getLayout()->createBlock('catalog/product_list')
                ->setTemplate('catalog/product/list.phtml')
                ->setCollection($collection);
    
        $this->getLayout()->getBlock('content')->append($listBlock);
    
        $this->renderLayout();
    }
    

        2
  •  4
  •   Elamurugan    15 年前

    对于其他参考,这是我根据维奈的代码添加的。

    $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);       
        $magento_block = Mage::getSingleton('core/layout');
        $productsHtml  = $magento_block->createBlock('catalog/product_list');
        $productsHtml ->setTemplate('catalog/product/list.phtml')->setCollection($collection);
        echo $productsHtml ->toHTML();
    

    它完美地呈现了分页工具栏。

        3
  •  2
  •   greg0ire    15 年前

    我想你应该从集合中初始化工具栏。你见过吗 this page ?

    推荐文章