【问题标题】:How to get Recently View Product For Guest User In Magento如何在 Magento 中为访客用户获取最近查看的产品
【发布时间】:2026-02-13 13:20:02
【问题描述】:

当我想为访客用户显示最近的产品时,我面临一个问题,有没有办法为访客用户显示最近查看的产品,

Magento 支持注册用户最近查看产品但访客用户如何显示该特定访客最近查看的产品...

我正在等待您的友好回复,

希望我能得到一些答复。

提前致谢。

【问题讨论】:

  • 我认为您可以更改核心文件...您尝试过吗??
  • 是的。我也需要这个功能。
  • 哦,感谢您的友好回复,我做到了,但 magento 不支持最近查看访客用户的产品

标签: magento


【解决方案1】:

这里是phtml

<?php if ($_products = $this->getRecentlyViewedProducts()):
$ids = '';
foreach ($_products as $_item) {
    $ids .= $_item->getId() . ';';
}
?>
<div class="lftHeading">
<span
        style="text-transform:capitalize;background:url(<?php echo $this->getSkinUrl('css/images/clo_left_heading_bullet2.gif') ?>) top left no-repeat;"
        >recently viewed</span>
</div>
<div class="innerRgtMenu recently_viewed_block">
<table id="recently-viewed-items">
    <?php $i = 0; foreach ($_products as $_item): if ($i == 3) {
    continue;
} ?>
    <?php $product = $_item ?>
    <tr>
        <td><a style="border:1px solid #DDDDDD;float:left;margin:5px;padding:5px;"
               href="<?php echo $this->getProductUrl($_item, array('_nosid' => true)) ?>" class="product-image"><img
                src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(50) ?>"
                width="50" alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a></td>
        <td><a style="position:relative;top:3px;font-size:11px;"
               href="<?php echo $this->getProductUrl($_item, array('_nosid' => true)) ?>"><?php echo $this->escapeHtml($_item->getName()) ?></a>
        </td>

    </tr>
    <?php $i++;
endforeach; ?>
</table>
<div style="margin: 5px 0px 5px 2px; text-align: center; width: 140px;">
    <input type="button" class="button recently_viewed_btn" value="<?php echo $this->__('Email These To Me') ?> "
           onClick="email_recently('<?php echo $ids; ?>')"/>
</div>
<div style="margin:5px;">
    <a href="<?php echo $this->getBaseUrl() ?>recently-viewed-items/"><?php echo $this->__('See All Recently Viewed') ?></a>
</div>
<script type="text/javascript">decorateList('recently-viewed-items');</script>

和php文件

class Mage_Reports_Block_Product_Viewed extends Mage_Reports_Block_Product_Abstract
{
const XML_PATH_RECENTLY_VIEWED_COUNT    = 'catalog/recently_products/viewed_count';

/**
 * Viewed Product Index model name
 *
 * @var string
 */
protected $_indexName       = 'reports/product_index_viewed';

/**
 * Retrieve page size (count)
 *
 * @return int
 */
public function getPageSize()
{
    if ($this->hasData('page_size')) {
        return $this->getData('page_size');
    }
    return Mage::getStoreConfig(self::XML_PATH_RECENTLY_VIEWED_COUNT);
}

/**
 * Added predefined ids support
 */
public function getCount()
{
    $ids = $this->getProductIds();
    if (!empty($ids)) {
        return count($ids);
    }
    return parent::getCount();
}

/**
 * Prepare to html
 * check has viewed products
 *
 * @return string
 */
protected function _toHtml()
{
    if (!$this->getCount()) {
        return '';
    }
    $this->setRecentlyViewedProducts($this->getItemsCollection());
    return parent::_toHtml();
}
}

如果它对客人不起作用 - 尝试将 php 文件中的最后一个函数更改为

   protected function _toHtml()
{
 /*   if ($this->_hasViewedProductsBefore() === false) {
        return '';
    } */

    $this->setDisplayMinimalPrice('1');
    $collection = $this->_getRecentProductsCollection();

    $hasProducts = (bool)count($collection);
//        if (is_null($this->_hasViewedProductsBefore())) {
//           Mage::getSingleton('reports/session')->setData('viewed_products', $hasProducts);
//        }
    if ($hasProducts) {
        $this->setRecentlyViewedProducts($collection);
    }

    return parent::_toHtml();
}

【讨论】:

  • 感谢您的友好回复,这对我很有帮助,但是一旦我关闭浏览器并再次返回,它就不会显示最近查看的产品,有什么选择吗!
  • 这些产品保存在会话中,所以可能只是会话过期
  • 是的,我做到了,它的工作,但我想向访客用户展示最近查看的产品,就像注册用户一样!您是否观察到注册用户关闭浏览器并在那时返回时会显示他上次访问的最近产品!所以我希望访客用户也一样,所以一旦他们关闭浏览器并再次回到我的网站,他将获得他最近查看的产品。现在我想你知道了
  • 我从一开始就有一个想法。但在这个网站上buyarearugs.com/recently-viewed-items 默认情况下这适用于客人。所以可能你的问题是基于会话生命周期。我没有看到任何其他原因,抱歉。
  • 没关系,我的朋友,但你的解决方案对我有用,我在 cookie 中为访客用户设置了最近查看产品,因此它将向他们显示最近查看的产品。谢谢你的好意
【解决方案2】:

阻止最近查看的产品在 magento 1.6-1.9.2.2 中无需任何代码修改即可正常工作
如果没有显示块,您需要检查:

  1. 块已正确添加到可见容器中的页面(默认情况下块添加到右侧边栏)
  2. 日志已启用。检查系统->配置->系统->日志选项“启用日志”=是
  3. 重建索引“类别产品”(catalog_category_product)

【讨论】:

    【解决方案3】:

    据我所知 - 它应该对客人很好。至少它适用于我的网站 这是我将其放在页面上的方式:

    <block type="reports/product_viewed" name="reports.product.recently.viewed" template="reports/recently_viewed.phtml" />
    

    【讨论】:

    • 您好,我的朋友,我认为 magento 1702 中没有 recenlty_viewed.phtml 文件,请您在此处复制/粘贴您的 recent_viewed.phtml 文件,以便我了解您在网站中的设置方式。