activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
9 months ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
9 months ago
carts_helper.php
1 year ago
connector_helper.php
1 year ago
csv_helper.php
9 months ago
customer_helper.php
9 months ago
customer_import_helper.php
9 months ago
database_helper.php
9 months ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
9 months ago
encrypt_helper.php
1 year ago
events_helper.php
1 year ago
form_helper.php
9 months ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
9 months ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
9 months ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
9 months ago
order_intent_helper.php
9 months ago
orders_helper.php
1 year ago
otp_helper.php
9 months ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
9 months ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
9 months ago
short_links_systems_helper.php
9 months ago
shortcodes_helper.php
9 months ago
sms_helper.php
1 year ago
steps_helper.php
9 months ago
stripe_connect_helper.php
9 months ago
styles_helper.php
9 months ago
support_topics_helper.php
1 year ago
time_helper.php
9 months ago
timeline_helper.php
9 months ago
transaction_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
9 months ago
version_specific_updates_helper.php
9 months ago
whatsapp_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
carts_helper.php
146 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2023 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsCartsHelper { |
| 7 | public static $cart; |
| 8 | |
| 9 | public static function reset_cart(){ |
| 10 | unset($_COOKIE[LATEPOINT_CART_COOKIE]); |
| 11 | self::$cart = self::create_cart(); |
| 12 | } |
| 13 | |
| 14 | public static function get_cart_uuid(){ |
| 15 | if(isset($_COOKIE[LATEPOINT_CART_COOKIE])){ |
| 16 | return sanitize_text_field( wp_unslash($_COOKIE[LATEPOINT_CART_COOKIE])); |
| 17 | }else{ |
| 18 | return false; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | public static function can_checkout_multiple_items() : bool{ |
| 23 | $can = apply_filters('latepoint_can_checkout_multiple_items', false); |
| 24 | if($can){ |
| 25 | $force_disabled = OsSettingsHelper::is_on('disable_checkout_multiple_items'); |
| 26 | if($force_disabled){ |
| 27 | return false; |
| 28 | }else{ |
| 29 | return true; |
| 30 | } |
| 31 | }else{ |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public static function get_cart_id(){ |
| 37 | $cart = self::get_or_create_cart(); |
| 38 | return $cart->id; |
| 39 | } |
| 40 | |
| 41 | public static function create_cart($persist = false){ |
| 42 | $cart = new OsCartModel(); |
| 43 | if(self::get_cart_uuid()){ |
| 44 | $cart->uuid = self::get_cart_uuid(); |
| 45 | }else{ |
| 46 | $cart->uuid = OsUtilHelper::generate_uuid(); |
| 47 | OsSessionsHelper::setcookie(LATEPOINT_CART_COOKIE, $cart->uuid); |
| 48 | $_COOKIE[LATEPOINT_CART_COOKIE] = $cart->uuid; |
| 49 | } |
| 50 | if(!$persist) return $cart; |
| 51 | if($cart->save()){ |
| 52 | return $cart; |
| 53 | }else{ |
| 54 | return null; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public static function clear_current_cart(){ |
| 59 | $cart = self::get_or_create_cart(); |
| 60 | } |
| 61 | |
| 62 | public static function is_current_cart_empty(){ |
| 63 | $cart = self::get_or_create_cart(); |
| 64 | $cart_items = $cart->get_items(); |
| 65 | return empty($cart_items); |
| 66 | } |
| 67 | |
| 68 | public static function get_or_create_cart($persist = false){ |
| 69 | // no cart in cookie and not in database when asked for persistent one - create one |
| 70 | if(!isset(self::$cart) || ($persist && empty(self::$cart->id))){ |
| 71 | if(empty(self::get_cart_uuid())){ |
| 72 | self::$cart = self::create_cart($persist); |
| 73 | }else{ |
| 74 | // cookie is set, try to retrieve from DB |
| 75 | $cart = new OsCartModel(); |
| 76 | $cart = $cart->where(['uuid' => self::get_cart_uuid()])->set_limit(1)->get_results_as_models(); |
| 77 | if(!empty($cart) && !empty($cart->id)){ |
| 78 | self::$cart = $cart; |
| 79 | }else{ |
| 80 | self::$cart = self::create_cart($persist); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return self::$cart; |
| 85 | } |
| 86 | |
| 87 | public static function add_item_to_cart(OsCartItemModel $item){ |
| 88 | $cart = self::get_or_create_cart(true); |
| 89 | if($cart){ |
| 90 | $cart->add_item($item); |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | public static function add_bundle_to_cart(OsBundleModel $bundle) { |
| 96 | $item = new OsCartItemModel(); |
| 97 | $item->variant = LATEPOINT_ITEM_VARIANT_BUNDLE; |
| 98 | $item->item_data = wp_json_encode($bundle->generate_params_for_booking_form()); |
| 99 | self::add_item_to_cart($item); |
| 100 | } |
| 101 | |
| 102 | public static function add_booking_to_cart(OsBookingModel $booking) { |
| 103 | $item = new OsCartItemModel(); |
| 104 | $item->variant = LATEPOINT_ITEM_VARIANT_BOOKING; |
| 105 | $item->item_data = wp_json_encode($booking->generate_params_for_booking_form()); |
| 106 | return self::add_item_to_cart($item); |
| 107 | } |
| 108 | |
| 109 | public static function get_items_for_cart_id($cart_id) { |
| 110 | $cart_items = new OsCartItemModel(); |
| 111 | return $cart_items->where(['cart_id' => $cart_id])->get_results_as_models(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | public static function get_default_payment_portion_type($cart) { |
| 117 | $regular_price = $cart->get_total(); |
| 118 | $deposit_price = $cart->deposit_amount_to_charge(['apply_coupons' => false]); |
| 119 | if (($regular_price == 0) && ($deposit_price > 0)) { |
| 120 | return LATEPOINT_PAYMENT_PORTION_DEPOSIT; |
| 121 | } else { |
| 122 | return LATEPOINT_PAYMENT_PORTION_FULL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | public static function can_checkout(): bool { |
| 128 | $cart = self::get_or_create_cart(); |
| 129 | return (count($cart->get_items()) > 0); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | public static function create_cart_item_from_item_data(array $cart_item_data): OsCartItemModel { |
| 134 | $cart_item = new OsCartItemModel(); |
| 135 | $cart_item->variant = $cart_item_data['variant']; |
| 136 | $cart_item->item_data = wp_json_encode($cart_item_data['item_data']); |
| 137 | |
| 138 | $cart_item->subtotal = $cart_item_data['subtotal']; |
| 139 | $cart_item->total = $cart_item_data['total']; |
| 140 | $cart_item->coupon_discount = $cart_item_data['coupon_discount']; |
| 141 | $cart_item->coupon_code = $cart_item_data['coupon_code']; |
| 142 | $cart_item->tax_total = $cart_item_data['tax_total']; |
| 143 | return $cart_item; |
| 144 | } |
| 145 | |
| 146 | } |