Thursday, September 17, 2015

Magento - how to list most viewed product

$storeId = Mage::app()->getStore()->getId();     
// Get most viewed product collection
$products = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')    
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addAttributeToSelect(array('name', 'price', 'minimal_price', 'small_image'))
    ->addViewsCount()
    ->setPageSize(6);

Mage::getSingleton('catalog/product_status')
        ->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')
        ->addVisibleInCatalogFilterToCollection($products);
       
$mostvieweddata= $products->getData();     // most view product   

Tuesday, September 15, 2015

Magento-How to Get Related Products by product Id

<?php
$model = Mage::getModel('catalog/product');
$product = $model->load($product_id);

// Get all related product ids of $product.
$allRelatedProductIds = $product->getRelatedProductIds();

foreach ($allRelatedProductIds as $id) {
            $relatedProduct = $model->load($id);

            // get Product's name
            echo $relatedProduct->getName();

            // get product's short description
            echo $relatedProduct->getShortDescription();

            // get Product's Long Description
            echo $relatedProduct->getDescription();

            // get Product's Regular Price
            echo $relatedProduct->getPrice();

            // get Product's Special price
            echo $relatedProduct->getSpecialPrice();

            // get Product's Url
            echo $relatedProduct->getProductUrl();

            // get Product's image Url
            echo $relatedProduct->getImageUrl();

        }
?>