Exemple #1
0
 /**
  * @param int $addressId
  * @return \Magento\Customer\Service\V1\Data\Address|null
  */
 public function getAddressById($addressId)
 {
     try {
         return $this->_addressService->getAddress($addressId);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         return null;
     }
 }
Exemple #2
0
 /**
  * @return string|null
  */
 public function getBillingAddressHtml()
 {
     try {
         $address = $this->_addressService->getAddress($this->getCustomer()->getDefaultBilling());
     } catch (NoSuchEntityException $e) {
         return __('The customer does not have default billing address.');
     }
     return $this->_addressHelper->getFormatTypeRenderer('html')->renderArray(AddressConverter::toFlatArray($address));
 }
 /**
  * @magentoDataFixture Magento/Customer/_files/customer.php
  * @magentoDataFixture Magento/Customer/_files/customer_address.php
  */
 public function testDeleteAddressFromCustomer()
 {
     $addressId = 1;
     // See that customer already has an address with expected addressId
     $addressDataObject = $this->_service->getAddress($addressId);
     $this->assertEquals($addressDataObject->getId(), $addressId);
     // Delete the address from the customer
     $this->_service->deleteAddress($addressId);
     // See that address is deleted
     try {
         $addressDataObject = $this->_service->getAddress($addressId);
         $this->fail("Expected NoSuchEntityException not caught");
     } catch (NoSuchEntityException $exception) {
         $this->assertEquals('No such entity with addressId = 1', $exception->getMessage());
     }
 }
Exemple #4
0
 /**
  * @magentoDataFixture Magento/Customer/_files/customer_sample.php
  */
 public function testSaveActionExistingCustomerAndExistingAddressData()
 {
     $post = array('customer_id' => '1', 'account' => array('middlename' => 'test middlename', 'group_id' => 1, 'website_id' => 1, 'firstname' => 'test firstname', 'lastname' => 'test lastname', 'email' => '*****@*****.**', 'default_shipping' => '_item1', 'new_password' => 'auto', 'sendemail_store_id' => '1', 'sendemail' => '1'), 'address' => array('1' => array('firstname' => 'update firstname', 'lastname' => 'update lastname', 'street' => array('update street'), 'city' => 'update city', 'country_id' => 'US', 'postcode' => '01001', 'telephone' => '+7000000001'), '_item1' => array('firstname' => 'new firstname', 'lastname' => 'new lastname', 'street' => array('new street'), 'city' => 'new city', 'country_id' => 'US', 'postcode' => '01001', 'telephone' => '+7000000001'), '_template_' => array('firstname' => '', 'lastname' => '', 'street' => array(), 'city' => '', 'country_id' => 'US', 'postcode' => '', 'telephone' => '')), 'subscription' => '');
     $this->getRequest()->setPost($post);
     $this->getRequest()->setParam('customer_id', 1);
     $this->dispatch('backend/customer/index/save');
     /**
      * Check that success message is set
      */
     $this->assertSessionMessages($this->equalTo(array('You saved the customer.')), \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS);
     /** @var $objectManager \Magento\TestFramework\ObjectManager */
     $objectManager = Bootstrap::getObjectManager();
     /**
      * Check that customer id set and addresses saved
      */
     $registry = $objectManager->get('Magento\\Framework\\Registry');
     $customerId = $registry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
     $customer = $this->customerAccountService->getCustomer($customerId);
     $this->assertEquals('test firstname', $customer->getFirstname());
     /**
      * Addresses should be removed by \Magento\Customer\Model\Resource\Customer::_saveAddresses during _afterSave
      * addressOne - updated
      * addressTwo - removed
      * addressThree - removed
      * _item1 - new address
      */
     $addresses = $this->customerAddressService->getAddresses($customerId);
     $this->assertEquals(2, count($addresses));
     $updatedAddress = $this->customerAddressService->getAddress(1);
     $this->assertEquals('update firstname', $updatedAddress->getFirstname());
     $newAddress = $this->customerAddressService->getDefaultShippingAddress($customerId);
     $this->assertEquals('new firstname', $newAddress->getFirstname());
     /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
     $subscriber = $objectManager->get('Magento\\Newsletter\\Model\\SubscriberFactory')->create();
     $this->assertEmpty($subscriber->getId());
     $subscriber->loadByCustomerId($customerId);
     $this->assertNotEmpty($subscriber->getId());
     $this->assertEquals(1, $subscriber->getStatus());
     $this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/key/'));
 }
Exemple #5
0
 /**
  * Prepare the layout of the address edit block.
  *
  * @return $this
  */
 protected function _prepareLayout()
 {
     parent::_prepareLayout();
     // Init address object
     if ($addressId = $this->getRequest()->getParam('id')) {
         try {
             $this->_address = $this->_addressService->getAddress($addressId);
         } catch (NoSuchEntityException $e) {
         }
     }
     if (is_null($this->_address) || !$this->_address->getId()) {
         $this->_address = $this->_addressBuilder->setPrefix($this->getCustomer()->getPrefix())->setFirstname($this->getCustomer()->getFirstname())->setMiddlename($this->getCustomer()->getMiddlename())->setLastname($this->getCustomer()->getLastname())->setSuffix($this->getCustomer()->getSuffix())->create();
     }
     if ($headBlock = $this->getLayout()->getBlock('head')) {
         $headBlock->setTitle($this->getTitle());
     }
     if ($postedData = $this->_customerSession->getAddressFormData(true)) {
         if (!empty($postedData['region_id']) || !empty($postedData['region'])) {
             $postedData['region'] = array('region_id' => $postedData['region_id'], 'region' => $postedData['region']);
         }
         $this->_address = $this->_addressBuilder->mergeDataObjectWithArray($this->_address, $postedData);
     }
     return $this;
 }
Exemple #6
0
 /**
  * Create customerAddressDataObject and save it in the Model\Quote so that it can be used to persist later.
  *
  * @param CustomerDataObject $customerDataObject
  * @param \Magento\Sales\Model\Quote\Address $quoteCustomerAddress
  * @return void
  * @throws \InvalidArgumentException
  */
 protected function _prepareCustomerAddress($customerDataObject, $quoteCustomerAddress)
 {
     // Possible that customerId is null for new customers
     $customerId = $customerDataObject->getId();
     $quoteCustomerAddress->setCustomerId($customerId);
     $customerAddressDataObject = $quoteCustomerAddress->exportCustomerAddressData();
     $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId();
     $addressType = $quoteCustomerAddress->getAddressType();
     if ($quoteAddressId) {
         /** Update existing address */
         $existingAddressDataObject = $this->_customerAddressService->getAddress($quoteAddressId);
         /** Update customer address data */
         $customerAddressDataObject = $this->_customerAddressBuilder->mergeDataObjects($existingAddressDataObject, $customerAddressDataObject);
     } elseif ($addressType == CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING) {
         try {
             $billingAddressDataObject = $this->_customerAddressService->getDefaultBillingAddress($customerId);
         } catch (\Exception $e) {
             /** Billing address does not exist. */
         }
         $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling();
         if (isset($billingAddressDataObject) && $isShippingAsBilling) {
             /** Set existing billing address as default shipping */
             $customerAddressDataObject = $this->_customerAddressBuilder->populate($billingAddressDataObject)->setDefaultShipping(true)->create();
         }
     }
     switch ($addressType) {
         case CustomerAddressDataObject::ADDRESS_TYPE_BILLING:
             if (is_null($customerDataObject->getDefaultBilling())) {
                 $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultBilling(true)->create();
             }
             break;
         case CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING:
             if (is_null($customerDataObject->getDefaultShipping())) {
                 $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultShipping(true)->create();
             }
             break;
         default:
             throw new \InvalidArgumentException('Customer address type is invalid.');
     }
     $this->getQuote()->addCustomerAddressData($customerAddressDataObject);
 }
 /**
  * Reimport customer billing address to quote
  *
  * @param int $addressId customer address id
  * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
  */
 public function setQuoteCustomerBillingAddress($addressId)
 {
     try {
         $address = $this->_customerAddressService->getAddress($addressId);
     } catch (\Exception $e) {
     }
     if (isset($address)) {
         $this->getQuote()->getBillingAddress($addressId)->importCustomerAddressData($address)->collectTotals();
         $this->getQuote()->collectTotals()->save();
     }
     return $this;
 }
Exemple #8
0
 /**
  * Save checkout shipping address
  *
  * @param   array $data
  * @param   int $customerAddressId
  * @return  array
  */
 public function saveShipping($data, $customerAddressId)
 {
     if (empty($data)) {
         return array('error' => -1, 'message' => __('Invalid data'));
     }
     $address = $this->getQuote()->getShippingAddress();
     $addressForm = $this->_formFactory->create('customer_address', 'customer_address_edit', array(), $this->_request->isAjax(), Form::IGNORE_INVISIBLE, array());
     if (!empty($customerAddressId)) {
         $addressData = null;
         try {
             $addressData = $this->_customerAddressService->getAddress($customerAddressId);
         } catch (NoSuchEntityException $e) {
             // do nothing if customer is not found by id
         }
         if ($addressData->getCustomerId() != $this->getQuote()->getCustomerId()) {
             return array('error' => 1, 'message' => __('The customer address is not valid.'));
         }
         $address->importCustomerAddressData($addressData)->setSaveInAddressBook(0);
         $addressErrors = $addressForm->validateData($address->getData());
         if ($addressErrors !== true) {
             return array('error' => 1, 'message' => $addressErrors);
         }
     } else {
         // emulate request object
         $addressData = $addressForm->extractData($addressForm->prepareRequest($data));
         $addressErrors = $addressForm->validateData($addressData);
         if ($addressErrors !== true) {
             return array('error' => 1, 'message' => $addressErrors);
         }
         $compactedData = $addressForm->compactData($addressData);
         // unset shipping address attributes which were not shown in form
         foreach ($addressForm->getAttributes() as $attribute) {
             $attributeCode = $attribute->getAttributeCode();
             if (!isset($data[$attributeCode])) {
                 $address->setData($attributeCode, null);
             } else {
                 $address->setDataUsingMethod($attributeCode, $compactedData[$attributeCode]);
             }
         }
         $address->setCustomerAddressId(null);
         // Additional form data, not fetched by extractData (as it fetches only attributes)
         $address->setSaveInAddressBook(empty($data['save_in_address_book']) ? 0 : 1);
         $address->setSameAsBilling(empty($data['same_as_billing']) ? 0 : 1);
     }
     $address->setCollectShippingRates(true);
     if (($validateRes = $address->validate()) !== true) {
         return array('error' => 1, 'message' => $validateRes);
     }
     $this->getQuote()->collectTotals()->save();
     $this->getCheckout()->setStepData('shipping', 'complete', true)->setStepData('shipping_method', 'allow', true);
     return array();
 }