0

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?

0

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()
        );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.