Magento 1.8 – How to Sort Product Array on Checkout Page by Mother-Category IDs

magento-1.8onepage-checkoutproductproduct-listsorting

I´m currently working om some modifications on my client´s Magento "One Page Checkout" page.

I have managed to get all the product items using this code:

$session= Mage::getSingleton('checkout/session');    
$items = $session->getQuote()->getAllItems();

So now I can loop through the products in the cart and for instance echo quantity and price by writing:

foreach($items as $item) {
$itemquantity = $item->getQty();
$itemPrice = $item->getPrice();
}

Now, what I need to do is to sort the $items array so that the products in the loop comes out sorted by the products 1st./mother/root category ID.

As far as I can tell the $items array is sorted according to the order of which the products are added to the card.
So I need to change this sort order so that the products with the lowest mother-category ID comes first and the products with highest mother-category ID come last.

I hope someone out there can help me with this.

Best Answer

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