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:
Then I initiated an Array:
Then I looped through the Cart items and stored a delimited string into the Array:
Now I sorted the Array:
(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:
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