I have successfully got the order details and below is my code to get the details. Now, I need to get the ordered item details. Please guide
$collection = $this->_orderCollectionFactory->create()
->addAttributeToSelect('*');
I have successfully got the order details and below is my code to get the details. Now, I need to get the ordered item details. Please guide
$collection = $this->_orderCollectionFactory->create()
->addAttributeToSelect('*');
Try this code
protected $_orderCollectionFactory;
protected $orderRepository;
public function __construct(
......................................
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
......................................
) {
......................................
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->orderRepository = $orderRepository;
......................................
}
public function getAllOrderProducts()
{
$orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
$orderData = $orderCollection->getData();
if (count($orderCollection->getData())) {
if (isset($orderData[0])) {
foreach ($orderData as $attribute) {
$order_id = $attribute['entity_id'];
$order_data = $this->orderRepository->get($order_id);
if (!empty($order_data)) {
foreach ($order_data->getAllItems() as $value) {
print_r($value->debug());
}
}
}
} else {
$order_id = $orderData['entity_id'];
$order_data = $this->orderRepository->get($order_id);
if (!empty($order_data)) {
foreach ($order_data->getAllItems() as $value) {
print_r($value->debug());
}
}
}
}
}
You can use this below code for get ordered item details :
$collection = $this->orderCollectionFactory->create()->addAttributeToSelect('*'); // It returns all order collection
foreach ($collection as $key => $value) {
foreach ($value->getAllItems() as $key1 => $items) {
echo $items->getName() . "<br/>";
echo $items->getPrice() . "<br/>";
echo $items->getSku() . "<br/>";
}
}
foreach ($collection as $order) {
foreach ($order->getAllVisibleItems() as $item) {
echo $item->getName();
echo $item->getQtyOrdered();
..............
..............
}