1

I need to display a js carousel in frontend with last sold products in Magento 1.9

I build a widget, I found the above code, but this code displays only name and date, I need to found all parameters (url, thumbnail, reviews etc) so I can build the carousel.

protected function _toHtml() {


 $itemsCollection = Mage::getModel('sales/order_item')->getCollection()
->setOrder('created_at', Varien_Data_Collection::SORT_ORDER_DESC)
->addAttributeToSelect('*')
->setCurPage(1)->setPageSize(5);

foreach($itemsCollection as $item) {
echo $item->getName() . ' - ' . $item->getData('created_at') . '</br>';
}

}

Is posible with that code to get other parameters like images, links and other or need something else.

UPDATE:

I decide to display the widget with a custom template. I found that code but is not displaying the last sold but only the bestselling.

<?php

    $storeId    = Mage::app()->getStore()->getId();
    $products = Mage::getResourceModel('reports/product_collection')
        ->setOrder('created_at', Varien_Data_Collection::SORT_ORDER_DESC)
        ->addOrderedQty()
        ->addAttributeToSelect('*')
        ->addAttributeToSelect(array('name', 'price', 'small_image'))
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount();
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

    $products->setPageSize(5)->setCurPage(1);
    $this->setProductCollection($products);

?>
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
 <div class=" most_viewed">
 <?php $_collectionSize = 5; ?>
 <ul class="products-grid" id="products-grid-table">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
<li id="td_<?php echo $i;?>" <?php if($i%5==0 or $i==$_collectionSize){echo 'class="last"';} ?> >
    <div id="cont_<?php echo $i;?>">        
        <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
        </a>
        <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
        <div class="a-center">                        
            <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
            <?php endif; ?>
            <?php echo $this->getPriceHtml($_product, true) ?>
            <?php if($_product->isSaleable()): ?>
                <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
                <div class="clear"></div>
            <?php else: ?>
                <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                <div class="clear"></div>
            <?php endif; ?>
            <ul class="add-to-links">
                <?php if ($this->helper('wishlist')->isAllow()) : ?>
                <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                <?php endif; ?>
                <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</li>
<?php $i++; endforeach; $kol = $_collectionSize; ?>
</ul>
</div>
<?php endif; ?>

I used

  ->setOrder('created_at', Varien_Data_Collection::SORT_ORDER_DESC)
  • add $item->getSku() in foreach loop of $itemCollection and load Mage::getModel('catalog/product')->loadByAttribute('sku',$item->getSku()); – sandip Jan 21 '17 at 2:29
2

I found myself the solution, this code works for me.

 <?php

 $storeID = Mage::app()->getStore()->getId();
 $itemsCollection = Mage::getResourceModel('sales/order_item_collection')
->join('order', 'order_id=entity_id')
->addFieldToFilter('main_table.store_id', array('eq'=>$storeID))
->setOrder('main_table.created_at','desc')
->setPageSize(12);
$itemsCollection->getSelect()->group(`main_table`.'product_id');
$products = array();
if(sizeof($itemsCollection)>0)
{
foreach ($itemsCollection as $item) {
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($item->getProductId());

?>

 <a class="product-image" href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>">
 <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(122, 109); ?>" width="122" height="109" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" /></a>
 <?php echo $this->getReviewsSummaryHtml($product, 'short') ?>

  <?php echo $this->htmlEscape($product->getName()) ?>
  <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>

  <?php
  }
  }?>
| improve this answer | |
1
$products = array();    
foreach($itemsCollection as $item) {
echo $item->getName() . ' - ' . $item->getData('created_at') . '</br>';
$products[] = Mage::getModel('catalog/product')->loadByAttribute('sku',$it‌​em->getSku());

}

use your $products array in carousal.

| improve this answer | |
  • Not works, its broke the page. – George George Jan 21 '17 at 10:18
  • @GeorgeGeorge check now.. edit $products = array(); – sandip Jan 21 '17 at 10:35
  • Still the same. – George George Jan 21 '17 at 11:00
  • I updated my question, check please. – George George Jan 21 '17 at 12:10
  • its report collection it will get only best selling as it is – sandip Jan 21 '17 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.