I've created a new module and I need the functionality from the Magento\Sales\Model\Order model to fetch a list of all orders.
How can I use this model within my own module?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI've created a new module and I need the functionality from the Magento\Sales\Model\Order model to fetch a list of all orders.
How can I use this model within my own module?
Instead of directly using the model, Magento recommends using API.
You can use following sample code to get some insight and modify according to your requirement.
namespace Path\To\Your\Class;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class YourClass extends \ParentClas\Path
{
---
/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
---
/**
* ---
* @param OrderRepositoryInterface $orderRepository
* @param SearchCriteriaBuilder $searchCriteria
* ---
*/
public function __construct(
---
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteria
---
) {
---
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteria
---
}
public function yourMethod(){
$records = $this->orderRepository->getList(
$this->searchCriteriaBuilder
->create()
);
}
}