2

I am using the object manager (directly and intentionally) to get the sales quantity of a product.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product_id = 101;
$sold_products = $objectManager->get('Magento\Reports\Model\ResourceModel\Product\Sold\Collection')
                             ->addOrderedQty()
                             ->addAttributeToFilter('product_id', $product_id)
                             ->getFirstItem();

This successfully allows me to get the quantity sold of a product by passing in the product ID to the attribute filter. Now I want to filter the results by a starting and end date...

$start_date = '2001-12-12 12:12';
$end_date = '2019-12-12 12:12';

... by adding this to the object manager parameters:

->addAttributeToFilter('created_at', array('from' => $start_date,'to' => $end_date))

But I can't seem to get this code to work. How do I filter results by a starting and end date?

EDIT: Workaround solution - The accepted answer works perfectly, however I ended up having to use the following workaround, which is a little slower but works perfectly fine too. Posting in case it helps anyone in a similar situation.

$salesRepository = $objectManager->get('\Magento\Sales\Model\Order\ItemFactory');
$sold_products = $salesRepository->create()
                                 ->getCollection()
                                 ->addAttributeToFilter('product_id', $product_id);

foreach ($sold_products as $sold_product) {
    $created_at = $sold_product->getData('created_at');
    if ($created_at < $start_date || $created_at > $end_date) {
        continue;
    }
    echo $created_at."<br>";
}

This will get you only the products within the specified date range.

2 Answers 2

3

Try this :

$startDate = date("Y-m-d h:i:s",strtotime('2001-12-12'));
$endDate = date("Y-m-d h:i:s", strtotime('2019-12-12'));

->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate));
2
  • Thanks for the response Vinz. I believe this is the correct solution, however it is not working for me. I am getting the error: Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous Which means that I am trying to select the created_at column from multiple tables and it doesn't know which one to take it from. Do you know how to find which table a Magento function is calling? Commented Dec 19, 2019 at 23:00
  • 1
    Your solution works, but because of my unique situation and how my DB was setup, I was getting errors. So I'm posting my unique solution in case anybody is interested. Commented Dec 20, 2019 at 16:57
0

Workaround solution:

$start_date = '2001-12-12 12:12';
$end_date = '2019-12-12 12:12';
$salesRepository = $objectManager->get('\Magento\Sales\Model\Order\ItemFactory');
$sold_products = $salesRepository->create()
                                 ->getCollection()
                                 ->addAttributeToFilter('product_id', $product_id);

foreach ($sold_products as $sold_product) {
    $created_at = $sold_product->getData('created_at');
    if ($created_at < $start_date || $created_at > $end_date) {
        continue;
    }
    echo $created_at."<br>";
}

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.