2

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.

2
  • Do you have product id in order details? Nov 9, 2016 at 5:47
  • Yes i have it in variable $orderId. Nov 9, 2016 at 5:50

2 Answers 2

3

you can get order item detail with below code.

$order_ = $objectManager->create('\Magento\Sales\Model\Order')
                                       ->load($orderId); 
$orderItems = $order_->getAllItems();
foreach ($orderItems as $item) {
        $proID = $item->getProductId();
        $itemSku = $item->getSku();
}
1
  • Thanks for your answer will try it, and get back to you. Nov 9, 2016 at 5:55
2

If you have product id in Order Data apply this to get product details:

/**
 * @var \Magento\Catalog\Model\ProductFactory
 */
protected $_productFactory;



public function __construct(
    ...
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    ...
    $this->_productFactory = $productFactory;
    ...
}

Get product ID:

$order_ = $_objectManager->create('\Magento\Sales\Model\Order')
                                   ->load($orderId); 
$orderItems = $order_->getAllItems();
foreach ($orderItems as $item) 
{
    $productIds[] = $item->getProductId();

}

Then use :

    $collection = $this->_productFactory->create()->getCollection()->addAttributeToSelect(
        'name'
    )->addAttributeToSelect(
        'sku'
    )->addAttributeToSelect(
        'price'
    )->addAttributeToFilter(
        'entity_id', array('in' => $productIds)
    );
4
  • how you get product id here i don't understand? Nov 9, 2016 at 6:02
  • I have updated my answer, Please check how to get product Ids Nov 9, 2016 at 6:05
  • why you are loading the product again when $item object containing the information of sku,product_id? why need extra code Nov 9, 2016 at 6:06
  • $item has not all information about product if we need more data like name, description and images , We have to user product data. Nov 9, 2016 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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