| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentCart; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentCart\App\Models\Order as CartOrder; |
| 7 |
use FluentCart\App\Models\ProductVariation as CartProductVariant; |
| 8 |
|
| 9 |
class CartHelper |
| 10 |
{ |
| 11 |
public static function getProduct($productId) |
| 12 |
{ |
| 13 |
return CartProductVariant::where('id', $productId) |
| 14 |
->whereHas('product', function ($query) { |
| 15 |
$query->whereIn('post_status', ['publish', 'private']); |
| 16 |
}) |
| 17 |
->first(); |
| 18 |
} |
| 19 |
|
| 20 |
public static function getOrder($orderId) |
| 21 |
{ |
| 22 |
return CartOrder::find($orderId); |
| 23 |
} |
| 24 |
|
| 25 |
public static function getEventProductId($calendarEvent, $duration = null) |
| 26 |
{ |
| 27 |
$paymentSettings = $calendarEvent->getPaymentSettings(); |
| 28 |
|
| 29 |
if ($calendarEvent->isMultiDurationEnabled() && Arr::get($paymentSettings, 'multi_payment_enabled') == 'yes') { |
| 30 |
if ($productId = Arr::get($paymentSettings, 'multi_payment_cart_ids.'. $duration)) { |
| 31 |
return (int) $productId; |
| 32 |
} |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
$productId = Arr::get($paymentSettings, 'cart_product_id'); |
| 37 |
if ($productId) { |
| 38 |
return (int) $productId; |
| 39 |
} |
| 40 |
|
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
public static function getProductVariationOptions($product) |
| 45 |
{ |
| 46 |
$placeholder = self::getPlaceholderImage(); |
| 47 |
|
| 48 |
$isSimple = Arr::get($product->detail, 'variation_type') == 'simple'; |
| 49 |
|
| 50 |
$options = []; |
| 51 |
foreach ($product->variants as $variant) { |
| 52 |
$thumbnail = $isSimple ? $product->thumbnail : $variant->thumbnail; |
| 53 |
$options[] = [ |
| 54 |
'value' => strval($variant->id), |
| 55 |
'title' => $variant->variation_title, |
| 56 |
'price' => $variant->formatted_total, |
| 57 |
'image' => $thumbnail ?? $placeholder |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
if (!empty($options)) { |
| 62 |
return [ |
| 63 |
'id' => $product->ID, |
| 64 |
'title' => $product->post_title, |
| 65 |
'options' => $options |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
public static function getPlaceholderImage() |
| 73 |
{ |
| 74 |
return \FluentCart\App\Vite::getAssetUrl('images/placeholder.svg'); |
| 75 |
} |
| 76 |
|
| 77 |
public static function getCartProductPrice($paymentSettings, $defaultDuration, $formatted = true) |
| 78 |
{ |
| 79 |
$productId = $paymentSettings['cart_product_id']; |
| 80 |
|
| 81 |
if (Arr::get($paymentSettings, 'multi_payment_enabled') == 'yes') { |
| 82 |
$productId = Arr::get($paymentSettings, 'multi_payment_cart_ids.' . $defaultDuration); |
| 83 |
} |
| 84 |
|
| 85 |
$price = ''; |
| 86 |
$product = self::getProduct($productId); |
| 87 |
if ($product) { |
| 88 |
if ($formatted) { |
| 89 |
$price = $product->formatted_total; |
| 90 |
} else { |
| 91 |
$price = floatval($product->item_price / 100) ?? 0; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $price; |
| 96 |
} |
| 97 |
|
| 98 |
public static function getCartProductPriceByDuration($productIds = []) |
| 99 |
{ |
| 100 |
$productPrices = []; |
| 101 |
if (empty($productIds)) { |
| 102 |
return $productPrices; |
| 103 |
} |
| 104 |
|
| 105 |
$products = CartProductVariant::whereIn('id', array_values($productIds)) |
| 106 |
->whereHas('product', function ($query) { |
| 107 |
$query->whereIn('post_status', ['publish', 'private']); |
| 108 |
}) |
| 109 |
->get() |
| 110 |
->keyBy('id'); |
| 111 |
|
| 112 |
foreach ($productIds as $duration => $productId) { |
| 113 |
if (!isset($products[$productId])) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
$product = $products[$productId]; |
| 118 |
$productPrices[$duration] = [ |
| 119 |
'value' => $product->formatted_total |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
return $productPrices; |
| 124 |
} |
| 125 |
|
| 126 |
public static function isEnabled($calendarEvent = null) |
| 127 |
{ |
| 128 |
if (!$calendarEvent) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
return $calendarEvent->isPaidEvent() && $calendarEvent->isCartEnabled(); |
| 133 |
} |
| 134 |
|
| 135 |
public static function getSuccessLog($bookingId, $order) |
| 136 |
{ |
| 137 |
return [ |
| 138 |
'booking_id' => $bookingId, |
| 139 |
'status' => 'closed', |
| 140 |
'type' => 'success', |
| 141 |
'title' => __('Cart: Booking status changed to scheduled', 'fluent-booking'), |
| 142 |
'description' => sprintf( |
| 143 |
/* translators: Notification message for the change of Woocommerce order status and booking status to scheduled. %1$s is a link to view the order, %2$s is the closing link tag */ |
| 144 |
__('Cart order status changed to paid and booking status changed to scheduled. %1$sView Order%2$s', 'fluent-booking'), |
| 145 |
'<a target="_blank" href="' . $order->getViewUrl('admin') . '">', |
| 146 |
'</a>' |
| 147 |
) |
| 148 |
]; |
| 149 |
} |
| 150 |
} |