Sfoglia il codice sorgente

地址从删除新建改为更新

chengwl 1 giorno fa
parent
commit
dbabc6d604
1 ha cambiato i file con 60 aggiunte e 61 eliminazioni
  1. 60 61
      packages/Webkul/BagistoApi/src/State/CheckoutProcessor.php

+ 60 - 61
packages/Webkul/BagistoApi/src/State/CheckoutProcessor.php

@@ -15,7 +15,6 @@ use Webkul\BagistoApi\Exception\ResourceNotFoundException;
 use Webkul\BagistoApi\Facades\CartTokenFacade;
 use Webkul\BagistoApi\Facades\TokenHeaderFacade;
 use Webkul\Checkout\Facades\Cart;
-use Webkul\Checkout\Models\CartAddress;
 use Webkul\Checkout\Repositories\CartRepository;
 use Webkul\Customer\Repositories\CustomerRepository;
 use Webkul\Paypal\Payment\SmartButton;
@@ -126,72 +125,18 @@ class CheckoutProcessor implements ProcessorInterface
                 }
             }
 
-            $billingAddress = null;
-            $shippingAddress = null;
-
-            $cart->billing_address()->delete();
-            $cart->shipping_address()->delete();
-
-            if ($input->billingFirstName || $input->billingAddress) {
-                $billingAddress = new CartAddress;
-                $billingAddress->cart_id = $cart->id;
-                $billingAddress->address_type = CartAddress::ADDRESS_TYPE_BILLING;
-                $billingAddress->first_name = $input->billingFirstName;
-                $billingAddress->last_name = $input->billingLastName;
-                $billingAddress->email = $input->billingEmail;
-                $billingAddress->company_name = $input->billingCompanyName;
-                $billingAddress->address = $input->billingAddress;
-                $billingAddress->country = $input->billingCountry;
-                $billingAddress->state = $input->billingState;
-                $billingAddress->city = $input->billingCity;
-                $billingAddress->postcode = $input->billingPostcode;
-                $billingAddress->phone = $input->billingPhoneNumber;
-                $billingAddress->use_for_shipping = $input->useForShipping;
-                $billingAddress->save();
-
-                if ($input->billingEmail && ! $cart->customer_email) {
-                    $cart->customer_email = $input->billingEmail;
-                    $cart->save();
-                }
-            }
+            cart()->setCart($cart);
+            Cart::saveAddresses($this->buildAddressPayload($input));
 
-            if ($input->useForShipping && $billingAddress !== null) {
-                $shippingAddress = new CartAddress;
-                $shippingAddress->cart_id = $cart->id;
-                $shippingAddress->address_type = CartAddress::ADDRESS_TYPE_SHIPPING;
-                $shippingAddress->first_name = $input->billingFirstName;
-                $shippingAddress->last_name = $input->billingLastName;
-                $shippingAddress->email = $input->billingEmail;
-                $shippingAddress->company_name = $input->billingCompanyName;
-                $shippingAddress->address = $input->billingAddress;
-                $shippingAddress->country = $input->billingCountry;
-                $shippingAddress->state = $input->billingState;
-                $shippingAddress->city = $input->billingCity;
-                $shippingAddress->postcode = $input->billingPostcode;
-                $shippingAddress->phone = $input->billingPhoneNumber;
-                $shippingAddress->save();
-            } elseif ($input->shippingFirstName || $input->shippingAddress) {
-                $shippingAddress = new CartAddress;
-                $shippingAddress->cart_id = $cart->id;
-                $shippingAddress->address_type = CartAddress::ADDRESS_TYPE_SHIPPING;
-                $shippingAddress->first_name = $input->shippingFirstName;
-                $shippingAddress->last_name = $input->shippingLastName;
-                $shippingAddress->email = $input->shippingEmail;
-                $shippingAddress->company_name = $input->shippingCompanyName;
-                $shippingAddress->address = $input->shippingAddress;
-                $shippingAddress->country = $input->shippingCountry;
-                $shippingAddress->state = $input->shippingState;
-                $shippingAddress->city = $input->shippingCity;
-                $shippingAddress->postcode = $input->shippingPostcode;
-                $shippingAddress->phone = $input->shippingPhoneNumber;
-                $shippingAddress->save();
-            }
+            $cart->refresh();
+            $billingAddress = $cart->billing_address;
+            $shippingAddress = $cart->shipping_address;
 
             if (! $billingAddress) {
                 throw new OperationFailedException('No billing address was provided');
             }
 
-            \Webkul\Checkout\Facades\Cart::collectTotals();
+            Cart::collectTotals();
 
             if ($cart->haveStockableItems()) {
                 \Webkul\Shipping\Facades\Shipping::collectRates();
@@ -203,6 +148,60 @@ class CheckoutProcessor implements ProcessorInterface
         }
     }
 
+    /**
+     * Build payload expected by Cart::saveAddresses from GraphQL input.
+     */
+    private function buildAddressPayload(CheckoutAddressInput $input): array
+    {
+        $payload = [
+            'billing' => [
+                'first_name'       => $input->billingFirstName,
+                'last_name'        => $input->billingLastName,
+                'email'            => $input->billingEmail,
+                'company_name'     => $input->billingCompanyName,
+                'address'          => $this->normalizeAddressLines($input->billingAddress),
+                'country'          => $input->billingCountry,
+                'state'            => $input->billingState,
+                'city'             => $input->billingCity,
+                'postcode'         => $input->billingPostcode,
+                'phone'            => $input->billingPhoneNumber,
+                'use_for_shipping' => (bool) $input->useForShipping,
+            ],
+        ];
+
+        if (! $input->useForShipping) {
+            $payload['shipping'] = [
+                'first_name'   => $input->shippingFirstName,
+                'last_name'    => $input->shippingLastName,
+                'email'        => $input->shippingEmail,
+                'company_name' => $input->shippingCompanyName,
+                'address'      => $this->normalizeAddressLines($input->shippingAddress),
+                'country'      => $input->shippingCountry,
+                'state'        => $input->shippingState,
+                'city'         => $input->shippingCity,
+                'postcode'     => $input->shippingPostcode,
+                'phone'        => $input->shippingPhoneNumber,
+            ];
+        }
+
+        return $payload;
+    }
+
+    /**
+     * Convert textarea-like address string into Cart::saveAddresses line array.
+     */
+    private function normalizeAddressLines(?string $address): array
+    {
+        if ($address === null) {
+            return [];
+        }
+
+        $lines = preg_split('/\r\n|\r|\n/', $address) ?: [];
+        $lines = array_values(array_filter(array_map('trim', $lines), static fn ($line) => $line !== ''));
+
+        return $lines ?: [''];
+    }
+
     /**
      * Save shipping method for cart.
      */