addressRepository = $addressRepository; $this->logger = $logger; $this->resultJsonFactory = $resultJsonFactory; } /** * Execute action to set customer default shipping address * * @return Json */ public function execute(): Json { $customerId = $this->getRequest()->getParam('parent_id', false); $addressId = $this->getRequest()->getParam('id', false); $error = true; $message = __('There is no address id in setting default shipping address.'); if ($addressId) { try { $address = $this->addressRepository->getById($addressId)->setCustomerId($customerId); $this->setAddressAsDefault($address); $this->addressRepository->save($address); $message = __('Default shipping address has been changed.'); $error = false; } catch (\Exception $e) { $message = __('We can\'t change default shipping address right now.'); $this->logger->critical($e); } } $resultJson = $this->resultJsonFactory->create(); $resultJson->setData( [ 'message' => $message, 'error' => $error, ] ); return $resultJson; } /** * Set address as default shipping address * * @param AddressInterface $address * @return void */ private function setAddressAsDefault(AddressInterface $address): void { $address->setIsDefaultShipping(true); } }