1

This File responsible for setting Grand Total in Order Grid. I test it.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\Pricing\PriceCurrencyInterface;

/**
 * Class Price
 */
class Price extends Column
{
    /**
     * @var PriceCurrencyInterface
     */
    protected $priceFormatter;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param PriceCurrencyInterface $priceFormatter
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        PriceCurrencyInterface $priceFormatter,
        array $components = [],
        array $data = []
    ) {
        $this->priceFormatter = $priceFormatter;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
                $item[$this->getData('name')] = $this->priceFormatter->format(
                    $item[$this->getData('name')],
                    false,
                    null,
                    null,
                    $currencyCode
                );
            }
        }

        return $dataSource;
    }
}

$item array return all orders i print one order print_r($item) die('');

Array ( [id_field_name] => entity_id [entity_id] => 53 [status] => pending [store_id] => Main Website Main Website Store Default Store View [store_name] => Main Website Main Website Store [customer_id] => 1 [base_grand_total] => 2623.9800 [base_total_paid] => [grand_total] => 2623.9800 [total_paid] => [increment_id] => 000000130 [base_currency_code] => USD [order_currency_code] => USD [shipping_name] => Veronica Costello [billing_name] => Veronica Costello [created_at] => 2019-02-25 19:46:38 [updated_at] => 2019-02-25 14:46:38 [billing_address] => 6146 Honey Bluff Parkway Calder Michigan 49628-7978 [shipping_address] => 6146 Honey Bluff Parkway Calder Michigan 49628-7978 [shipping_information] => Best Way - Table Rate [customer_email] => roni_cost@example.com [customer_group] => 1 [subtotal] => 3030.0000 [shipping_and_handling] => 0.0000 [customer_name] => Veronica Costello [payment_method] => checkmo [total_refunded] =>

I want to get order Items from sales_order_item table when I give a specific id like => $item['entity_id'] then it return all the order items of specific id $item['entity_id'].

How can do this? kindly guide me little bit.

2

Try this,

Add the below code in your controller

<?php                                                         
namespace Vendor\ModuleName\Controller\Orders;                    
class ReadOrders extends \Magento\Framework\App\Action\Action         
{
  protected $resultPageFactory;
  protected $itemFactory;
  public function __construct(
    \Magento\Sales\Model\Order\ItemFactory $itemFactory,
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
  ) {
    $this->itemFactory = $itemFactory;
    $this->resultPageFactory = $resultPageFactory;
    parent::__construct($context);
 }
 public function execute()
 {
    $entity_id = 1; // give your entity_id here to get the details of items
    $order = $this->itemFactory->create()->getCollection()->addFieldToFilter('order_id', $entity_id);
    foreach ($order as $items) {
        echo 'Order Id :'.$itemId =  $items->getItemId() . '<br>';
        echo 'Status :'.$items->getStatus(). '<br>';
        echo 'Product Name :'.$items->getName(). '<br>';
    }
 }}

Hope this helps :)

  • let me check it. – HaFiz Umer Mar 7 at 8:04
  • Dear how can get on same class ? I try this \Magento\Sales\Api\OrderRepositoryInterface $orderRepo in constructor and try to get . it generate error Fatal error: Out of memory (allocated 383778816) (tried to allocate 360714240 bytes) in C:\xampp\htdocs\market-place\vendor\magento\module-sales\Ui\Component\Listing\Column\Price.php on line 56 – HaFiz Umer Mar 7 at 8:21
  • Updated now hitting the item table passing the order_id also known as the entity id from sales_order table. and It works fine, try it and let me know – Prathap Gunasekaran Mar 7 at 8:22
  • Yes it work Thanks. here 1 condition check $order = $this->itemFactory->create()->getCollection()->addFieldToFilter('order_id', $entity_id); i need 2 condition. in sales_order_item table i have a custom attribute "seller_id" i want to check 2 condition like entity_id and seller_id. How use addFieldToFilter on 2 condidtions ? – HaFiz Umer Mar 7 at 8:30
  • $this->itemFactory->create()->getCollection()->addFieldToFilter('order_id', $entity_id)->addFieldToFilter('seller_id',$seller_id); this filters out those matching :) – Prathap Gunasekaran Mar 7 at 8:32

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.