Let’s initiate a discussion!!
If you want to add custom column in product grid, then follow below step to create custom module to add custom column in grid.
Step 1) app/code/Magemonkeys/SalesordercreategridCustomattr/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_SalesordercreategridCustomattr', __DIR__ ); |
Step 2) app/code/Magemonkeys/SalesordercreategridCustomattr/etc/module.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkeys_SalesordercreategridCustomattr" setup_version="1.0.0"> </module> </config> |
Step 3) app/code/Magemonkeys/SalesordercreategridCustomattr/etc/di.xml
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid" type="Magemonkeys\SalesordercreategridCustomattr\Block\Adminhtml\Order\Create\Search\Grid" /> </config> |
Step 4) app/code/Magemonkeys/SalesordercreategridCustomattr/Block/Adminhtml/Order/Create/Search/Grid.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
<?php namespace Magemonkeys\SalesordercreategridCustomattr\Block\Adminhtml\Order\Create\Search; class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid { /** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Backend\Helper\Data $backendHelper * @param \Magento\Catalog\Model\ProductFactory $productFactory * @param \Magento\Catalog\Model\Config $catalogConfig * @param \Magento\Backend\Model\Session\Quote $sessionQuote * @param \Magento\Sales\Model\Config $salesConfig * @param array $data */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Catalog\Model\Config $catalogConfig, \Magento\Backend\Model\Session\Quote $sessionQuote, \Magento\Sales\Model\Config $salesConfig, array $data = [] ) { $this->_productFactory = $productFactory; $this->_catalogConfig = $catalogConfig; $this->_sessionQuote = $sessionQuote; $this->_salesConfig = $salesConfig; parent::__construct($context, $backendHelper, $productFactory, $catalogConfig, $sessionQuote, $salesConfig, $data); } /** * Prepare collection to be displayed in the grid * * @return $this */ protected function _prepareCollection() { $attributes = $this->_catalogConfig->getProductAttributes(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $collection = $this->_productFactory->create()->getCollection(); $collection->setStore( $this->getStore() )->addAttributeToSelect( $attributes )->addAttributeToSelect( 'sku' )->addStoreFilter()->addAttributeToFilter( 'type_id', $this->_salesConfig->getAvailableProductTypes() )->addAttributeToSelect( 'gift_message_available' ); $collection->joinField( 'qty_in_stock', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1 AND {{table}}.website_id=0', 'left' ); $this->setCollection($collection); $parent = get_parent_class($this); $parentclass = get_parent_class($parent); return $parentclass::_prepareCollection(); } /** * Prepare columns * * @return $this */ protected function _prepareColumns() { $this->addColumn( 'entity_id', [ 'header' => __('ID'), 'sortable' => true, 'header_css_class' => 'col-id', 'column_css_class' => 'col-id', 'index' => 'entity_id' ] ); $this->addColumn( 'name', [ 'header' => __('Product'), 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Product::class, 'index' => 'name' ] ); $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']); $this->addColumn('customsku', ['header' => __('Custom SKU'), 'index' => 'customsku']); $this->addColumn( 'price', [ 'header' => __('Price'), 'column_css_class' => 'price', 'type' => 'currency', 'currency_code' => $this->getStore()->getCurrentCurrencyCode(), 'rate' => $this->getStore()->getBaseCurrency()->getRate($this->getStore()->getCurrentCurrencyCode()), 'index' => 'price', 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Price::class ] ); $this->addColumn( 'in_products', [ 'header' => __('Select'), 'type' => 'checkbox', 'name' => 'in_products', 'values' => $this->_getSelectedProducts(), 'index' => 'entity_id', 'sortable' => false, 'header_css_class' => 'col-select', 'column_css_class' => 'col-select' ] ); $this->addColumn( 'qty', [ 'filter' => false, 'sortable' => false, 'header' => __('Quantity'), 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty::class, 'name' => 'qty', 'inline_css' => 'qty', 'type' => 'input', 'validate_class' => 'validate-number', 'index' => 'qty' ] ); $this->addColumn( 'qty_in_stock', [ 'header' => __('Stock'), 'type' => 'number', 'index' => 'qty_in_stock' ] ); $parent = get_parent_class($this); $parentclass = get_parent_class($parent); return $parentclass::_prepareColumns(); } } |
Step 5 : After place above code, please run below mentioned commands
1 2 3 |
- php bin/magento setup:upgrade - php bin/magento setup:static-content:deploy - php bin/magento cache:clean |
That’s it…
Now refresh your sales order, create grid page and see that your custom column will be reflecting there.
[crayon-641f03327fd6f274855122/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.