0

I need override block file in custom module. Below is block file path.

vendor/magento/module-sales/Block/Adminhtml/Order/Create/Search/Grid.php

How can I do that?

Yes, please check this error and code what i get.

enter image description here

etc/di.xml

<?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="Company\ModuleName\Block\Adminhtml\Order\Create\Search\Grid" />
</config>

Bloc/Adminhtml/order/Create/Search/Grid.php

<?php

namespace Company\ModuleName\Block\Adminhtml\Order\Create\Search;


class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
{
    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, $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',
                'left'
        );

        $this->setCollection($collection);
        return parent::_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',
                'index' => 'name'
            ]
        );
        $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
        $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'
            ]
        );
       $this->addColumn(
                'qty_in_stock',
                [
                'header' => __('Quantity In Stock'),
                'type' => 'number',
                'index' => 'qty_in_stock'
                ]
        );
        $this->addColumn(
            'in_products',
            [
                'header' => __('Select'),
                'type' => 'checkbox',
                'name' => 'in_products',
                'values' => $this->_getSelectedProducts(),
                'index' => 'entity_id',
                'sortable' => false
            ]
        );

        $this->addColumn(
            'qty',
            [
                'filter' => false,
                'sortable' => false,
                'header' => __('Quantity'),
                'renderer' => 'Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty',
                'name' => 'qty',
                'inline_css' => 'qty',
                'type' => 'input',
                'validate_class' => 'validate-number',
                'index' => 'qty'
            ]
        );

        return parent::_prepareColumns();
    }

    /**
     * Get grid url
     *
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl(
            'sales/*/loadBlock',
            ['block' => 'search_grid', '_current' => true, 'collapse' => null]
        );
    }

    /**
     * Get selected products
     *
     * @return mixed
     */
    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('products', []);

        return $products;
    }

    /**
     * Add custom options to product collection
     *
     * @return $this
     */
    protected function _afterLoadCollection()
    {
        $this->getCollection()->addOptionsToResult();
        return parent::_afterLoadCollection();
    }


}

Just i need override this block and add new column for available qty for the product and it used when create order from admin. So please check let me know what i missing.

  • make sure you are passing all arguments, which are needed in the Parent constructor – Mrunmay Deswandikar Apr 17 '17 at 5:24
2

To overide block file you need to do this things.

1 – First of all create di.xml file in Folder Company/Extension/etc/di.xml

<?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="Company\Extension\Block\Adminhtml\Order\Create\Search\Grid" />
</config>

2 – Now create Grid.php Block file in Folder Company\Extension\Block\Adminhtml\Order\Create\Search

<?php
    namespace Company\Extension\Block\Adminhtml\Order\Create\Search;

    class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
    {
        // WRITE YOUR FUNCTION TO OVERIDE
    }
?>
  • Hello Dhiren, Yes see that solution, but i can't get proper way can you please elaborate more. Thankyou Man – Dhrumin Apr 15 '17 at 10:16
  • @Dhrumin can you tell me what error you face in above code? – Dhiren Vasoya Apr 15 '17 at 10:17
  • @Dhrumin Preference is like rewrite in Magento 1. – MagePsycho Apr 15 '17 at 11:55
  • 1
    @Dhrumin if you have another question, then please ask new question. Becuase you have question that is not related to your previous question. This things help other in community. Hope you understand. – Dhiren Vasoya Apr 17 '17 at 5:16
  • 1
    @Dhrumin if above answer will hel you, please accept it. It will help other in community as well. – Dhiren Vasoya Apr 17 '17 at 5:45

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.