1

I'm trying to write a function that takes an order object, and returns an array of sku => qty (key-value) showing how many of each sku was sold/shipped.

The tricky part is, magento formats line items differently on configurable and bundle products.

Example:

Line1: name shirt-X2, type bundle, ordered 2, shipped 2.
Line2: name shirt, type simple, ordered 4, shipped 0. // <-- my problem

I've written this code:

/**
 * @param \Magento\Sales\Model\Order $order
 * @return array
 */
public function getOrderData($order)
{
    $result = [];
    foreach ($order->getAllItems() as $item) {
        if ($item->getProductType() != 'simple') {
            continue;
        }
        $sku = $item->getSku();
        $orderedReal = $item->getQtyOrdered();
        $orderedB = $item->getQtyOrdered() - $item->getQtyRefunded() - $item->getQtyCanceled();
        $orderedB = max(round($orderedB, 8), 0);
        $invoicedB = $item->getQtyOrdered() - $item->getQtyInvoiced() - $item->getQtyCanceled();
        $invoicedB = max(round($invoicedB, 8), 0);
        $shippedReal = 0;
        $isShipped = false;
        if ($item->getParentItem()) {
            $parent = $item->getParentItem();
            if ($parent->getQtyOrdered() > 0 && $parent->getQtyOrdered() == $parent->getQtyShipped()) {
                $isShipped = true;
            }
        } else {
            if ($item->getQtyOrdered() > 0 && $item->getQtyOrdered() == $item->getQtyShipped()) {
                $isShipped = true;
            }
        }
        if ($isShipped) {
            $shippedReal = $orderedReal;
        }
        $result[] = [
            'sku' => $sku,
            'invoiced' => $invoicedB,
            'ordered' => $orderedB,
            'shipped' => $shippedReal
        ];
    }
    return $result;
}

Is this function valid, or could it be improved ? Any help is appreciated.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.