1

I'm new to Magento just, I'm working on an event I call many files through object manager, but I know that's not a good way, I have a chunk of code just need to know how can I improve it.

CustomerAddress\Attribute\Observer

<?php
namespace CustomerAddress\Attribute\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterPlaceObserver implements ObserverInterface
{
    protected $addressRepository;

    protected $addressFactory;

    protected $checkoutSession;

    protected $orderFactory;

    protected $jsonHelper;

    public function __construct(
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->addressRepository = $addressRepository;
        $this->addressFactory = $addressFactory;
        $this->checkoutSession = $checkoutSession;
        $this->orderFactory = $orderFactory;
        $this->jsonHelper = $jsonHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $objectManager          = \Magento\Framework\App\ObjectManager::getInstance();
        // $addressRepository      = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');
        // $addressFactory         = $objectManager->create('\Magento\Customer\Model\AddressFactory');
        // $checkoutSession        = $objectManager->create('\Magento\Checkout\Model\Session');
        // $orderFactory           = $objectManager->create('\Magento\Sales\Model\OrderFactory');
        // $jsonHelper             = $objectManager->create('\Magento\Framework\Json\Helper\Data');

        $orderIncrementId       = $checkoutSession->getLastRealOrderId();
        $orderInfo              = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId);

        $bssCustomfield         = $orderInfo->getBssCustomfield();
        $jsonBssCustomfield     = $jsonHelper->jsonDecode($bssCustomfield);
        $jsonTaxExemptNumber    = $jsonBssCustomfield['tax_exempt_number']['value'];

        $addressRepository      = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');
        $addressObject          = $addressRepository->getById($orderInfo->getShippingAddress()->getCustomerAddressId());
        $addressObject->setCustomAttribute('customer_tax_exempt_number', $jsonTaxExemptNumber);
        $addressRepository->save($addressObject);
    }

}
  • 2
    why not inject in constructor? – aravind Jul 23 '19 at 14:03
1

Dependency injection

Without Object Manager Your Code is Look Like This :-

<?php
namespace CustomerAddress\Attribute\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterPlaceObserver implements ObserverInterface
{
    protected $addressRepository;

    protected $addressFactory;

    protected $checkoutSession;

    protected $order;

    protected $jsonHelper;

    public function __construct(
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order $order,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->addressRepository = $addressRepository;
        $this->addressFactory = $addressFactory;
        $this->checkoutSession = $checkoutSession;
        $this->order = $order;
        $this->jsonHelper = $jsonHelper;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderIncrementId       = $this->checkoutSession->getLastRealOrderId();
        $orderInfo              = $this->order->loadByIncrementId($orderIncrementId);

        $bssCustomfield         = $orderInfo->getBssCustomfield();
        $jsonBssCustomfield     = $this->jsonHelper->jsonDecode($bssCustomfield);
        $jsonTaxExemptNumber    = $jsonBssCustomfield['tax_exempt_number']['value'];

        $addressRepository      = $this->addressRepository;
        $addressObject          = $addressRepository->getById($orderInfo->getShippingAddress()->getCustomerAddressId());
        $addressObject->setCustomAttribute('customer_tax_exempt_number', $jsonTaxExemptNumber);
        $addressRepository->save($addressObject);
    }

}
| improve this answer | |

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.