filter = $filter; $this->collectionFactory = $collectionFactory; $this->addressRepository = $addressRepository; $this->logger = $logger; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } /** * Delete specified customer addresses using grid massaction * * @return Json * @throws LocalizedException */ public function execute(): Json { $customerData = $this->_session->getData('customer_data'); /** @var \Magento\Customer\Model\ResourceModel\Address\Collection $collection */ $collection = $this->filter->getCollection($this->collectionFactory->create()); $error = false; try { if ($customerData && $customerData['customer_id']) { $collection->addFieldToFilter('parent_id', $customerData['customer_id']); } else { throw new \Exception(); } $collectionSize = $collection->getSize(); /** @var \Magento\Customer\Model\Address $address */ foreach ($collection as $address) { $this->addressRepository->deleteById($address->getId()); } $message = __('A total of %1 record(s) have been deleted.', $collectionSize); } catch (NoSuchEntityException $e) { $message = __('There is no such address entity to delete.'); $error = true; $this->logger->critical($e); } catch (LocalizedException $e) { $message = __($e->getMessage()); $error = true; $this->logger->critical($e); } catch (\Exception $e) { $message = __('We can\'t mass delete the addresses right now.'); $error = true; $this->logger->critical($e); } $resultJson = $this->resultJsonFactory->create(); $resultJson->setData( [ 'message' => $message, 'error' => $error, ] ); return $resultJson; } }