PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.32
OttoKit: All-in-One Automation Platform v1.1.32
1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / late-point / actions / create-order.php
suretriggers / src / Integrations / late-point / actions Last commit date
add-booking-to-order.php 5 months ago cancel-booking.php 10 months ago create-agent.php 10 months ago create-booking.php 1 year ago create-coupon.php 10 months ago create-customer.php 10 months ago create-order.php 10 months ago find-agent-by-email.php 2 years ago find-booking-by-id.php 10 months ago find-customer-by-email.php 2 years ago list-bundles.php 1 year ago update-booking.php 2 years ago update-coupon.php 10 months ago update-customer.php 2 months ago
create-order.php
235 lines
1 <?php
2 /**
3 * CreateBundleOrder.
4 * php version 5.6
5 *
6 * @category CreateBundleOrder
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\LatePoint\Actions;
15
16 use Exception;
17 use OsAgentModel;
18 use OsBookingModel;
19 use OsCustomerModel;
20 use OsOrderModel;
21 use OsOrdersHelper;
22 use OsOrderItemModel;
23 use OsBundleModel;
24 use SureTriggers\Integrations\AutomateAction;
25 use SureTriggers\Traits\SingletonLoader;
26
27 /**
28 * CreateBundleOrder
29 *
30 * @category CreateBundleOrder
31 * @package SureTriggers
32 * @author BSF <username@example.com>
33 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
34 * @link https://www.brainstormforce.com/
35 * @since 1.0.0
36 */
37 class CreateBundleOrder extends AutomateAction {
38 /**
39 * Integration type.
40 *
41 * @var string
42 */
43 public $integration = 'LatePoint';
44
45 /**
46 * Action name.
47 *
48 * @var string
49 */
50 public $action = 'lp_create_order';
51
52 use SingletonLoader;
53
54 /**
55 * Register action.
56 *
57 * @param array $actions action data.
58 * @return array
59 */
60 public function register( $actions ) {
61 $actions[ $this->integration ][ $this->action ] = [
62 'label' => __( 'Create Bundle Order', 'suretriggers' ),
63 'action' => 'lp_create_order',
64 'function' => [ $this, 'action_listener' ],
65 ];
66 return $actions;
67 }
68
69 /**
70 * Action listener.
71 *
72 * @param int $user_id user_id.
73 * @param int $automation_id automation_id.
74 * @param array $fields fields.
75 * @param array $selected_options selectedOptions.
76 *
77 * @throws Exception Exception.
78 *
79 * @return array
80 */
81 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
82 if ( ! class_exists( 'OsBundleModel' ) || ! class_exists( 'OsCustomerModel' ) ||
83 ! class_exists( 'OsOrderModel' ) || ! class_exists( 'OsOrderItemModel' ) ) {
84 return [
85 'status' => 'error',
86 'message' => 'LatePoint plugin not installed.',
87 ];
88 }
89
90 $required_params = [
91 'bundle_id',
92 'customer_type',
93 ];
94
95 foreach ( $required_params as $param ) {
96 if ( ! isset( $selected_options[ $param ] ) ) {
97 return [
98 'status' => 'error',
99 'message' => "Missing required parameter: {$param}",
100 ];
101 }
102 }
103
104 $bundle = new OsBundleModel( $selected_options['bundle_id'] );
105
106 if ( ! $bundle->id ) {
107 return [
108 'status' => 'error',
109 'message' => 'Invalid bundle ID provided.',
110 ];
111 }
112
113 $customer_type = isset( $selected_options['customer_type'] ) ?
114 $selected_options['customer_type'] : 'new';
115
116 $customer_id = null;
117 if ( 'existing' === $customer_type ) {
118 $customer_id = isset( $selected_options['customer_id'] ) ? $selected_options['customer_id'] : null;
119
120 if ( ! $customer_id ) {
121 return [
122 'status' => 'error',
123 'message' => 'Customer ID not provided.',
124 ];
125 }
126 }
127
128
129 if ( 'new' === $customer_type ) {
130 $customer_params = [
131 'first_name' => isset( $selected_options['customer_first_name'] ) ? $selected_options['customer_first_name'] : '',
132 'last_name' => isset( $selected_options['customer_last_name'] ) ? $selected_options['customer_last_name'] : '',
133 'email' => isset( $selected_options['customer_email'] ) ? $selected_options['customer_email'] : '',
134 'phone' => isset( $selected_options['customer_phone'] ) ? $selected_options['customer_phone'] : '',
135 'notes' => isset( $selected_options['customer_notes'] ) ? $selected_options['customer_notes'] : '',
136 'admin_notes' => isset( $selected_options['admin_notes'] ) ? $selected_options['admin_notes'] : '',
137 ];
138
139 $customer_custom_fields = [];
140 if ( ! empty( $selected_options['customer_fields'] ) ) {
141 foreach ( $selected_options['customer_fields'] as $field ) {
142 if ( is_array( $field ) && ! empty( $field ) ) {
143 foreach ( $field as $key => $value ) {
144 if ( false === strpos( $key, 'field_column' ) && '' !== $value ) {
145 $customer_custom_fields[ $key ] = $value;
146 }
147 }
148 }
149 }
150 }
151
152 $customer_params['custom_fields'] = $customer_custom_fields;
153
154 $customer = new OsCustomerModel();
155 $existing_customer = $customer->where( [ 'email' => $customer_params['email'] ] )
156 ->set_limit( 1 )
157 ->get_results_as_models();
158
159 if ( isset( $existing_customer->id ) && ! empty( $existing_customer->id ) ) {
160 $customer = new OsCustomerModel( $existing_customer->id );
161 } else {
162 $customer = new OsCustomerModel();
163 }
164
165 $customer->set_data( $customer_params );
166
167 if ( ! $customer->save() ) {
168 $errors = $customer->get_error_messages();
169 $error_msg = isset( $errors[0] ) ? $errors[0] : 'Customer could not be created.';
170 return [
171 'status' => 'error',
172 'message' => $error_msg,
173 ];
174 }
175 } else {
176 $customer = new OsCustomerModel( $customer_id );
177 if ( ! $customer->id ) {
178 return [
179 'status' => 'error',
180 'message' => 'Customer not found.',
181 ];
182 }
183 }
184
185 $order = new OsOrderModel();
186 $order->status = isset( $selected_options['status'] ) ? $selected_options['status'] : 'open';
187 $order->fulfillment_status = isset( $selected_options['fulfillment_status'] ) ? $selected_options['fulfillment_status'] : $order->get_default_fulfillment_status();
188 $order->customer_id = $customer->id;
189 $order->payment_status = isset( $selected_options['payment_status'] ) ? $selected_options['payment_status'] : 'not_paid';
190
191 if ( ! $order->save() ) {
192 $errors = $order->get_error_messages();
193 $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order could not be created.';
194 return [
195 'status' => 'error',
196 'message' => $error_msg,
197 ];
198 }
199
200 $order_item_model = new OsOrderItemModel();
201 $order_item_model->variant = defined( 'LATEPOINT_ITEM_VARIANT_BUNDLE' ) ? LATEPOINT_ITEM_VARIANT_BUNDLE : 'bundle';
202 $order_item_model->order_id = $order->id;
203
204 if ( ! $order_item_model->save() ) {
205 $errors = $order_item_model->get_error_messages();
206 $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order Item could not be created.';
207 return [
208 'status' => 'error',
209 'message' => $error_msg,
210 ];
211 }
212
213 $bundle_data = [
214 'bundle_id' => $bundle->id,
215 ];
216
217 $order_item_model->item_data = wp_json_encode( $bundle_data );
218 $order_item_model->recalculate_prices();
219
220 $order->total = $order_item_model->total;
221 $order->subtotal = $order_item_model->subtotal;
222 $order->save();
223 $order_item_model->save();
224
225 do_action( 'latepoint_order_created', $order );
226
227 return [
228 'bundle' => $bundle->get_data_vars(),
229 'order' => $order->get_data_vars(),
230 ];
231 }
232 }
233
234 CreateBundleOrder::get_instance();
235