2

For Product A have 100 qty., For Product B i have 200 qty.

I would like to check how many products ordered for a particular product.

Which table i need to look up & what will be the query to get that in format of Magento.

Thanks

2 Answers 2

1

You can get product sold quantity by product SKU for the particular product using the below-mentioned code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$soldproduct = $objectManager->get('Magento\Reports\Model\ResourceModel\Product\Sold\Collection')->addOrderedQty()->addAttributeToFilter('sku', $sku)->setOrder('ordered_qty', 'desc')->getFirstItem(); 
$soldnumber = $soldproduct->getOrderedQty();
0

This should work:

 
$id = 786 // enter your product ID here
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = $connection->select()
    ->from(
        ['o' => 'mg_sales_order_item'],
        [ new \Zend_Db_Expr('SUM(o.qty_ordered) as qty_ordered') ]
    )
    ->where('o.product_id=?', $id)
    ->group('o.product_id');

$result = $connection->fetchAll($select); //echo''; print_r($result);

It's working for me

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.