【问题标题】:Magento recently viewed productsMagento 最近查看的产品
【发布时间】:2016-03-16 19:38:19
【问题描述】:

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

 <?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中的表,有关于上次查看产品的记录。所以我的问题是,是什么让我错了?有什么想法吗?

【问题讨论】:

  • getSingleton 用于调用模型类,您是否尝试将其用于块?
  • @Prateek 我用作 getSingleton 方法,用作 getBlockSingleton。没有什么好用的。
  • 您是否在系统> 配置、目录> 最近查看过的产品中设置为显示1+?
  • @PixieMedia 为当前网站设置了 5。太奇怪了,我今天必须完成它:(
  • 尝试在viewed_products的开头if语句上方添加任何内容,看看它是否至少加载了模板

标签: php mysql magento product mage


【解决方案1】:

你已经使用 getSingleton 尝试使用这个:

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

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

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

【讨论】:

    【解决方案2】:

    尝试在你的 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 的布局模型实例化。应该以这种方式使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多