示例#1
0
 /**
  * Add address to customer during create account
  *
  * @return \Magento\Customer\Service\V1\Data\Address|null
  */
 protected function _extractAddress()
 {
     if (!$this->getRequest()->getPost('create_address')) {
         return null;
     }
     $addressForm = $this->_formFactory->create('customer_address', 'customer_register_address');
     $allowedAttributes = $addressForm->getAllowedAttributes();
     $addressData = array();
     foreach ($allowedAttributes as $attribute) {
         $attributeCode = $attribute->getAttributeCode();
         $value = $this->getRequest()->getParam($attributeCode);
         if (is_null($value)) {
             continue;
         }
         switch ($attributeCode) {
             case 'region_id':
                 $this->_regionBuilder->setRegionId($value);
                 break;
             case 'region':
                 $this->_regionBuilder->setRegion($value);
                 break;
             default:
                 $addressData[$attributeCode] = $value;
         }
     }
     $this->_addressBuilder->populateWithArray($addressData);
     $this->_addressBuilder->setRegion($this->_regionBuilder->create());
     $this->_addressBuilder->setDefaultBilling($this->getRequest()->getParam('default_billing', false))->setDefaultShipping($this->getRequest()->getParam('default_shipping', false));
     return $this->_addressBuilder->create();
 }
示例#2
0
 /**
  * Prepare quote for customer order submit
  *
  * @return void
  */
 protected function _prepareCustomerQuote()
 {
     $quote = $this->getQuote();
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     $customer = $this->_customerAccountService->getCustomer($this->getCustomerSession()->getCustomerId());
     $hasDefaultBilling = (bool) $customer->getDefaultBilling();
     $hasDefaultShipping = (bool) $customer->getDefaultShipping();
     if ($shipping && !$shipping->getSameAsBilling() && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())) {
         $shippingAddress = $shipping->exportCustomerAddressData();
         if (!$hasDefaultShipping) {
             //Make provided address as default shipping address
             $shippingAddress = $this->_addressBuilder->populate($shippingAddress)->setDefaultShipping(true)->create();
             $hasDefaultShipping = true;
         }
         $quote->addCustomerAddressData($shippingAddress);
         $shipping->setCustomerAddressData($shippingAddress);
     }
     if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
         $billingAddress = $billing->exportCustomerAddressData();
         if (!$hasDefaultBilling) {
             //Make provided address as default shipping address
             $this->_addressBuilder->populate($billingAddress);
             if (!$hasDefaultShipping) {
                 //Make provided address as default shipping address
                 $this->_addressBuilder->setDefaultShipping(true);
             }
             $this->_addressBuilder->setDefaultBilling(true);
             $billingAddress = $this->_addressBuilder->create();
         }
         $quote->addCustomerAddressData($billingAddress);
         $billing->setCustomerAddressData($billingAddress);
     }
 }