0

I'm new to Magento 2. I want to show the sold count of product only after the order was complete.

I have used this code to display count.

$_product = $block->getProduct();
$sku = $_product->getSku(); 
$objectModelManager = \Magento\Framework\App\ObjectManager::getInstance ();
$soldproduct = $objectModelManager->get('Magento\Reports\Model\ResourceModel\Product\Sold\Collection')->addOrderedQty()->addAttributeToFilter('sku', $sku)->setOrder('ordered_qty', 'desc')->getFirstItem(); 
$soldnumber = $soldproduct->getOrderedQty();
?> <div class="sold_count"><b>Sold Count: </b> <?php echo (int)$soldnumber;?> </div>

But for every order placement the count is increasing.But I want show the count of products whose status was completed.

Thanks in advance

2 Answers 2

1

we added code above but not showing (magento 2.3.4)

0
0

I got the solution.We can filter based on the order state 'complete'(addAttributeToFilter('state', 'complete'))

$soldproduct = $objectModelManager->get('Magento\Reports\Model\ResourceModel\Product\Sold\Collection')->addOrderedQty()->addAttributeToFilter('sku', $sku)->setOrder('ordered_qty', 'desc')->addAttributeToFilter('state', 'complete')->getFirstItem(); 

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.