Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Alphabetical order issue

  • Resolved vitalseeds

    (@vitalseeds)


    Hello

    Is there a way to change how the items are organised in alphabetical order?

    At the moment the plug-in arranges them differently to woocommerce and our database too.

    e.g. Packing slip is arranged ‘KaRR’ comes after ‘KRAS’ – it is prioritizing the uppercase letter over the letter itself

    We want to organise by letter not capitalisation, so KRAS comes after KaRR

    Is this possible?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Ewout

    (@pomegranate)

    Hi! The PDF invoice plugin does not apply any sorting, but (just like the order in the WooCommerce backend and the email sent to your customer), the items are presented in the order they have been added to the cart.

    You can override this with a small code snippet:

    
    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_name', 10, 2 );
    function wpo_wcpdf_sort_items_by_name ( $items, $order ) {
    	usort($items, function ($a, $b) {
    		return strcmp($a['name'], $b['name']);
    	});
    	return $items;
    }
    

    Note that this is not sensitive to any capitalization, it just sorts plain alphabetically.

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

    Thanks for your reply.

    At the moment we are using the snippet below to order by SKU, but it is ordering by case rather than letter as i mentioned before (see list below snippet), is there a way around this and to order by letters only?

    —————–
    //Sort items by SKU
    add_filter( ‘wpo_wcpdf_order_items_data’, ‘wpo_wcpdf_sort_items_by_sku’, 10, 2 );
    function wpo_wcpdf_sort_items_by_sku ( $items, $order ) {
    usort($items, ‘wpo_wcpdf_sort_by_sku’);
    return $items;
    }

    function wpo_wcpdf_sort_by_sku($a, $b) {
    if ($a[‘sku’]==$b[‘sku’]) return 0;
    return ($a[‘sku’]<$b[‘sku’])?-1:1;
    }
    ———————

    BLFC – Chard – Five colours (Organic) 1
    CuMa – Cucumber – Marketmore (Organic) 1
    LkMu – Leek – Musselburgh (Organic) 1
    OnWL – Spring onion – White Lisbon (Organic) 1
    OrPC – Oriental greens – Pak Choi (Organic) 1
    XCaZ – Calendula – Traingle Flashback / Zeolights (Organic) 1
    mssr – Rocket – Salad rocket (Organic)

    Plugin Author Ewout

    (@pomegranate)

    Yes, it’s probably better to use strcasecmp instead, very much like my previous example. Try this instead of your own filter:

    
    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_sku', 10, 2 );
    function wpo_wcpdf_sort_items_by_sku ( $items, $order ) {
    	usort($items, function ($a, $b) {
    		return strcasecmp($a['sku'], $b['sku']);
    	});
    	return $items;
    }
    

    You can then also remove that second wpo_wcpdf_sort_by_sku function.

    I tried that and got a fatal error!:
    ——
    Don’t Panic
    The code snippet you are trying to save produced a fatal error on line 1:

    syntax error, unexpected ‘&’
    The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.

    Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

    Plugin Author Ewout

    (@pomegranate)

    That must have been a copy paste error, there is no & in the snippet above.

    Oh thanks, i realised it must be because i copied from the email notification i received and not from your post on this page, seems to be working well now.
    Thanks very much for your help.
    Fred

    Plugin Author Ewout

    (@pomegranate)

    Glad to hear that Fred, let us know if you need help with anything else!

    If you can spare a moment, we’d appreciate a plugin review here on wordpress.org: https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/#new-post

    Thanks in advance and happy selling!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Alphabetical order issue’ is closed to new replies.