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->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->___callParent('saveAddressInfo...', Array)
#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))
#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."}]}}
Best Answer
$address->getCustomerAddressId() can return null and $addressObject left null.
You can try this: