Magento 自定义块

发布于 2024-09-28 23:22:31 字数 1255 浏览 2 评论 0原文

我正在尝试使用 ajax 在 magento 的主页上使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是与结果集一起添加的分页工具栏。

这是我添加的用于显示热门产品的内容,

// 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'));

?>

因此这给了我指定页面和限制的热门产品,但我无法加载分页工具栏(通过直接将工具栏添加到popular.phtml 或者通过创建块布局功能),哪里错了?有人可以告诉我吗?

谢谢

I am trying to show the popular product list using ajax in magento on the home page using ajax, I could do that for 5 or "N" no.of products, but what i want is the pagination toolbar to be added with the result set.

This is what i added to show the popular products,

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

And under 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'));

?>

So this gives me the popular products of specified page and limit, but i could not load the pagination toolbar(by directly adding the toolbar to popular.phtml or through create block layout function), Where am wrong? Could anybody tell me please.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许一世地老天荒 2024-10-05 23:22:31

尝试创建一个 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');

产品列表块始终初始化工具栏块本身。
您可以使用 getToolbarHtml() ?> 在模板中显示工具栏

编辑:
这是 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();
}

我希望这能让它更清楚。

Try creating a Mage_Catalog_Block_Product_List block and setting the collection of the popular products yourself.

$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');

The product list block always initializes a toolbar block itself.
You can display the toolbar in the template by using <?php echo $this->getToolbarHtml() ?>

EDIT:
Here is a working example of a sample frontend action in 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();
}

I hope that makes it clearer.

糖果控 2024-10-05 23:22:31

对于其他人参考,这是我根据 Vinai 的代码添加的内容。

$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();

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

For others reference this is what i added as per Vinai's code.

$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();

It renders the pagination toolbar perfectly.

小ぇ时光︴ 2024-10-05 23:22:31

我猜你应该从你的集合中初始化工具栏。您看过此页面吗?

You should initialize the toolbar from your collection I guess. Have you seen this page?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文