Here is what I ended up with, which is very simple and maybe also very inefficient coding wise, but it works:
1st off I fetched the items in the Cart:
$session= Mage::getSingleton('checkout/session');
$items = $session->getQuote()->getAllItems();
Then I initiated an Array:
$ArrayOfItems = array();
Then I looped through the Cart items and stored a delimited string into the Array:
foreach($items as $item) {
$productID = $item->getId();
$_product = $item->getProduct();
$productName = $item->getName();
$itemquantity = $item->getQty();
$_priceIncludingTax = Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice());
$productURL = $_product->getUrlInStore();
$itemsubtotal = $itemquantity * $_priceIncludingTax;
//Getting the Products Category IDs and fetching the name and ID of the mother-category and the last category
$categories_array = $item->getProduct()->getCategoryIds();
$categories_array_end = end($categories_array);
$catscounter = 0;
foreach($categories_array as $categoryId) {
$catscounter = $catscounter + 1;
if ($catscounter > 1) {
break;
}
$category = Mage::getModel('catalog/category')->load($categoryId);
$catidindex = $categoryId;
$catname = $category->getName();
}
//Collecting Array
$ArrayOfItems[] = $catname.'|'.$productName.'|'.$productID.'|'.$itemquantity.'|'.$itemsubtotal.'|'.$catidindex.'|'.$categories_array_end.'|'.$productURL;
}
Now I sorted the Array:
sort($ArrayOfItems);
(This way the Array is sorted by the Category Name of the products mother-category and then by the product name and so on..)
Then finally I looped through the sorted array:
foreach($ArrayOfItems as $ArrayItem) {
$arrayString = explode('|', $ArrayItem);
$catname = $arrayString[0];
$productNameFinal = $arrayString[1];
$productIDfinal = $arrayString[2];
$itemquantity = $arrayString[3];
$itemsubtotal = $arrayString[4];
$categoryId = $arrayString[5];
$categoryIdEnd = $arrayString[6];
$ProductURL = $arrayString[7];
}
Feel free to comment on this, especially if I´m doing something very "stupid" or "Bad" here..
As a side note I must mention that I´m used to working in Joomla, calling out all the "facepalms" :), where I make most of my custom shopping scripts based on this kind of PHP loops and arrays. And I´m sure that this can be done much simpler in Magento as shown by ProxiBlue
for -> Recently Viewed see here
for ->Sorting by rating
Copy the file
app/code/core/Mage/Catalog/Block/Product/List.php
to
app/code/local/Mage/Catalog/Block/Product/List.php
in list.php
find for this line
$this->_productCollection =$layer->getProductCollection();
which will be in around line no 86
add the following code after that
$this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left')
now copy
app/code/core/Mage/Catalog/Model/Config.php
to
app/code/local/Mage/Catalog/Model/Config.php
in config.php find for this code
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
replace with
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'rating_summary' => Mage::helper('catalog')->__('Rating')
);
-->>for BESTSELLER
follow this procedure create a folder naming Inchoo
and inside that folder place Catalog
and inside catalog create 3 folders Block
,etc
and Model
In Block
add Product
in Product
add List
and in List
create a file and name it as Toolbar.php
and ad this code into it
<?php
class Inchoo_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function setCollection($collection)
{
parent::setCollection($collection);
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder() == 'qty_ordered') {
$this->getCollection()->getSelect()
->joinLeft(
array('sfoi' => $collection->getResource()->getTable('sales/order_item')),
'e.entity_id = sfoi.product_id',
array('qty_ordered' => 'SUM(sfoi.qty_ordered)')
)
->group('e.entity_id')
->order('qty_ordered ' . $this->getCurrentDirection());
} else {
$this->getCollection()
->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->getSelect();
}
}
return $this;
}
}
now in etc
folder create a file with name config.xml
and add this code
<config>
<modules>
<Inchoo_Catalog>
<version>0.1.0</version>
</Inchoo_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Inchoo_Catalog_Block_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
<models>
<catalog>
<rewrite>
<config>Inchoo_Catalog_Model_Config</config>
</rewrite>
</catalog>
<catalog_resource>
<rewrite>
<product_collection>Inchoo_Catalog_Model_Resource_Product_Collection</product_collection>
</rewrite>
</catalog_resource>
</models>
</global>
</config>
Now in Model
create a file naming Config.php
and add this code.
<?php class Inchoo_Catalog_Model_Config extends Mage_Catalog_Model_Config
{
public function getAttributeUsedForSortByArray()
{
return array_merge(
parent::getAttributeUsedForSortByArray(),
array('qty_ordered' => Mage::helper('catalog')->__('Sold quantity'))
);
}
}
also create Resource
folder in Model
and in Resource
folder create Product
folder and create a file naming Collection.php
and add following code.
<?php
class Inchoo_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
protected function _getSelectCountSql($select = null, $resetLeftJoins = true)
{
$this->_renderFilters();
$countSelect = (is_null($select)) ?
$this->_getClearSelect() :
$this->_buildClearSelect($select);
if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
}
$countSelect->columns('COUNT(DISTINCT e.entity_id)');
if ($resetLeftJoins) {
$countSelect->resetJoinLeft();
}
return $countSelect;
}
}
Now finally activate this module by going to app/etc/modules
create a file Inchoo_Catalog.xml
add this code.
<?xml version="1.0"?>
<!--
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Connect
* @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
-->
<config>
<modules>
<Inchoo_Catalog>
<active>true</active>
<codePool>community</codePool>
<depends />
</Inchoo_Catalog>
</modules>
</config>
and for SALE
I suggest you this extension as I cannot find any programmatic way to achieve this.
Best Answer
Create the file
Then add the following code:
After this, create another file:
with following code:
Now, to display it, use this code:
And there you could see the most viewed products.
If you want, you could display best selling options with these codes:
And
And to display the most selling products: