magento - 如何在 Magento 中为访客用户获取最近查看的产品

标签 magento

当我想为访客用户显示最近查看的产品时,我遇到一个问题,有什么方法可以为访客用户显示最近查看的产品,

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

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

希望我能得到一些答复。

提前致谢。

最佳答案

这里是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();
}

关于magento - 如何在 Magento 中为访客用户获取最近查看的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15682483/

相关文章:

magento - 如何在产品列表页面上显示产品详细说明?

php - Magento 连接错误 : Failed to open file

magento - 从 Magento 产品页面的自定义选项下拉列表中更改/删除 '--Please Select--' 消息

magento - 为什么我的 Magento 观察者陷入无限循环?

magento - Magento 中的哪些文件没有用于源代码管理?

php - Magento:当我尝试在主页上显示最畅销的产品时,$this->getAddToCartUrl($product) 不起作用

php - Magento upsell_products 引发错误 "must be an instance of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection"

javascript - 将外部 JavaScript 文件添加到 Magento

php - Magento 自定义订单属性/字段?搬起石头砸自己的脚?

magento - 从哪里开始使用 Magento?