0

The items in printout of the invoices, shipments and credit memos are sorted by sequences as to when they are added in the order. My question is how do I go about sorting them. For example how do I go about sorting them based on SKU column.

Scenario is that I have a long list of products in our Magento 2.36 store. In which each product's SKU is compromised of 9 integers.

Case in point: 106110152, 106110163, 106110203, 106110172, etc.

I want to be able to sort based on the last digit of each items SKU number.

Product SKU
Product with last digit 2 106110152
Product with last digit also 2 106110172
Product with last digit 3 106110163
Product with last digit also 3 106110203

I have been trying and playing with invoice.php and figured out I have to do something with:

            /* Add body */
        foreach ($invoice->getAllItems() as $item) {
            if ($item->getOrderItem()->getParentItem()) {
                continue;
            }
            /* Draw item */
            $this->_drawItem($item, $page, $order);
            $page = end($pdf->pages);
        }

Any comments, suggestions, etc would be a massive help.

1 Answer 1

0

So I figured out how to tackle this problem. BTW I have installed a 3rd party module called FOOMAN\PrintOrderPDF (for printing the order list!) and modified on top of it. This is just for testing purposes for now. The logic is the same to modify the Invoice, Shipping and Credit Memos.

Modify:

        foreach ($order->getAllItems() as $item) {
            if ($item->getParentItem()) {
                continue;
            }
            

with this:

       $_sortedItems = array();

       foreach ($order->getAllItems() as $item) {
           $_sortedItems[$item->getSKU()] = $item;
        }

        function my_sort($a,$b)
        {
            if (($a->getSKU()) == ($b->getSKU())) {
                return 0;
            }

            return (($a->getSKU()%10) < ($b->getSKU()%10)) ? -1 : 1;
        } 
        usort($_sortedItems, "Fooman\PrintOrderPdf\Model\Pdf\my_sort");

        foreach ($_sortedItems as $item) {
            if ($item->getParentItem()) {
                continue;
            }

Posting this as "if someone is facing the same problem" and needs inspiration!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.