I want attribute set name in sales order grid In that column I want an Ordered First Item Attribute Set Name. In Magento 2

<?php

namespace Emipro\AdminSalesOrderGrid\Ui\Component\Listing\Column;

use Magento\Catalog\Model\ProductFactory;
use Magento\Eav\Model\AttributeSetRepository;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Item;
use Magento\Sales\Model\ResourceModel\Order\Grid\CollectionFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class Attribute extends Column {

public function __construct(
    ContextInterface $context,
    OrderInterface $order,
    Item $item,
    CollectionFactory $collection,
    ProductFactory $product,
    AttributeSetRepository $attributeSet,
    UiComponentFactory $uiComponentFactory,
    array $components = [],
    array $data = []
) {
    $this->attributeSet = $attributeSet;
    $this->item = $item;
    $this->order = $order;
    $this->product = $product;
    $this->collection = $collection->create();
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function getAttributeSetIdByProductId($product_id) {
    $product = $this->product->create()->load($product_id);
    foreach ($product as $item) {
        $attribute_set_id = $product->getAttributeSetId();
        $attributeCollection = $this->attributeSet->get($attribute_set_id);
        foreach ($attributeCollection as $name) {
            $attribute_set_name = $name['attribute_set_name'];
        }
    }
    return $attribute_set_name;
}

public function getOrderDetails($entityId) {

    $orderCollection = $this->order->load($entityId);
    $ordered_items = $orderCollection->getAllItems();
    $item_sku = "";
    $attribute_set_name = "";
    foreach ($ordered_items as $item) {
        $item_id = $item->getItemId();
        $product_id = $item->getProductId();

        $attribute_set_name = $this->getAttributeSetIdByProductId($product_id);

        if ($item_id == 1) {
            $item_sku = $item->getSku();
        }

        return $attribute_set_name;
    }
}

public function prepareDataSource(array $dataSource) {

    if (isset($dataSource['data']['items'])) {

        foreach ($dataSource['data']['items'] as &$item) {

            $item[$this->getData('name')] = $this->getOrderDetails($item['entity_id']);

            // echo "<pre>";
            // print_r($item);
            // exit;
        }
    }
    return $dataSource;
}
}

Solutions

namespace Emipro\AdminSalesOrderGrid\Ui\Component\Listing\Column;

use Magento\Catalog\Model\ProductFactory;
use Magento\Eav\Model\AttributeSetRepository;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Item;
use Magento\Sales\Model\ResourceModel\Order\Grid\CollectionFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class Attribute extends Column {

public function __construct(
    ContextInterface $context,
    OrderInterface $order,
    Item $item,
    CollectionFactory $collection,
    ProductFactory $product,
    AttributeSetRepository $attributeSet,
    UiComponentFactory $uiComponentFactory,
    array $components = [],
    array $data = []
) {
    $this->attributeSet = $attributeSet;
    $this->item = $item;
    $this->order = $order;
    $this->product = $product;
    $this->collection = $collection->create();
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function getAttributeSetIdByProductId($product_id) {
    $product = $this->product->create()->load($product_id);
    foreach ($product as $item) {
        $attribute_set_id = $product->getAttributeSetId();
        $attributeCollection = $this->attributeSet->get($attribute_set_id);
        foreach ($attributeCollection as $name) {
            $attribute_set_name = $name['attribute_set_name'];
        }
    }
    return $attribute_set_name;
}

public function getOrderDetails($entityId) {

    $orderCollection = $this->order->load($entityId);
    $ordered_items = $orderCollection->getAllItems();
    $item_sku = "";
    $attribute_set_name = "";
    foreach ($ordered_items as $item) {
        $item_id = $item->getItemId();
        $product_id = $item->getProductId();

        $attribute_set_name = $this->getAttributeSetIdByProductId($product_id);

        if ($item_id == 1) {
            $item_sku = $item->getSku();
        }

        return $attribute_set_name;
    }
}

public function prepareDataSource(array $dataSource) {

    if (isset($dataSource['data']['items'])) {

        foreach ($dataSource['data']['items'] as &$item) {

            $item[$this->getData('name')] = $this->getOrderDetails($item['entity_id']);

        }
    }
    return $dataSource;
}

}

Try this

<?php
namespace Emipro\AdminSalesOrderGrid\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Attribute extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    protected $attributeSet;
    protected $_productloader;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet, \Magento\Catalog\Model\ProductFactory $_productloader, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->attributeSet = $attributeSet;
        $this->_productloader = $_productloader;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function getAttributeSetName($product)
    {       
        $attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
        return $attributeSetRepository->getAttributeSetName();
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $items = $order->getAllItems();
                foreach ($items as $_item) {
                    $productId = $_item->getProductId();
                    $product = $this->_productloader->create()->load($productId);
                    $attributeSetName =  $this->getAttributeSetName($product);
                    break;
                }
                $item[$this->getData('name')] = $attributeSetName;
            }
        }

        return $dataSource;
    }
}
Tags: Magento 2.x

Similar questions

I want to rebill on every month first Wednesday of first full week, Every month should starts from Monday
I have a subscription module, it rebills a term on every month on first day, But here I want to rebill on Wednesday of first full week of a month. I need to write this in php code. Currently I am getting the First Wednesday of month. here is the snippet Actual result: First Wednesday in first week of a month but it starts from 02 is Wednesday. Expe...
How to refresh sales order grid by calling \Magento\Sales\Model\ResourceModel\Grid::refresh method
How to call \Magento\Sales\Model\ResourceModel\Grid::refresh method I am following this link: blog.sivaschenko.com Getting
How to add extra column in Item ordered tab in Magento2.3?
How to add an extra column in Item ordered tab in Magento 2? PFA Any ideas?
Magento2 How to custom column in sales order item renderer in admin order view
I need to add custom column in admin sales_order_view for each item listing with the help of renderer. For example, there are some predefined columns like name, status, price, qty, discount etc are in order item listing. I need to order custom column in that listing.
Magento2 How to add column in sales order item renderer in admin order view
I have two custom fields in sales_order_item table. Now i want to show that two fields in sales order item section. I tried this solution. But it is always adding new column at the end. So, how i can add my custom column after Item status and also should be editable? Like attached example.
Magento2 : Show First placed order first in order history
In My order tab in customer account show last order first i want there first placed order first.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.