1

Our client would like to have names of all people who ordered a product included into the Out of Stock notification email. I seem to have issue with getting such info ...

Code I have to get the orders by product sku is (in helper): ($orderFactory is \Magento\Sales\Model\ResourceModel\Order\CollectionFactory)

public function getOrdersWithProductSku($productSku)
    {
        try {
            if ($productSku)
            {
                $orderCollection = $this->orderFactory->create();
                $tableName   = $this->orderFactory->getTableName('sales_order_item');
                $orderCollection->addFieldToSelect('*');
                $orderCollection->getSelect()
                ->join(
                    $tableName,
                    'main_table.entity_id = ' . $tableName. '.order_id'
                )->where('sku LIKE '.$productSku);

            $orderCollection->getSelect()->group('main_table.entity_id');

            return $orderCollection;
            }
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

Then parsing that collection in the Observer as:

try
                        {
                            $orderCollection = $this->helperData->getOrdersWithProductSku($productSku);
                            if ($orderCollection && !empty($orderCollection))
                            {
                                foreach ($orderCollection as $order) {
                                    array_push($names, $order->getBillingAddress()->getName());
                                }
                            }
                            else
                            {
                                array_push($names, "Customer names list is either null or empty");
                            }
                        }
                        catch(\Exception $e)
                        {
                            array_push($names, "Error generating customer names: ". $e->getMessage());
                        }

Is there an easy way to do this .... simply get an array of names who ordered the product based on SKU

Thanks

0

I found a way by simply querying the database directly for such info

Your Answer

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

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