I have an array that contains some SKUs belonging to the current category.
I want to reorder the collection based on that array so that the SKUs present in my order array will come first.
For this i have been trying to edit the list.phtml file
$category = Mage::registry('current_category');
$inArray = explode(',', $category->getData('top_skus'));
usort($_productCollection, function($a, $b) use ($inArray){
print_r($a);
$aAge = $a['sku'];
$bAge = $b['sku'];
$aWeight = 0;
$bWeight = 0;
if (in_array($aAge, $inArray))
$aWeight++;
if (in_array($bAge, $inArray))
$bWeight++;
if ($aWeight != $bWeight) {
return $aWeight > $bWeight ? -1 : 1;
} else if ($aWeight > 0) {
// need to sort by order which specified in array
$aIndex = array_search($aAge, $inArray);
$bIndex = array_search($bAge, $inArray);
return ($aIndex == $bIndex ? 0 : ($aIndex > $bIndex ? 1 : -1));
} else {
// just compare age values
return ($aAge == $bAge ? 0 : ($aAge > $bAge ? 1 : -1));
}
});
I am getting an error like this
Warning: usort() expects parameter 1 to be array, object given in C:\wamp\www\magento1\app\design\frontend\base\default\template\catalog\product\list.phtml on line 74
How to supply the array that contains the products to my usort? From where i will get that array?
Best Answer
I assume you are constructing the collection like this:
and then calling
it doesn't work because
$_productCollection
is not an array, but you figured that out from the error message (I'm just being Captain Obvious).You can try this instead.