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
order_intent_helper.php
201 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2024 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsOrderIntentHelper { |
| 7 | |
| 8 | public static function generate_continue_intent_url( $order_intent_key ) { |
| 9 | return OsRouterHelper::build_admin_post_link( |
| 10 | [ |
| 11 | 'orders', |
| 12 | 'continue_order_intent', |
| 13 | ], |
| 14 | [ 'order_intent_key' => $order_intent_key ] |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | public static function get_order_id_from_intent_key( $intent_key ) { |
| 19 | if ( empty( $intent_key ) ) { |
| 20 | return false; |
| 21 | } |
| 22 | $order_intent = new OsOrderIntentModel(); |
| 23 | $order_intent = $order_intent->where( [ 'intent_key' => $intent_key ] )->set_limit( 1 )->get_results_as_models(); |
| 24 | |
| 25 | if ( $order_intent && $order_intent->order_id ) { |
| 26 | return $order_intent->order_id; |
| 27 | } else { |
| 28 | return null; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public static function set_order_intent_data_from_cart( OsOrderIntentModel $order_intent, OsCartModel $cart ): OsOrderIntentModel { |
| 33 | |
| 34 | $cart_items = $cart->get_items(); |
| 35 | |
| 36 | $cart_items_data = []; |
| 37 | foreach ( $cart_items as $cart_item ) { |
| 38 | $cart_item_data = json_decode( $cart_item->item_data, true ); |
| 39 | // cart item could have been added while user was not logged in, make sure to update customer id when creating order intent |
| 40 | $cart_item_data['customer_id'] = $order_intent->customer_id; |
| 41 | |
| 42 | |
| 43 | $cart_items_data[] = [ |
| 44 | 'variant' => $cart_item->variant, |
| 45 | 'item_data' => $cart_item_data, |
| 46 | 'subtotal' => $cart_item->get_subtotal(), |
| 47 | 'total' => $cart_item->get_total(), |
| 48 | 'coupon_discount' => $cart_item->get_coupon_discount(), |
| 49 | 'coupon_code' => $cart_item->get_coupon_code(), |
| 50 | 'tax_total' => $cart_item->get_tax_total(), |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | $order_intent->cart_items_data = wp_json_encode( $cart_items_data ); |
| 55 | $order_intent->charge_amount = $cart->amount_to_charge(); |
| 56 | $order_intent->specs_charge_amount = $cart->specs_calculate_amount_to_charge(); |
| 57 | $order_intent->total = $cart->get_total(); |
| 58 | $order_intent->subtotal = $cart->get_subtotal(); |
| 59 | $order_intent->tax_total = $cart->get_tax_total(); |
| 60 | $order_intent->coupon_code = $cart->get_coupon_code(); |
| 61 | if ( ! empty( $cart->get_coupon_code() ) ) { |
| 62 | $order_intent->coupon_discount = $cart->get_coupon_discount(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // hide "payments & credits" row if we are not accepting payments |
| 67 | $rows_to_hide = ( ! OsPaymentsHelper::is_accepting_payments() ) ? [ 'payments' ] : []; |
| 68 | $order_intent->price_breakdown = wp_json_encode( $cart->generate_price_breakdown_rows( $rows_to_hide ) ); |
| 69 | $order_intent->payment_data = wp_json_encode( |
| 70 | [ |
| 71 | 'processor' => $cart->payment_processor, |
| 72 | 'time' => $cart->payment_time, |
| 73 | 'method' => $cart->payment_method, |
| 74 | 'portion' => $cart->payment_portion, |
| 75 | 'token' => $cart->payment_token, |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | /** |
| 80 | * Sets order intent from a cart |
| 81 | * |
| 82 | * @param {OsOrderIntentModel} $order_intent Order intent |
| 83 | * @param {OsCartModel} $cart Cart that order intent is using |
| 84 | * @returns {OsOrderIntentModel} The filtered order intent object |
| 85 | * |
| 86 | * @since 5.0.0 |
| 87 | * @hook set_order_intent_data_from_cart |
| 88 | * |
| 89 | */ |
| 90 | return apply_filters( 'set_order_intent_data_from_cart', $order_intent, $cart ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param OsCartItemModel $cart |
| 95 | * @param array $restrictions_data |
| 96 | * @param array $presets_data |
| 97 | * @param string $booking_form_page_url |
| 98 | * |
| 99 | * @return OsOrderIntentModel |
| 100 | */ |
| 101 | public static function create_or_update_order_intent( OsCartModel $cart, array $restrictions_data = [], array $presets_data = [], string $booking_form_page_url = '', $customer_id = false ): OsOrderIntentModel { |
| 102 | if ( empty( $booking_form_page_url ) ) { |
| 103 | $booking_form_page_url = OsUtilHelper::get_referrer(); |
| 104 | } |
| 105 | $order_intent = new OsOrderIntentModel(); |
| 106 | if ( ! empty( $cart->order_intent_id ) ) { |
| 107 | $order_intent->load_by_id( $cart->order_intent_id ); |
| 108 | } |
| 109 | $is_new = $order_intent->is_new_record(); |
| 110 | |
| 111 | if ( ! $is_new ) { |
| 112 | if ( $order_intent->is_converted() ) { |
| 113 | return $order_intent; |
| 114 | } |
| 115 | $old_order_intent = clone $order_intent; |
| 116 | } |
| 117 | |
| 118 | $order_intent->restrictions_data = wp_json_encode( $restrictions_data ); |
| 119 | $order_intent->presets_data = wp_json_encode( $presets_data ); |
| 120 | // override only if not empty |
| 121 | if ( ! empty( $booking_form_page_url ) ) { |
| 122 | $order_intent->booking_form_page_url = urldecode( $booking_form_page_url ); |
| 123 | } |
| 124 | |
| 125 | if ( empty( $customer_id ) ) { |
| 126 | $order_intent->customer_id = OsAuthHelper::get_logged_in_customer_id(); |
| 127 | } else { |
| 128 | $order_intent->customer_id = $customer_id; |
| 129 | } |
| 130 | |
| 131 | $order_intent = self::set_order_intent_data_from_cart( $order_intent, $cart ); |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Filters order intent right before it's about to be saved when created or updated from cart |
| 136 | * |
| 137 | * @param {OsOrderIntentModel} $order_intent Order intent to be filtered |
| 138 | * @returns {OsOrderIntentModel} The filtered order intent |
| 139 | * |
| 140 | * @since 5.0.0 |
| 141 | * @hook latepoint_before_order_intent_save_from_cart |
| 142 | * |
| 143 | */ |
| 144 | $order_intent = apply_filters( 'latepoint_before_order_intent_save_from_cart', $order_intent ); |
| 145 | if ( $order_intent->save() ) { |
| 146 | if ( $is_new ) { |
| 147 | $cart->update_attributes( [ 'order_intent_id' => $order_intent->id ] ); |
| 148 | /** |
| 149 | * Order intent is created |
| 150 | * |
| 151 | * @param {OsOrderIntentModel} $order_intent Instance of order intent model that was created |
| 152 | * |
| 153 | * @since 5.0.0 |
| 154 | * @hook latepoint_order_intent_created |
| 155 | * |
| 156 | */ |
| 157 | do_action( 'latepoint_order_intent_created', $order_intent ); |
| 158 | } else { |
| 159 | /** |
| 160 | * Order intent is updated |
| 161 | * |
| 162 | * @param {OsOrderIntentModel} $order_intent Updated instance of order intent model |
| 163 | * @param {OsOrderIntentModel} $old_order_intent Instance of order intent model before it was updated |
| 164 | * |
| 165 | * @since 5.0.0 |
| 166 | * @hook latepoint_order_intent_updated |
| 167 | * |
| 168 | */ |
| 169 | do_action( 'latepoint_order_intent_updated', $order_intent, $old_order_intent ); |
| 170 | } |
| 171 | } else { |
| 172 | $action_type = $is_new ? 'creating' : 'updating'; |
| 173 | OsDebugHelper::log( 'Error ' . $action_type . ' order intent', 'error_saving_order_intent', $order_intent->get_error_messages() ); |
| 174 | } |
| 175 | |
| 176 | return $order_intent; |
| 177 | } |
| 178 | |
| 179 | public static function get_order_intent_by_intent_key( string $intent_key ): OsOrderIntentModel { |
| 180 | $order_intent = new OsOrderIntentModel(); |
| 181 | if ( empty( $intent_key ) ) { |
| 182 | return $order_intent; |
| 183 | } |
| 184 | $order_intent = $order_intent->where( [ 'intent_key' => $intent_key ] )->set_limit( 1 )->get_results_as_models(); |
| 185 | if ( ! empty( $order_intent ) ) { |
| 186 | return $order_intent; |
| 187 | } else { |
| 188 | return new OsOrderIntentModel(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | public static function is_converted( $order_intent_id ) { |
| 193 | $order_intent = new OsOrderIntentModel( $order_intent_id ); |
| 194 | if ( ! empty( $order_intent->order_id ) ) { |
| 195 | return $order_intent->order_id; |
| 196 | } else { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 |