News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Ordering cart items by SKU

Started by inkomico, September 24, 2015, 19:56:15 PM

Previous topic - Next topic

inkomico

Is there any way to ordering items by SKU on the cart?

I created the default_pricelist.php override but I donĀ“t know how to ordering.

I tried this code before the line

foreach( $this->cart->products as $pkey =>$prow ) { ?>


function order_by_sku($a, $b)
{
return ($a->product_sku > $b->order_item_sku);
}
usort($this->cart->products, 'order_by_sku');


Probaly my PHP skills are the worse  ::)

Thanks.

Studio 42

Hi inkomico,

try this.

before
foreach( $this->cart->products as $pkey =>$prow ) {
add
$bySku = array();
foreach( $this->cart->products as $pkey =>$prow ) {
$bySku[$b->order_item_sku] = $prow ;
}
ksort( $bySku);
$this->cart->products = $bySku;


Write from scratch
Greets,
Patrick

inkomico

Thanks, I tried but not worked.

I think that order by sku in the orders are more simple.

I will try.

My last code not worked in invoice_items.php



//ordering by sku
function order_by_sku($a, $b)
{
  return ($a->order_item_sku > $b->order_item_sku);
}
usort($this->orderDetails['items'], 'order_by_sku');


Studio 42

Hi,
If you want absolutly use usort then this should work :


function order_by_sku($a, $b) {
        $v1 = $a->order_item_sku;
        $v2 = $a->order_item_sku;
        if ($v1  == $v2 ) {
            return 0;
        }
        return ($v1  > $v2) ? +1 : -1;
}
usort($this->orderDetails['items'], "order_by_sku");


Derived from PHP doc example with object and not tested

Greets,
Patrick