Como consultar os pedidos realizados durante um período de tempo no Magento 2.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
echo "<pre>";
print_r($order->getCreatedAt());
exit;
Uma aproximação mais correta para utilização dentro de um módulo:
public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory
) {
$this->_orderFactory = $orderFactory;
}
$order = $this->_orderFactory->create()->load($passOrderId);
echo "<pre>";
print_r($order->getCreatedAt());
exit;
Consultando diretamente dentro de um Bloco em um módulo:
public function getOrderCollectionByDate($from, $to)
{
$collection = $this->_orderCollectionFactory()->create($customerId)
->addFieldToSelect('*')
->addFieldToFilter('status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)
->setOrder(
'created_at',
'desc'
);
return $collection;
}
class Products extends \Magento\Framework\View\Element\Template
{
protected $_orderCollectionFactory;
public function __construct(
Magento\Framework\App\Action\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context);
}
public function getOrderCollection()
{
$collection = $this->_orderCollectionFactory->create()
->addAttributeToSelect('*')
->addFieldToFilter($field, $condition); //Add condition if you wish
return $collection;
}
public function getOrderCollectionByCustomerId($customerId)
{
$collection = $this->_orderCollectionFactory()->create($customerId)
->addFieldToSelect('*')
->addFieldToFilter('status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)
->setOrder(
'created_at',
'desc'
);
return $collection;
}
}