Esempio n. 1
0
 /**
  * Prepare Form and add elements to form
  *
  * @return $this
  */
 protected function _prepareForm()
 {
     /** @var \Magento\Customer\Model\Metadata\Form $customerForm */
     $customerForm = $this->_metadataFormFactory->create('customer', 'adminhtml_checkout');
     // prepare customer attributes to show
     $attributes = [];
     // add system required attributes
     foreach ($customerForm->getSystemAttributes() as $attribute) {
         if ($attribute->isRequired()) {
             $attributes[$attribute->getAttributeCode()] = $attribute;
         }
     }
     if ($this->getQuote()->getCustomerIsGuest()) {
         unset($attributes['group_id']);
     }
     // add user defined attributes
     foreach ($customerForm->getUserAttributes() as $attribute) {
         $attributes[$attribute->getAttributeCode()] = $attribute;
     }
     $fieldset = $this->_form->addFieldset('main', []);
     $this->_addAttributesToForm($attributes, $fieldset);
     $this->_form->addFieldNameSuffix('order[account]');
     $this->_form->setValues($this->getFormValues());
     return $this;
 }
Esempio n. 2
0
 /**
  * @param string $formCode
  * @param RequestInterface $request
  * @return \Magento\Customer\Service\V1\Data\Customer
  */
 public function extract($formCode, RequestInterface $request)
 {
     $customerForm = $this->formFactory->create('customer', $formCode);
     $allowedAttributes = $customerForm->getAllowedAttributes();
     $isGroupIdEmpty = true;
     $customerData = array();
     foreach ($allowedAttributes as $attribute) {
         // confirmation in request param is the repeated password, not a confirmation code.
         if ($attribute === 'confirmation') {
             continue;
         }
         $attributeCode = $attribute->getAttributeCode();
         if ($attributeCode == 'group_id') {
             $isGroupIdEmpty = false;
         }
         $customerData[$attributeCode] = $request->getParam($attributeCode);
     }
     $this->customerBuilder->populateWithArray($customerData);
     $store = $this->storeManager->getStore();
     if ($isGroupIdEmpty) {
         $this->customerBuilder->setGroupId($this->groupService->getDefaultGroup($store->getId())->getId());
     }
     $this->customerBuilder->setWebsiteId($store->getWebsiteId());
     $this->customerBuilder->setStoreId($store->getId());
     return $this->customerBuilder->create();
 }
Esempio n. 3
0
 /**
  * Initialize customer form
  *
  * @return \Magento\Customer\Model\Metadata\Form $customerForm
  */
 protected function _getCustomerForm()
 {
     if (is_null($this->_customerForm)) {
         $this->_customerForm = $this->_customerFormFactory->create('customer', 'adminhtml_customer', DataObjectConverter::toFlatArray($this->_getCustomerDataObject()));
     }
     return $this->_customerForm;
 }
Esempio n. 4
0
 /**
  * Add address to customer during create account
  *
  * @return AddressInterface|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 = [];
     $regionDataObject = $this->regionDataFactory->create();
     foreach ($allowedAttributes as $attribute) {
         $attributeCode = $attribute->getAttributeCode();
         $value = $this->getRequest()->getParam($attributeCode);
         if ($value === null) {
             continue;
         }
         switch ($attributeCode) {
             case 'region_id':
                 $regionDataObject->setRegionId($value);
                 break;
             case 'region':
                 $regionDataObject->setRegion($value);
                 break;
             default:
                 $addressData[$attributeCode] = $value;
         }
     }
     $addressDataObject = $this->addressDataFactory->create();
     $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
     $addressDataObject->setRegion($regionDataObject);
     $addressDataObject->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
     return $addressDataObject;
 }
 /**
  * @param string $formCode
  * @param RequestInterface $request
  * @return \Magento\Customer\Api\Data\CustomerInterface
  */
 public function extract($formCode, RequestInterface $request)
 {
     $customerForm = $this->formFactory->create('customer', $formCode);
     $customerData = $customerForm->extractData($request);
     $allowedAttributes = $customerForm->getAllowedAttributes();
     $isGroupIdEmpty = isset($allowedAttributes['group_id']);
     $customerDataObject = $this->customerFactory->create();
     $this->dataObjectHelper->populateWithArray($customerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $store = $this->storeManager->getStore();
     if ($isGroupIdEmpty) {
         $customerDataObject->setGroupId($this->customerGroupManagement->getDefaultGroup($store->getId())->getId());
     }
     $customerDataObject->setWebsiteId($store->getWebsiteId());
     $customerDataObject->setStoreId($store->getId());
     return $customerDataObject;
 }
Esempio n. 6
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();
 }
Esempio n. 7
0
 /**
  * Initialize customer form
  *
  * @return \Magento\Customer\Model\Metadata\Form $customerForm
  */
 protected function _getCustomerForm()
 {
     if ($this->_customerForm === null) {
         $this->_customerForm = $this->_customerFormFactory->create('customer', 'adminhtml_customer', $this->_extensibleDataObjectConverter->toFlatArray($this->_getCustomerDataObject()));
     }
     return $this->_customerForm;
 }
Esempio n. 8
0
 /**
  * Prepare Form and add elements to form
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _prepareForm()
 {
     $fieldset = $this->_form->addFieldset('main', array('no_container' => true));
     $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address');
     $attributes = $addressForm->getAttributes();
     $this->_addAttributesToForm($attributes, $fieldset);
     $prefixElement = $this->_form->getElement('prefix');
     if ($prefixElement) {
         $prefixOptions = $this->_customerHelper->getNamePrefixOptions($this->getStore());
         if (!empty($prefixOptions)) {
             $fieldset->removeField($prefixElement->getId());
             $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^');
             $prefixField->setValues($prefixOptions);
             if ($this->getAddressId()) {
                 $prefixField->addElementValues($this->getAddress()->getPrefix());
             }
         }
     }
     $suffixElement = $this->_form->getElement('suffix');
     if ($suffixElement) {
         $suffixOptions = $this->_customerHelper->getNameSuffixOptions($this->getStore());
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $this->_form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
             if ($this->getAddressId()) {
                 $suffixField->addElementValues($this->getAddress()->getSuffix());
             }
         }
     }
     $regionElement = $this->_form->getElement('region_id');
     if ($regionElement) {
         $regionElement->setNoDisplay(true);
     }
     $this->_form->setValues($this->getFormValues());
     if ($this->_form->getElement('country_id')->getValue()) {
         $countryId = $this->_form->getElement('country_id')->getValue();
         $this->_form->getElement('country_id')->setValue(null);
         foreach ($this->_form->getElement('country_id')->getValues() as $country) {
             if ($country['value'] == $countryId) {
                 $this->_form->getElement('country_id')->setValue($countryId);
             }
         }
     }
     if (is_null($this->_form->getElement('country_id')->getValue())) {
         $this->_form->getElement('country_id')->setValue($this->_coreData->getDefaultCountry($this->getStore()));
     }
     // Set custom renderer for VAT field if needed
     $vatIdElement = $this->_form->getElement('vat_id');
     if ($vatIdElement && $this->getDisplayVatValidationButton() !== false) {
         $vatIdElement->setRenderer($this->getLayout()->createBlock('Magento\\Customer\\Block\\Adminhtml\\Sales\\Order\\Address\\Form\\Renderer\\Vat')->setJsVariablePrefix($this->getJsVariablePrefix()));
     }
     return $this;
 }
Esempio n. 9
0
 /**
  * Perform customer data filtration based on form code and form object
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @param string $formCode The code of EAV form to take the list of attributes from
  * @param string $entityType entity type for the form
  * @param string[] $additionalAttributes The list of attribute codes to skip filtration for
  * @param string $scope scope of the request
  * @param \Magento\Customer\Model\Metadata\Form $metadataForm to use for extraction
  * @return array Filtered customer data
  */
 protected function _extractData(\Magento\Framework\App\RequestInterface $request, $formCode, $entityType, $additionalAttributes = [], $scope = null, \Magento\Customer\Model\Metadata\Form $metadataForm = null)
 {
     if ($metadataForm === null) {
         $metadataForm = $this->_formFactory->create($entityType, $formCode, [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE);
     }
     $filteredData = $metadataForm->extractData($request, $scope);
     $object = $this->_objectFactory->create(['data' => $request->getPostValue()]);
     $requestData = $object->getData($scope);
     foreach ($additionalAttributes as $attributeCode) {
         $filteredData[$attributeCode] = isset($requestData[$attributeCode]) ? $requestData[$attributeCode] : false;
     }
     $formAttributes = $metadataForm->getAttributes();
     /** @var \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute */
     foreach ($formAttributes as $attribute) {
         $attributeCode = $attribute->getAttributeCode();
         $frontendInput = $attribute->getFrontendInput();
         if ($frontendInput != 'boolean' && $filteredData[$attributeCode] === false) {
             unset($filteredData[$attributeCode]);
         }
     }
     return $filteredData;
 }
Esempio n. 10
0
 /**
  * Get order data jason
  *
  * @return string
  */
 public function getOrderDataJson()
 {
     $data = array();
     if ($this->getCustomerId()) {
         $data['customer_id'] = $this->getCustomerId();
         $data['addresses'] = array();
         $addresses = $this->_addressService->getAddresses($this->getCustomerId());
         foreach ($addresses as $addressData) {
             $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($addressData));
             $data['addresses'][$addressData->getId()] = $addressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON);
         }
     }
     if (!is_null($this->getStoreId())) {
         $data['store_id'] = $this->getStoreId();
         $currency = $this->_localeCurrency->getCurrency($this->getStore()->getCurrentCurrencyCode());
         $symbol = $currency->getSymbol() ? $currency->getSymbol() : $currency->getShortName();
         $data['currency_symbol'] = $symbol;
         $data['shipping_method_reseted'] = !(bool) $this->getQuote()->getShippingAddress()->getShippingMethod();
         $data['payment_method'] = $this->getQuote()->getPayment()->getMethod();
     }
     return $this->_jsonEncoder->encode($data);
 }
Esempio n. 11
0
 /**
  * Save checkout shipping address
  *
  * @param   array $data
  * @param   int $customerAddressId
  * @return  array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function saveShipping($data, $customerAddressId)
 {
     if (empty($data)) {
         return ['error' => -1, 'message' => __('Invalid data')];
     }
     $address = $this->getQuote()->getShippingAddress();
     $addressForm = $this->_formFactory->create('customer_address', 'customer_address_edit', [], $this->_request->isAjax(), Form::IGNORE_INVISIBLE, []);
     if (!empty($customerAddressId)) {
         $addressData = null;
         try {
             $addressData = $this->addressRepository->getById($customerAddressId);
         } catch (NoSuchEntityException $e) {
             // do nothing if customer is not found by id
         }
         if ($addressData->getCustomerId() != $this->getQuote()->getCustomerId()) {
             return ['error' => 1, 'message' => __('The customer address is not valid.')];
         }
         $address->importCustomerAddressData($addressData)->setSaveInAddressBook(0);
         $addressErrors = $addressForm->validateData($address->getData());
         if ($addressErrors !== true) {
             return ['error' => 1, 'message' => $addressErrors];
         }
     } else {
         // emulate request object
         $addressData = $addressForm->extractData($addressForm->prepareRequest($data));
         $addressErrors = $addressForm->validateData($addressData);
         if ($addressErrors !== true) {
             return ['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 ['error' => 1, 'message' => $validateRes];
     }
     $address->collectTotals()->save();
     $this->getCheckout()->setStepData('shipping', 'complete', true)->setStepData('shipping_method', 'allow', true);
     return [];
 }
Esempio n. 12
0
 /**
  * Set and validate Quote address
  * All errors added to _errors
  *
  * @param \Magento\Quote\Model\Quote\Address $address
  * @param array $data
  * @return $this
  */
 protected function _setQuoteAddress(\Magento\Quote\Model\Quote\Address $address, array $data)
 {
     $isAjax = !$this->getIsValidate();
     // Region is a Data Object, so it is represented by an array. validateData() doesn't understand arrays, so we
     // need to merge region data with address data. This is going to be removed when we switch to use address Data
     // Object instead of the address model.
     // Note: if we use getRegion() here it will pull region from db using the region_id
     $data = isset($data['region']) && is_array($data['region']) ? array_merge($data, $data['region']) : $data;
     $addressForm = $this->_metadataFormFactory->create(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'adminhtml_customer_address', $data, $isAjax, CustomerForm::DONT_IGNORE_INVISIBLE, []);
     // prepare request
     // save original request structure for files
     if ($address->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
         $requestData = ['order' => ['shipping_address' => $data]];
         $requestScope = 'order/shipping_address';
     } else {
         $requestData = ['order' => ['billing_address' => $data]];
         $requestScope = 'order/billing_address';
     }
     $request = $addressForm->prepareRequest($requestData);
     $addressData = $addressForm->extractData($request, $requestScope);
     if ($this->getIsValidate()) {
         $errors = $addressForm->validateData($addressData);
         if ($errors !== true) {
             if ($address->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
                 $typeName = __('Shipping Address: ');
             } else {
                 $typeName = __('Billing Address: ');
             }
             foreach ($errors as $error) {
                 $this->_errors[] = $typeName . $error;
             }
             $address->setData($addressForm->restoreData($addressData));
         } else {
             $address->setData($addressForm->compactData($addressData));
         }
     } else {
         $address->addData($addressForm->restoreData($addressData));
     }
     return $this;
 }
Esempio n. 13
0
 /**
  * Initialize form object
  *
  * @return $this
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function initForm()
 {
     $customerData = $this->_backendSession->getCustomerData();
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $fieldset = $form->addFieldset('address_fieldset', ['legend' => __("Edit Customer's Address")]);
     $account = $customerData['account'];
     $address = $this->addressDataFactory->create();
     if (!empty($account) && isset($account['store_id'])) {
         $address->setCountryId($this->_directoryHelper->getDefaultCountry($this->_storeManager->getStore($account['store_id'])));
     } else {
         $address->setCountryId($this->_directoryHelper->getDefaultCountry());
     }
     $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', $this->addressMapper->toFlatArray($address));
     $attributes = $addressForm->getAttributes();
     if (isset($attributes['street'])) {
         if ($attributes['street']->getMultilineCount() <= 0) {
             $attributes['street']->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT);
         }
     }
     foreach ($attributes as $key => $attribute) {
         $attributes[$key]->setFrontendLabel(__($attribute->getFrontendLabel()))->setIsVisible(false);
     }
     $this->_setFieldset($attributes, $fieldset);
     $regionElement = $form->getElement('region');
     if ($regionElement) {
         $regionElement->setRenderer($this->_regionFactory->create());
     }
     $regionElement = $form->getElement('region_id');
     if ($regionElement) {
         $regionElement->setNoDisplay(true);
     }
     $country = $form->getElement('country_id');
     if ($country) {
         $country->addClass('countries');
     }
     $postcode = $form->getElement('postcode');
     if ($postcode) {
         $postcode->removeClass('required-entry')->setRequired(!$this->_directoryHelper->isZipCodeOptional($address->getCountryId()));
     }
     if ($this->isReadonly()) {
         foreach ($this->_addressMetadataService->getAllAttributesMetadata() as $attribute) {
             $element = $form->getElement($attribute->getAttributeCode());
             if ($element) {
                 $element->setReadonly(true, true);
             }
         }
     }
     $customerStoreId = null;
     if (!empty($account) && isset($account['id']) && isset($account['website_id'])) {
         $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId();
     }
     $prefixElement = $form->getElement('prefix');
     if ($prefixElement) {
         $prefixOptions = $this->options->getNamePrefixOptions($customerStoreId);
         if (!empty($prefixOptions)) {
             $fieldset->removeField($prefixElement->getId());
             $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^');
             $prefixField->setValues($prefixOptions);
         }
     }
     $suffixElement = $form->getElement('suffix');
     if ($suffixElement) {
         $suffixOptions = $this->options->getNameSuffixOptions($customerStoreId);
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
         }
     }
     $customerDataObject = $this->customerDataFactory->create();
     $this->dataObjectHelper->populateWithArray($customerDataObject, $account, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $this->assign('customer', $customerDataObject);
     $addressCollection = [];
     foreach ($customerData['address'] as $key => $addressData) {
         $addressDataObject = $this->addressDataFactory->create();
         $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
         $addressCollection[$key] = $addressDataObject;
     }
     $this->assign('addressCollection', $addressCollection);
     $form->setValues($this->addressMapper->toFlatArray($address));
     $this->setForm($form);
     return $this;
 }
Esempio n. 14
0
 /**
  * Perform customer data filtration based on form code and form object
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @param string $formCode The code of EAV form to take the list of attributes from
  * @param string $entityType entity type for the form
  * @param string[] $additionalAttributes The list of attribute codes to skip filtration for
  * @param string $scope scope of the request
  * @param \Magento\Customer\Model\Metadata\Form $metadataForm to use for extraction
  * @return array Filtered customer data
  */
 public function extractCustomerData(\Magento\Framework\App\RequestInterface $request, $formCode, $entityType, $additionalAttributes = array(), $scope = null, \Magento\Customer\Model\Metadata\Form $metadataForm = null)
 {
     if (is_null($metadataForm)) {
         $metadataForm = $this->_formFactory->create($entityType, $formCode, array(), false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE);
     }
     $filteredData = $metadataForm->extractData($request, $scope);
     $requestData = $request->getPost($scope);
     foreach ($additionalAttributes as $attributeCode) {
         $filteredData[$attributeCode] = isset($requestData[$attributeCode]) ? $requestData[$attributeCode] : false;
     }
     $formAttributes = $metadataForm->getAttributes();
     /** @var \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata $attribute */
     foreach ($formAttributes as $attribute) {
         $attributeCode = $attribute->getAttributeCode();
         $frontendInput = $attribute->getFrontendInput();
         if ($frontendInput != 'boolean' && $filteredData[$attributeCode] === false) {
             unset($filteredData[$attributeCode]);
         }
     }
     return $filteredData;
 }