Skip to content

Commit 58cb4c7

Browse files
author
Korshenko, Olexii(okorshenko)
committed
Merge pull request #8 from magento-south/MAGETWO-42988
[SOUTH] Bugfixes
2 parents 2ed35e6 + 7a4fa32 commit 58cb4c7

File tree

15 files changed

+218
-1099
lines changed

15 files changed

+218
-1099
lines changed

app/code/Magento/Checkout/Model/Type/Onepage.php

Lines changed: 0 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -379,149 +379,6 @@ public function saveCheckoutMethod($method)
379379
return [];
380380
}
381381

382-
/**
383-
* Save billing address information to quote
384-
* This method is called by One Page Checkout JS (AJAX) while saving the billing information.
385-
*
386-
* @param array $data
387-
* @param int $customerAddressId
388-
* @return array
389-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
390-
* @SuppressWarnings(PHPMD.NPathComplexity)
391-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
392-
*/
393-
public function saveBilling($data, $customerAddressId)
394-
{
395-
if (empty($data)) {
396-
return ['error' => -1, 'message' => __('Invalid data')];
397-
}
398-
399-
$address = $this->getQuote()->getBillingAddress();
400-
$addressForm = $this->_formFactory->create(
401-
AddressMetadata::ENTITY_TYPE_ADDRESS,
402-
'customer_address_edit',
403-
[],
404-
$this->_request->isAjax(),
405-
Form::IGNORE_INVISIBLE,
406-
[]
407-
);
408-
409-
if ($customerAddressId) {
410-
try {
411-
$customerAddress = $this->addressRepository->getById($customerAddressId);
412-
if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) {
413-
return ['error' => 1, 'message' => __('The customer address is not valid.')];
414-
}
415-
$address->importCustomerAddressData($customerAddress)->setSaveInAddressBook(0);
416-
} catch (\Exception $e) {
417-
return ['error' => 1, 'message' => __('Address does not exist.')];
418-
}
419-
} else {
420-
// emulate request object
421-
$addressData = $addressForm->extractData($addressForm->prepareRequest($data));
422-
$addressErrors = $addressForm->validateData($addressData);
423-
if ($addressErrors !== true) {
424-
return ['error' => 1, 'message' => array_values($addressErrors)];
425-
}
426-
$address->addData($addressForm->compactData($addressData));
427-
//unset billing address attributes which were not shown in form
428-
foreach ($addressForm->getAttributes() as $attribute) {
429-
if (!isset($data[$attribute->getAttributeCode()])) {
430-
$address->setData($attribute->getAttributeCode(), null);
431-
}
432-
}
433-
$address->setCustomerAddressId(null);
434-
// Additional form data, not fetched by extractData (as it fetches only attributes)
435-
$address->setSaveInAddressBook(empty($data['save_in_address_book']) ? 0 : 1);
436-
$this->getQuote()->setBillingAddress($address);
437-
}
438-
439-
// validate billing address
440-
if (($validateRes = $address->validate()) !== true) {
441-
return ['error' => 1, 'message' => $validateRes];
442-
}
443-
444-
if (true !== ($result = $this->_validateCustomerData($data))) {
445-
return $result;
446-
} else {
447-
/** Even though _validateCustomerData should not modify data, it does */
448-
$address = $this->getQuote()->getBillingAddress();
449-
}
450-
451-
if (!$this->getQuote()->getCustomerId() && $this->isCheckoutMethodRegister()) {
452-
if ($this->_customerEmailExists($address->getEmail(), $this->_storeManager->getWebsite()->getId())) {
453-
return [
454-
'error' => 1,
455-
// @codingStandardsIgnoreStart
456-
'message' => __(
457-
'This email address already belongs to a registered customer. You can sign in or create an account with a different email address.'
458-
)
459-
// @codingStandardsIgnoreEnd
460-
];
461-
}
462-
}
463-
464-
if (!$this->getQuote()->isVirtual()) {
465-
/**
466-
* Billing address using options
467-
*/
468-
$usingCase = isset($data['use_for_shipping'])
469-
? (bool)$data['use_for_shipping']
470-
: self::NOT_USE_FOR_SHIPPING;
471-
472-
switch ($usingCase) {
473-
case self::NOT_USE_FOR_SHIPPING:
474-
$shipping = $this->getQuote()->getShippingAddress();
475-
$shipping->setSameAsBilling(0);
476-
$shipping->save();
477-
break;
478-
case self::USE_FOR_SHIPPING:
479-
$billing = clone $address;
480-
$billing->unsAddressId()->unsAddressType();
481-
$shipping = $this->getQuote()->getShippingAddress();
482-
$shippingMethod = $shipping->getShippingMethod();
483-
484-
// Billing address properties that must be always copied to shipping address
485-
$requiredBillingAttributes = ['customer_address_id'];
486-
487-
// don't reset original shipping data, if it was not changed by customer
488-
foreach ($shipping->getData() as $shippingKey => $shippingValue) {
489-
if ($shippingValue !== null
490-
&& $billing->getData($shippingKey) !== null
491-
&& !isset($data[$shippingKey])
492-
&& !in_array($shippingKey, $requiredBillingAttributes)
493-
) {
494-
$billing->unsetData($shippingKey);
495-
}
496-
}
497-
$shipping->addData($billing->getData())
498-
->setSameAsBilling(1)
499-
->setSaveInAddressBook(0)
500-
->setShippingMethod($shippingMethod)
501-
->setCollectShippingRates(true);
502-
$this->totalsCollector->collectAddressTotals($this->getQuote(), $shipping);
503-
504-
if (!$this->isCheckoutMethodRegister()) {
505-
$shipping->save();
506-
}
507-
$this->getCheckout()->setStepData('shipping', 'complete', true);
508-
break;
509-
}
510-
}
511-
512-
if ($this->isCheckoutMethodRegister()) {
513-
$this->quoteRepository->save($this->getQuote());
514-
} else {
515-
$address->save();
516-
}
517-
518-
$this->getCheckout()
519-
->setStepData('billing', 'allow', true)
520-
->setStepData('billing', 'complete', true)
521-
->setStepData('shipping', 'allow', true);
522-
return [];
523-
}
524-
525382
/**
526383
* Check whether checkout method is "register"
527384
*
@@ -532,100 +389,6 @@ protected function isCheckoutMethodRegister()
532389
return $this->getQuote()->getCheckoutMethod() == self::METHOD_REGISTER;
533390
}
534391

535-
/**
536-
* Validate customer data and set some its data for further usage in quote
537-
*
538-
* Will return either true or array with error messages
539-
*
540-
* @param array $data
541-
* @return bool|array
542-
*/
543-
protected function _validateCustomerData(array $data)
544-
{
545-
$quote = $this->getQuote();
546-
$isCustomerNew = !$quote->getCustomerId();
547-
$customer = $quote->getCustomer();
548-
$customerData = $this->extensibleDataObjectConverter->toFlatArray(
549-
$customer,
550-
[],
551-
'\Magento\Customer\Api\Data\CustomerInterface'
552-
);
553-
554-
/** @var Form $customerForm */
555-
$customerForm = $this->_formFactory->create(
556-
\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
557-
'checkout_register',
558-
$customerData,
559-
$this->_request->isAjax(),
560-
Form::IGNORE_INVISIBLE,
561-
[]
562-
);
563-
564-
if ($isCustomerNew) {
565-
$customerRequest = $customerForm->prepareRequest($data);
566-
$customerData = $customerForm->extractData($customerRequest);
567-
}
568-
569-
$customerErrors = $customerForm->validateData($customerData);
570-
if ($customerErrors !== true) {
571-
return ['error' => -1, 'message' => implode(', ', $customerErrors)];
572-
}
573-
574-
if (!$isCustomerNew) {
575-
return true;
576-
}
577-
578-
$customer = $this->customerDataFactory->create();
579-
$this->dataObjectHelper->populateWithArray(
580-
$customer,
581-
$customerData,
582-
'\Magento\Customer\Api\Data\CustomerInterface'
583-
);
584-
585-
if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) {
586-
// We always have $customerRequest here, otherwise we would have been kicked off the function several
587-
// lines above
588-
$password = $customerRequest->getParam('customer_password');
589-
if ($password != $customerRequest->getParam('confirm_password')) {
590-
return [
591-
'error' => -1,
592-
'message' => __('Password and password confirmation are not equal.')
593-
];
594-
}
595-
$quote->setPasswordHash($this->accountManagement->getPasswordHash($password));
596-
} else {
597-
// set NOT LOGGED IN group id explicitly,
598-
// otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value
599-
$customer->setGroupId(GroupInterface::NOT_LOGGED_IN_ID);
600-
}
601-
602-
//validate customer
603-
$result = $this->accountManagement->validate($customer);
604-
if (!$result->isValid()) {
605-
return [
606-
'error' => -1,
607-
'message' => implode(', ', $result->getMessages())
608-
];
609-
}
610-
611-
// copy customer/guest email to address
612-
$quote->getBillingAddress()->setEmail($customer->getEmail());
613-
614-
// copy customer data to quote
615-
$this->_objectCopyService->copyFieldsetToTarget(
616-
'customer_account',
617-
'to_quote',
618-
$this->extensibleDataObjectConverter->toFlatArray(
619-
$customer,
620-
[],
621-
'\Magento\Customer\Api\Data\CustomerInterface'
622-
),
623-
$quote
624-
);
625-
626-
return true;
627-
}
628-
629392
/**
630393
* Save checkout shipping address
631394
*

0 commit comments

Comments
 (0)