Skip to main content
added 49 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Magento 2 has service contact class for almost most of theall tables(entities).

InSo for this table, you can usemagento has a service contact class Magento\Sales\Api\OrderItemRepositoryInterface::getList($searchCriteria) . Using this interface for getting the all data of sales_order_item table data.

Code:

<?php

namespace MagentostackExchange\Test\Model;


class OrderItems {

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Sales\Api\OrderItemRepositoryInterface
     */
    private $orderItemRepository;

    public function __construct(
     \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
     \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder       
   ) {
       
       $this->orderItemRepository = $orderItemRepository;
       $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    public function getALlItemofOrderItem(){
        
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $orderItemList = $this->orderItemRepository->getList($searchCriteria);
        
        if($orderItemList->getTotalCount() >= 0)
        {
            foreach ($orderItemList->getItems() as $orderItem)
            {
                echo '<pre>';
                print_r($orderItem->getData());
            }
        }
    }
}

Magento 2 has service contact class for almost most of the tables(entities).

In this, you can useMagento\Sales\Api\OrderItemRepositoryInterface::getList($searchCriteria) interface for getting the all data of sales_order_item table data.

Code:

<?php

namespace MagentostackExchange\Test\Model;


class OrderItems {

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Sales\Api\OrderItemRepositoryInterface
     */
    private $orderItemRepository;

    public function __construct(
     \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
     \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder       
   ) {
       
       $this->orderItemRepository = $orderItemRepository;
       $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    public function getALlItemofOrderItem(){
        
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $orderItemList = $this->orderItemRepository->getList($searchCriteria);
        
        if($orderItemList->getTotalCount() >= 0)
        {
            foreach ($orderItemList->getItems() as $orderItem)
            {
                echo '<pre>';
                print_r($orderItem->getData());
            }
        }
    }
}

Magento 2 has service contact class for almost most of all tables(entities).

So for this table, magento has a service contact class Magento\Sales\Api\OrderItemRepositoryInterface::getList($searchCriteria) . Using this interface for getting the all data of sales_order_item table data.

Code:

<?php

namespace MagentostackExchange\Test\Model;


class OrderItems {

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Sales\Api\OrderItemRepositoryInterface
     */
    private $orderItemRepository;

    public function __construct(
     \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
     \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder       
   ) {
       
       $this->orderItemRepository = $orderItemRepository;
       $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    public function getALlItemofOrderItem(){
        
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $orderItemList = $this->orderItemRepository->getList($searchCriteria);
        
        if($orderItemList->getTotalCount() >= 0)
        {
            foreach ($orderItemList->getItems() as $orderItem)
            {
                echo '<pre>';
                print_r($orderItem->getData());
            }
        }
    }
}
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

Magento 2 has service contact class for almost most of the tables(entities).

In this, you can useMagento\Sales\Api\OrderItemRepositoryInterface::getList($searchCriteria) interface for getting the all data of sales_order_item table data.

Code:

<?php

namespace MagentostackExchange\Test\Model;


class OrderItems {

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Sales\Api\OrderItemRepositoryInterface
     */
    private $orderItemRepository;

    public function __construct(
     \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
     \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder       
   ) {
       
       $this->orderItemRepository = $orderItemRepository;
       $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    public function getALlItemofOrderItem(){
        
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $orderItemList = $this->orderItemRepository->getList($searchCriteria);
        
        if($orderItemList->getTotalCount() >= 0)
        {
            foreach ($orderItemList->getItems() as $orderItem)
            {
                echo '<pre>';
                print_r($orderItem->getData());
            }
        }
    }
}