如何通过客户ID获取最近查看的产品?


8

我想通过SOAP WS公开客户最近查看的项目。

我如何到达那些物品?我知道它们存储在“ reports / product_index_viewed”中;但是,我不知道哪种方法是实现这些目标的正确方法。

这是到目前为止我得到的:

public function getRecentlyViewedByCustomer($customerId)
{
    Mage::log(__METHOD__);
    $customer = $this->_getCustomer($customerId);
    Mage::log('Getting recently viewed products of '. $customer->getName() .' ('. $customer->getEmail() .'), ID: ' . $customer->getId() );

    $productCollection = Mage::getResourceModel('reports/product_index_viewed');

    Mage::log(print_r($productCollection, true));

    return __METHOD__;
}

public function _getCustomer($customerId)
{
    $customer = Mage::getModel('customer/customer')->load($customerId);
    return $customer;
}

Answers:


0
public function getMostViewedProducts()
{       
    /**
     * Number of products to display
     * You may change it to your desired value
     */
    $productCount = 5; 

    /**
     * Get Store ID
     */
    $storeId    = Mage::app()->getStore()->getId();       

    /**
     * Get most viewed product collection
     */
    $products = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')     
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->setPageSize($productCount); 

    Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);

    return $products; 
}

抱歉,这对我不起作用。另外,在api中,如何获取与customerId相关联的storeId?
拉美西斯2015年

1
此收藏集将返回商店而不是顾客的已查看产品。
paj 2015年

代码不起作用
Gem

0

您应该添加一个观察者巫婆,以检测到用户正在查看产品,并返回产品ID和客户ID并将其存储在数据库中,以便可以使用它


0

这就是我最终解决这个问题的方法

public function getRecentlyViewedByCustomer($customerId, $categoryId, $limit = 5){
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

    $q = "SELECT DISTINCT report_viewed_product_index.product_id, report_viewed_product_index.added_at "  .
    " FROM report_viewed_product_index " .
    " INNER JOIN catalog_category_product ON catalog_category_product.product_id = report_viewed_product_index.product_id " .
    " WHERE customer_id = " . $customerId;

    if($categoryId > 0){
        $categories = $this->_getCategories($categoryId);
        $q = $q . " AND category_id in (" . $categories . ")";
    }

    $q = $q . " ORDER BY added_at desc LIMIT " . $limit;

    return $readConnection->fetchAll($q);
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.