Skip to main content
3 of 3
improved title
Qaisar Satti
  • 32.5k
  • 18
  • 87
  • 137

Get product information in admin controller using the order id?

I want to get the product information like product id, sku at admin(backend), When action is being performed on any order. How can I achieve this.

This is code:

<?php
    
namespace Custom\Confirm\Controller\Adminhtml\Order;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Custom\Confirm\Model\ResourceModel\Saleslist\CollectionFactory;
    
/**
 * Class Payseller.
 */
class MassConfirm extends \Magento\Backend\App\Action
{
     /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $_orderRepository;
 
    /**
     * @var \Magento\Sales\Model\Service\InvoiceService
     */
    protected $_invoiceService;
 
    /**
     * @var \Magento\Framework\DB\Transaction
     */
    protected $_transaction;
    
    protected $_order;
 
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\Service\InvoiceService $invoiceService,
        \Magento\Sales\Model\Order $orderInfo,
        \Magento\Framework\DB\Transaction $transaction
    ) {
        $this->_orderRepository = $orderRepository;
        $this->_order = $orderInfo;
        $this->_invoiceService = $invoiceService;
        $this->_transaction = $transaction;
        parent::__construct($context);
    }
   
    /**
     * Execute action.
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     *
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
            /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        
        $wholedata = $this->getRequest()->getPost();
        
        foreach ($wholedata['selected'] as $orderId){
        
        
         $order = $this->_orderRepository->get($orderId);
            
            
                $order2=$this->_order->load($orderId);
                $items = $order2->getAllItems();
                echo $orderId;
                //var_dump($items);
                foreach($items as $i){
                 //var_dump($i);
                 // code to retrieve product id 
                }
            
                
                  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $order_ = $objectManager->create('\Magento\Sales\Model\Order')
                                           ->load($orderId); 
                 
                $order_->setState("processing")->setStatus("processing");
                 
                $order_->save();  
            
        }   
        $this->messageManager->addSuccess(__('The order has been confirm. '));
        return $resultRedirect->setPath('sales/order/index');
    }

}

Thanks in advance.

Nitin Pawar
  • 1.6k
  • 9
  • 24
  • 49