1

I am trying to override block file of adminhtml of sales module.

Below is the code of my 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\Form\Address" type="Vendor\Module\Block\Adminhtml\Order\Create\Form\Address" />

</config>

I have used the Address.php file from this path

Magento\Sales\Block\Adminhtml\Order\Create\Form

and moved into my module , but the block file is not overrided,

here is the code of my block file

  <?php

   namespace Vendor\Module\Block\Adminhtml\Order\Create\Form;

   use Magento\Backend\Model\Session\Quote;
   use Magento\Framework\App\ObjectManager;
   use Magento\Framework\Data\Form\Element\AbstractElement;
   use Magento\Framework\Pricing\PriceCurrencyInterface;
   use Psr\Log\LoggerInterface;

   class Address extends \Magento\Sales\Block\Adminhtml\Order\Create\Form\Address
{
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Model\Session\Quote $sessionQuote,
    \Magento\Sales\Model\AdminOrder\Create $orderCreate,
    PriceCurrencyInterface $priceCurrency,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
    \Magento\Directory\Helper\Data $directoryHelper,
    \Magento\Framework\Json\EncoderInterface $jsonEncoder,
    \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
    \Magento\Customer\Model\Options $options,
    \Magento\Customer\Helper\Address $addressHelper,
    \Magento\Customer\Api\AddressRepositoryInterface $addressService,
    \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Customer\Model\Address\Mapper $addressMapper,
    array $data = []
) {
    parent::__construct(
        $context,
        $sessionQuote,
        $orderCreate,
        $priceCurrency,
        $formFactory,
        $dataObjectProcessor,
        $directoryHelper,
        $jsonEncoder,
        $customerFormFactory,
        $options,
        $addressHelper,
        $addressService,
        $criteriaBuilder,
        $filterBuilder,
        $addressMapper,
        $data
     );
  }

  public function getAddressCollection()
   {
    //echo $this->getCustomerId();exit;
    if ($this->getCustomerId()) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($this->getCustomerId());
        $customerMasterId = $customerObj->getMasterId();
        $filter = $this->filterBuilder
            ->setField('parent_id')
            ->setValue($customerMasterId)
            ->setConditionType('eq')
            ->create();
        $this->searchCriteriaBuilder->addFilters([$filter]);
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $result = $this->addressService->getList($searchCriteria);
        return $result->getItems();
    }
    return [];
  }
}

Anywhere i am wrong? Its not overriding into my module. Please anybody help me. Thanks

8
  • Please add code of your block file. Oct 12, 2018 at 8:48
  • Yes, @RohanHapani, updated my block file Oct 12, 2018 at 8:54
  • extend this file \Magento\Sales\Block\Adminhtml\Order\Create\Form\Address Oct 12, 2018 at 8:55
  • yes, changed with that n checked, but no effect Oct 12, 2018 at 8:58
  • What do you want to change in this file? Oct 12, 2018 at 8:59

2 Answers 2

0

create 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\Form\Address" type="Vendor\Module\Block\Adminhtml\Order\Create\Form\Address" />
</config>

Then create your Address.php file :

<?php

    namespace Vendor\Module\Block\Adminhtml\Order\Create\Form;

    class Address extends \Magento\Sales\Block\Adminhtml\Order\Create\Form\Address
    {
        public function getAddressCollection()
        {
            // Do your stuff here
        }

        //write here function in which you want to add your code 
    }

Update :

You can add construct like this way. Add this below code :

public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Model\Session\Quote $sessionQuote,
        \Magento\Sales\Model\AdminOrder\Create $orderCreate,
        PriceCurrencyInterface $priceCurrency,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
        \Magento\Directory\Helper\Data $directoryHelper,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
        \Magento\Customer\Model\Options $options,
        \Magento\Customer\Helper\Address $addressHelper,
        \Magento\Customer\Api\AddressRepositoryInterface $addressService,
        \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
        \Magento\Framework\Api\FilterBuilder $filterBuilder,
        \Magento\Customer\Model\Address\Mapper $addressMapper,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $sessionQuote,
            $orderCreate,
            $priceCurrency,
            $formFactory,
            $dataObjectProcessor,
            $directoryHelper,
            $jsonEncoder,
            $customerFormFactory,
            $options,
            $addressHelper,
            $addressService,
            $criteriaBuilder,
            $filterBuilder,
            $addressMapper,
            $data
        );
}

Remove var and generated folder and check it.

8
  • I think we need to use constructor here, because we are using searchCriteriaBuilder in that function Oct 12, 2018 at 9:10
  • Check my updated answer. Oct 12, 2018 at 9:20
  • yes used above code, seems the block file is not overrided Oct 12, 2018 at 9:27
  • any system log or exception log? Oct 12, 2018 at 9:28
  • yes checked no, errors in log file Oct 12, 2018 at 9:31
0

When overriding a class, you must use the same class after extends in your block file, which you have used in preference in the di.xml file.

Since you have;

<preference for="Magento\Sales\Block\Adminhtml\Order\Create\Form\Address" type="Vendor\Module\Block\Adminhtml\Order\Create\Form\Address" />

in di.xml, you should change:

class Address extends 
  \Magento\Sales\Block\Adminhtml\Order\Create\Form\AbstractForm

to

class Address extends 
  \Magento\Sales\Block\Adminhtml\Order\Create\Form\Address

Please change it and then check.

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.