Pergunta

I'm very to Magento, I got a task to do, When a new customer sign-in, and get to CheckOut page, user fill his/her address and and hit next button, customer new address should save...

NOTICE: If customer's address already exist or when he comes to checkOut after creating it's address everything runs OKAY.

Here is my PHP code where is get error, i get error on last line:

        $addressObject = null;
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        $customerRepository = $objectManager->create('\Magento\Customer\Api\CustomerRepositoryInterface');
        $customer = $customerRepository->getById($customerSession->getCustomer()->getId());
        $addressRepository = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');

        if ($address->getCustomerAddressId() != '')
        {
            $addressObject = $addressRepository->getById($address->getCustomerAddressId());
        }

        if(isset($customFields['shipping_account_number']) && $customFields['shipping_account_number'] != '')
        {
            $customer->setCustomAttribute('shipping_account_number',$customFields['shipping_account_number']);
        }
        else
        {
            $customer->setCustomAttribute('shipping_account_number','');
        }

        if(isset($customFields['tax_exempt']) && $customFields['tax_exempt'] == '1' && isset($customFields['tax_exempt_number']) && $customFields['tax_exempt_number'] != '')
        {
            //$customer->setCustomAttribute('tax_exempt_number', $customFields['tax_exempt_number']);
            $addressObject->setCustomAttribute('customer_tax_exempt_number', $customFields['tax_exempt_number']);
        }

        $addressRepository->save($addressObject);

Error i get:

<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function setCustomAttribute() on null in /var/www/html/shopgms.local/app/code/Known/Locate/Model/ShippingInformationManagement.php:215
Stack trace:
#0 /var/www/html/shopgms.local/vendor/magento/framework/Interception/Interceptor.php(58): Known\Locate\Model\ShippingInformationManagement-&gt;saveAddressInformation(9767, Object(Magento\Checkout\Model\ShippingInformation))
#1 /var/www/html/shopgms.local/vendor/magento/framework/Interception/Interceptor.php(138): Known\Locate\Model\ShippingInformationManagement\Interceptor-&gt;___callParent('saveAddressInfo...', Array)
#2 /var/www/html/shopgms.local/vendor/shipperhq/module-shipper/src/Plugin/Checkout/ShippingInformationPlugin.php(170): Known\Locate\Model\ShippingInformationManagement\Interceptor-&gt;Magento\Framework\Interception\{closure}(9767, Object(Magento\Checkout\Model\ShippingInformation))
#3 /var/www/html/shopgms.local/vendor/magento/framework/Interception/Interceptor.php(135): ShipperHQ\Shipper\Plugin\Checkout\ShippingInformationPl in <b>/var/www/html/shopgms.local/app/code/Known/Locate/Model/ShippingInformationManagement.php</b> on line <b>215</b><br />

{"messages":{"error":[{"code":500,"message":"Fatal Error: 'Uncaught Error: Call to a member function setCustomAttribute() on null in \/var\/www\/html\/shopgms.local\/app\/code\/Known\/Locate\/Model\/ShippingInformationManagement.php:215\nStack trace:\n#0 \/var\/www\/html\/shopgms.local\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Known\\Locate\\Model\\ShippingInformationManagement->saveAddressInformation(9767, Object(Magento\\Checkout\\Model\\ShippingInformation))\n#1 \/var\/www\/html\/shopgms.local\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Known\\Locate\\Model\\ShippingInformationManagement\\Interceptor->___callParent('saveAddressInfo...', Array)\n#2 \/var\/www\/html\/shopgms.local\/vendor\/shipperhq\/module-shipper\/src\/Plugin\/Checkout\/ShippingInformationPlugin.php(170): Known\\Locate\\Model\\ShippingInformationManagement\\Interceptor->Magento\\Framework\\Interception\\{closure}(9767, Object(Magento\\Checkout\\Model\\ShippingInformation))\n#3 \/var\/www\/html\/shopgms.local\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): ShipperHQ\\Shipper\\Plugin\\Checkout\\ShippingInformationPl' in '\/var\/www\/html\/shopgms.local\/app\/code\/Known\/Locate\/Model\/ShippingInformationManagement.php' on line 215","trace":"Trace is not available."}]}}
Foi útil?

Solução 2

Yes i solved my problem, i made an event, named: checkout_onepage_controller_success_action, and in Observer i saved my custom customer address attribute, after success i get current order object with that object i got customer address, then took my custom customer address attribute and save it.

/app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="custom-address-attr" instance="Vendor\Module\Observer\AfterPlaceObserver" />
    </event>
</config>

/app/code/Vendor/Module/Observer/AfterPlaceObserver.php

<?php
namespace CustomerAddress\Attribute\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterPlaceObserver implements ObserverInterface
{
    protected $addressRepository;

    protected $addressFactory;

    protected $checkoutSession;

    protected $orderFactory;

    protected $jsonHelper;

    protected $order;

    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,
        \Magento\Sales\Model\Order $order
    ) {
        $this->addressRepository = $addressRepository;
        $this->addressFactory = $addressFactory;
        $this->checkoutSession = $checkoutSession;
        $this->orderFactory = $orderFactory;
        $this->jsonHelper = $jsonHelper;
        $this->order = $order;
    }

    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);

    }

}

Outras dicas

$address->getCustomerAddressId() can return null and $addressObject left null.

You can try this:

    $addressObject = null;
    $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    $customerRepository = $objectManager->create('\Magento\Customer\Api\CustomerRepositoryInterface');
    $customer = $customerRepository->getById($customerSession->getCustomer()->getId());
    $addressRepository = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');

    if(isset($customFields['shipping_account_number']) && $customFields['shipping_account_number'] != '')
    {
        $customer->setCustomAttribute('shipping_account_number',$customFields['shipping_account_number']);
    }
    else
    {
        $customer->setCustomAttribute('shipping_account_number','');
    }

    if (isset($customFields['tax_exempt'])
        && $customFields['tax_exempt'] == '1'
        && isset($customFields['tax_exempt_number'])
        && $customFields['tax_exempt_number'] != ''
    ) {
        if ($address->getCustomerAddressId()) {
            $addressObject = $addressRepository->getById($address->getCustomerAddressId());
            $addressObject->setCustomAttribute('customer_tax_exempt_number', $customFields['tax_exempt_number']);
            $addressRepository->save($addressObject);
        } else {
            $address->setCustomAttribute('customer_tax_exempt_number', $customFields['tax_exempt_number']);
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top