| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\ShortCodes\Checkout; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\CustomerResource; |
| 6 |
use FluentCart\App\Helpers\AddressHelper; |
| 7 |
use FluentCart\App\Helpers\CartHelper; |
| 8 |
use FluentCart\App\Hooks\Handlers\ShortCodes\ShortCode; |
| 9 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 10 |
use FluentCart\App\Services\Renderer\CartRenderer; |
| 11 |
use FluentCart\App\Services\Renderer\CheckoutRenderer; |
| 12 |
use FluentCart\Framework\Support\Arr; |
| 13 |
|
| 14 |
class CheckoutPageHandler extends ShortCode |
| 15 |
{ |
| 16 |
const SHORT_CODE = 'fluent_cart_checkout'; |
| 17 |
protected static string $shortCodeName = 'fluent_cart_checkout'; |
| 18 |
|
| 19 |
public static function register() |
| 20 |
{ |
| 21 |
parent::register(); |
| 22 |
} |
| 23 |
|
| 24 |
public function render(?array $viewData = null) |
| 25 |
{ |
| 26 |
|
| 27 |
$cart = CartHelper::getCart(); |
| 28 |
|
| 29 |
if (!$cart || empty($cart->cart_data)) { |
| 30 |
return (new CartRenderer())->renderEmpty(); |
| 31 |
} |
| 32 |
|
| 33 |
// Push the shipping and billing address from id |
| 34 |
$checkoutData = $cart->checkout_data; |
| 35 |
$formData = Arr::get($checkoutData, 'form_data', []); |
| 36 |
|
| 37 |
$currentCustomer = CustomerResource::getCurrentCustomer(); |
| 38 |
|
| 39 |
if ($currentCustomer && empty($formData['billing_country']) && empty($formData['billing_address_id'])) { |
| 40 |
// this is a new cart. So we should fill the address id if any |
| 41 |
$primaryBillingAddress = $currentCustomer->primary_billing_address; |
| 42 |
if ($primaryBillingAddress) { |
| 43 |
$formData['billing_address_id'] = $primaryBillingAddress->id; |
| 44 |
} else { |
| 45 |
// Case 3: no primary address — use the first available billing address |
| 46 |
$firstBillingAddress = $currentCustomer->billing_address() |
| 47 |
->where('status', 'active') |
| 48 |
->orderBy('id', 'ASC') |
| 49 |
->first(); |
| 50 |
if ($firstBillingAddress) { |
| 51 |
$formData['billing_address_id'] = $firstBillingAddress->id; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ($cart->isShipToDifferent()) { |
| 56 |
$primaryShippingAddress = $currentCustomer->primary_shipping_address; |
| 57 |
if ($primaryShippingAddress) { |
| 58 |
$formData['shipping_address_id'] = $primaryShippingAddress->id; |
| 59 |
} else { |
| 60 |
$firstShippingAddress = $currentCustomer->shipping_address() |
| 61 |
->where('status', 'active') |
| 62 |
->orderBy('id', 'ASC') |
| 63 |
->first(); |
| 64 |
if ($firstShippingAddress) { |
| 65 |
$formData['shipping_address_id'] = $firstShippingAddress->id; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$formData = AddressHelper::maybePushAddressDataForCheckout($formData, 'billing'); |
| 72 |
if ($cart->isShipToDifferent()) { |
| 73 |
$formData = AddressHelper::maybePushAddressDataForCheckout($formData, 'shipping'); |
| 74 |
} |
| 75 |
|
| 76 |
if(empty($formData['billing_country'])) { |
| 77 |
$formData['billing_country'] = AddressHelper::getDefaultBillingCountryForCheckout(); |
| 78 |
} |
| 79 |
|
| 80 |
$checkoutData['form_data'] = $formData; |
| 81 |
$cart->checkout_data = $checkoutData; |
| 82 |
$cart->save(); |
| 83 |
|
| 84 |
do_action('fluent_cart/cart/cart_data_items_updated', [ |
| 85 |
'cart' => $cart, |
| 86 |
'scope' => 'loading', |
| 87 |
'scope_data' => '' |
| 88 |
]); |
| 89 |
|
| 90 |
|
| 91 |
AssetLoader::loadCheckoutAssets($cart); |
| 92 |
|
| 93 |
(new CheckoutRenderer($cart))->render(); |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
|