Magento最近浏览过产品

时间:2016-03-16 19:38:20

标签: php mysql magento product mage

我尝试过的。只是获取被查看的产品并显示它,结果我什么都没有:

 <?php $_recentlyViewed =
 Mage::getSingleton('Mage_Reports_Block_Product_Viewed')-> getItemsCollection(); ?>

     <?php foreach ($_recentlyViewed as $_recentlyProduct): ?>
         <?php var_dump($_recentlyProduct); ?>
     <?php endforeach; ?>

接下来,它试图显示默认块,然后我什么也没得到。在local.xml中我添加了:

<?xml version="1.0"?>
<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <reference name="product.info"> 
                <block type="reports/product_viewed" name="product.recently.viewed" as="product_recently_viewed" template="reports/product_viewed.phtml"/> 
                <block type="poll/activePoll" name="right.poll" after="product_recently_viewed">
                    <action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
                    <action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
                </block>
           </reference>
    </reference>
</catalog_product_view>

并且

<?php echo $this->getChildHtml('product_recently_viewed'); ?>

在view.phtml中,但我一无所获。 我检查了mysql中的表,有关于最后查看产品的记录。所以我的问题是,是什么让我错了?有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您已经使用过getSingleton尝试:

 <?php $_recentlyViewed =
 Mage::getModel('Mage_Reports_Block_Product_Viewed')-> getItemsCollection(); ?>

 <?php foreach ($_recentlyViewed as $_recentlyProduct): ?>
     <?php var_dump($_recentlyProduct); ?>
 <?php endforeach; ?>

通过使用getModel,我认为您的工作可以实现

答案 1 :(得分:0)

在local.xml文件中尝试使用此xml代码:

<?xml version="1.0"?>
<layout version="0.1.0">
    <catalog_product_view>
        <reference name="product.info">
            <block type="reports/product_viewed" name="product.recently.viewed" as="product_recently_viewed" template="reports/product_viewed.phtml"/>
            <block type="poll/activePoll" name="right.poll" after="product_recently_viewed">
                <action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
                <action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
            </block>
        </reference>
    </catalog_product_view>
</layout>

这是因为您不需要嵌套引用来在模板中注入某些东西。如果这不起作用,那么唯一的原因是你在catalog.xml文件中没有product.info句柄,因为大多数主题都是自定义的。您需要检查主题的catalog.xml内部。

对于您显示的PHP代码,不会以这种方式调用块。 getSingleton方法保留用于以单例方式加载模型。但是,你用完全限定的类名传递它的方式,你很可能会获得该块的对象。请记住以备将来参考。只要需要,就可以通过Magento的布局模型实例化块。它们应该以这种方式使用。

相关问题