activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
3 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
carts_helper.php
148 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 ) { |
| 51 | return $cart; |
| 52 | } |
| 53 | if ( $cart->save() ) { |
| 54 | return $cart; |
| 55 | } else { |
| 56 | return null; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public static function clear_current_cart() { |
| 61 | $cart = self::get_or_create_cart(); |
| 62 | } |
| 63 | |
| 64 | public static function is_current_cart_empty() { |
| 65 | $cart = self::get_or_create_cart(); |
| 66 | $cart_items = $cart->get_items(); |
| 67 | return empty( $cart_items ); |
| 68 | } |
| 69 | |
| 70 | public static function get_or_create_cart( $persist = false ) { |
| 71 | // no cart in cookie and not in database when asked for persistent one - create one |
| 72 | if ( ! isset( self::$cart ) || ( $persist && empty( self::$cart->id ) ) ) { |
| 73 | if ( empty( self::get_cart_uuid() ) ) { |
| 74 | self::$cart = self::create_cart( $persist ); |
| 75 | } else { |
| 76 | // cookie is set, try to retrieve from DB |
| 77 | $cart = new OsCartModel(); |
| 78 | $cart = $cart->where( [ 'uuid' => self::get_cart_uuid() ] )->set_limit( 1 )->get_results_as_models(); |
| 79 | if ( ! empty( $cart ) && ! empty( $cart->id ) ) { |
| 80 | self::$cart = $cart; |
| 81 | } else { |
| 82 | self::$cart = self::create_cart( $persist ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return self::$cart; |
| 87 | } |
| 88 | |
| 89 | public static function add_item_to_cart( OsCartItemModel $item ) { |
| 90 | $cart = self::get_or_create_cart( true ); |
| 91 | if ( $cart ) { |
| 92 | $cart->add_item( $item ); |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | public static function add_bundle_to_cart( OsBundleModel $bundle ) { |
| 98 | $item = new OsCartItemModel(); |
| 99 | $item->variant = LATEPOINT_ITEM_VARIANT_BUNDLE; |
| 100 | $item->item_data = wp_json_encode( $bundle->generate_params_for_booking_form() ); |
| 101 | self::add_item_to_cart( $item ); |
| 102 | } |
| 103 | |
| 104 | public static function add_booking_to_cart( OsBookingModel $booking ) { |
| 105 | $item = new OsCartItemModel(); |
| 106 | $item->variant = LATEPOINT_ITEM_VARIANT_BOOKING; |
| 107 | $item->item_data = wp_json_encode( $booking->generate_params_for_booking_form() ); |
| 108 | return self::add_item_to_cart( $item ); |
| 109 | } |
| 110 | |
| 111 | public static function get_items_for_cart_id( $cart_id ) { |
| 112 | $cart_items = new OsCartItemModel(); |
| 113 | return $cart_items->where( [ 'cart_id' => $cart_id ] )->get_results_as_models(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | public static function get_default_payment_portion_type( $cart ) { |
| 119 | $regular_price = $cart->get_total(); |
| 120 | $deposit_price = $cart->deposit_amount_to_charge( [ 'apply_coupons' => false ] ); |
| 121 | if ( ( $regular_price == 0 ) && ( $deposit_price > 0 ) ) { |
| 122 | return LATEPOINT_PAYMENT_PORTION_DEPOSIT; |
| 123 | } else { |
| 124 | return LATEPOINT_PAYMENT_PORTION_FULL; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | public static function can_checkout(): bool { |
| 130 | $cart = self::get_or_create_cart(); |
| 131 | return ( count( $cart->get_items() ) > 0 ); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | public static function create_cart_item_from_item_data( array $cart_item_data ): OsCartItemModel { |
| 136 | $cart_item = new OsCartItemModel(); |
| 137 | $cart_item->variant = $cart_item_data['variant']; |
| 138 | $cart_item->item_data = wp_json_encode( $cart_item_data['item_data'] ); |
| 139 | |
| 140 | $cart_item->subtotal = $cart_item_data['subtotal']; |
| 141 | $cart_item->total = $cart_item_data['total']; |
| 142 | $cart_item->coupon_discount = $cart_item_data['coupon_discount']; |
| 143 | $cart_item->coupon_code = $cart_item_data['coupon_code']; |
| 144 | $cart_item->tax_total = $cart_item_data['tax_total']; |
| 145 | return $cart_item; |
| 146 | } |
| 147 | } |
| 148 |