Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AurelienLavorel/a9f3674a3774922a9fb1596367699452 to your computer and use it in GitHub Desktop.
Save AurelienLavorel/a9f3674a3774922a9fb1596367699452 to your computer and use it in GitHub Desktop.
fix empty customer id on customer cart
Index: vendor/magento/module-quote/Model/QuoteManagement.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/vendor/magento/module-quote/Model/QuoteManagement.php (date 1568972641000)
+++ src/vendor/magento/module-quote/Model/QuoteManagement.php (date 1568972665300)
@@ -543,7 +543,7 @@
}
/**
- * Prepare quote for customer order submit
+ * Prepare address for customer quote.
*
* @param Quote $quote
* @return void
@@ -563,41 +563,69 @@
if ($shipping && !$shipping->getSameAsBilling()
&& (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())
) {
- $shippingAddress = $shipping->exportCustomerAddress();
- if (!$hasDefaultShipping) {
- //Make provided address as default shipping address
- $shippingAddress->setIsDefaultShipping(true);
- $hasDefaultShipping = true;
- if (!$hasDefaultBilling && !$billing->getSaveInAddressBook()) {
- $shippingAddress->setIsDefaultBilling(true);
- $hasDefaultBilling = true;
- }
- }
- //save here new customer address
- $shippingAddress->setCustomerId($quote->getCustomerId());
- $this->addressRepository->save($shippingAddress);
- $quote->addCustomerAddress($shippingAddress);
- $shipping->setCustomerAddressData($shippingAddress);
- $this->addressesToSync[] = $shippingAddress->getId();
- $shipping->setCustomerAddressId($shippingAddress->getId());
+ if ($shipping->getQuoteId()) {
+ $shippingAddress = $shipping->exportCustomerAddress();
+ } else {
+ $defaultShipping = $this->customerRepository->getById($customer->getId())->getDefaultShipping();
+ if ($defaultShipping) {
+ try {
+ $shippingAddress = $this->addressRepository->getById($defaultShipping);
+ // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
+ } catch (LocalizedException $e) {
+ // no address
+ }
+ }
+ }
+ if (isset($shippingAddress)) {
+ if (!$hasDefaultShipping) {
+ //Make provided address as default shipping address
+ $shippingAddress->setIsDefaultShipping(true);
+ $hasDefaultShipping = true;
+ if (!$hasDefaultBilling && !$billing->getSaveInAddressBook()) {
+ $shippingAddress->setIsDefaultBilling(true);
+ $hasDefaultBilling = true;
+ }
+ }
+ //save here new customer address
+ $shippingAddress->setCustomerId($quote->getCustomerId());
+ $this->addressRepository->save($shippingAddress);
+ $quote->addCustomerAddress($shippingAddress);
+ $shipping->setCustomerAddressData($shippingAddress);
+ $this->addressesToSync[] = $shippingAddress->getId();
+ $shipping->setCustomerAddressId($shippingAddress->getId());
+ }
}
if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
- $billingAddress = $billing->exportCustomerAddress();
- if (!$hasDefaultBilling) {
- //Make provided address as default shipping address
- if (!$hasDefaultShipping) {
- //Make provided address as default shipping address
- $billingAddress->setIsDefaultShipping(true);
- }
- $billingAddress->setIsDefaultBilling(true);
- }
- $billingAddress->setCustomerId($quote->getCustomerId());
- $this->addressRepository->save($billingAddress);
- $quote->addCustomerAddress($billingAddress);
- $billing->setCustomerAddressData($billingAddress);
- $this->addressesToSync[] = $billingAddress->getId();
- $billing->setCustomerAddressId($billingAddress->getId());
+ if ($billing->getQuoteId()) {
+ $billingAddress = $billing->exportCustomerAddress();
+ } else {
+ $defaultBilling = $this->customerRepository->getById($customer->getId())->getDefaultBilling();
+ if ($defaultBilling) {
+ try {
+ $billingAddress = $this->addressRepository->getById($defaultBilling);
+ // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
+ } catch (LocalizedException $e) {
+ // no address
+ }
+ }
+ }
+ if (isset($billingAddress)) {
+ if (!$hasDefaultBilling) {
+ //Make provided address as default shipping address
+ if (!$hasDefaultShipping) {
+ //Make provided address as default shipping address
+ $billingAddress->setIsDefaultShipping(true);
+ }
+ $billingAddress->setIsDefaultBilling(true);
+ }
+ $billingAddress->setCustomerId($quote->getCustomerId());
+ $this->addressRepository->save($billingAddress);
+ $quote->addCustomerAddress($billingAddress);
+ $billing->setCustomerAddressData($billingAddress);
+ $this->addressesToSync[] = $billingAddress->getId();
+ $billing->setCustomerAddressId($billingAddress->getId());
+ }
}
if ($shipping && !$shipping->getCustomerId() && !$hasDefaultBilling) {
$shipping->setIsDefaultBilling(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment