0

I am trying to override part or all of a function for the file located here: Magento\Customer\Block\Adminhtml\Edit\Tab\Orders.php

I want to change the customer_id to main_table.customer_id in the _prepareCollection() function. So I can add a few more columns to the grid.

Currently I have tried the following code but customer_id is actually not removed, I am just adding another field.

public function _prepareCollection()
{
    $collection = $this->_collectionFactory->getReport('sales_order_grid_data_source')->addFieldToFilter(
        'main_table.customer_id'
    );
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

I have been trying to extend the file and add the new main_table.customer_id but the old customer_id is still there and causes errors.

I am currently overriding the entire file rather than extending to make this work. Which I know eventually could cause issues with upgrades.

Magento\Customer\Block\Adminhtml\Edit\Tab\Orders.php

protected function _prepareCollection()
{
    $collection = $this->_collectionFactory->getReport('sales_order_grid_data_source')->addFieldToSelect(
        'entity_id'
    )->addFieldToSelect(
        'increment_id'
    )->addFieldToSelect(
        'customer_id'
    )->addFieldToSelect(
        'created_at'
    )->addFieldToSelect(
        'grand_total'
    )->addFieldToSelect(
        'order_currency_code'
    )->addFieldToSelect(
        'store_id'
    )->addFieldToSelect(
        'billing_name'
    )->addFieldToSelect(
        'shipping_name'
    )->addFieldToFilter(
        'customer_id',
        $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID)
    );

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

So in my Orders.php file, I just copied the entire file and change the customer_id to what I need.

Vendor\Module\Block\Adminhtml\Edit\Tab\Orders.php

protected function _prepareCollection()
{
    $collection = $this->_collectionFactory->getReport('sales_order_grid_data_source')->addFieldToSelect(
        'entity_id'
    )->addFieldToSelect(
        'increment_id'
    )->addFieldToSelect(
        'customer_id'
    )->addFieldToSelect(
        'created_at'
    )->addFieldToSelect(
        'grand_total'
    )->addFieldToSelect(
        'order_currency_code'
    )->addFieldToSelect(
        'store_id'
    )->addFieldToSelect(
        'billing_name'
    )->addFieldToSelect(
        'shipping_name'
    )->addFieldToFilter(
        'main_table.customer_id', 
        $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID)
    );

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

I am also adding a few columns but I think that will be fairly straight forward once I figure out how to change the _prepareCollection() issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.