PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 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 / woocommerce-subscriptions / actions / create-subscription-order-product.php
suretriggers / src / Integrations / woocommerce-subscriptions / actions Last commit date
add-subscription-coupon.php 11 months ago add-update-subscription-custom-fields.php 11 months ago change-subscription-status.php 11 months ago create-subscription-order-cost.php 8 months ago create-subscription-order-parent.php 7 months ago create-subscription-order-product.php 11 months ago extend-user-subscription.php 11 months ago find-subscription-by-id.php 1 month ago find-subscription-by-user-id.php 11 months ago remove-product-user-subscription.php 11 months ago update-subscription-end-date.php 11 months ago update-subscription-next-payment-date.php 11 months ago update-subscription-order-downloads.php 7 months ago
create-subscription-order-product.php
244 lines
1 <?php
2 /**
3 * CreateSubscriptionOrderProduct.
4 * php version 5.6
5 *
6 * @category CreateSubscriptionOrderProduct
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\WoocommerceSubscriptions\Actions;
15
16 use SureTriggers\Integrations\AutomateAction;
17 use SureTriggers\Integrations\WooCommerce\WooCommerce;
18 use SureTriggers\Traits\SingletonLoader;
19 use Exception;
20 use WC_Subscriptions_Product;
21 use WC_Order;
22
23 /**
24 * CreateSubscriptionOrderProduct
25 *
26 * @category CreateSubscriptionOrderProduct
27 * @package SureTriggers
28 * @author BSF <username@example.com>
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
30 * @link https://www.brainstormforce.com/
31 * @since 1.0.0
32 */
33 class CreateSubscriptionOrderProduct extends AutomateAction {
34
35 /**
36 * Integration type.
37 *
38 * @var string
39 */
40 public $integration = 'WoocommerceSubscriptions';
41
42 /**
43 * Action name.
44 *
45 * @var string
46 */
47 public $action = 'wc_create_subscription_order_product';
48
49 use SingletonLoader;
50
51 /**
52 * Register a action.
53 *
54 * @param array $actions actions.
55 * @return array
56 */
57 public function register( $actions ) {
58 $actions[ $this->integration ][ $this->action ] = [
59 'label' => __( 'Create a subscription order with a product', 'suretriggers' ),
60 'action' => 'wc_create_subscription_order_product',
61 'function' => [ $this, 'action_listener' ],
62 ];
63 return $actions;
64 }
65
66 /**
67 * Action listener.
68 *
69 * @param mixed $user_id user_id.
70 * @param int $automation_id automation_id.
71 * @param array $fields fields.
72 * @param array $selected_options selectedOptions.
73 * @throws Exception Exception.
74 *
75 * @return object|array|null|void
76 */
77 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
78 // First make sure all required functions and classes exist.
79 if ( ! function_exists( 'wc_create_order' ) || ! function_exists( 'wcs_create_subscription' ) || ! class_exists( 'WC_Subscriptions' ) ) {
80 return [
81 'status' => 'error',
82 'message' => '`wc_create_order` or `wcs_create_subscription` function is missing.',
83 ];
84 }
85
86 if ( ! class_exists( '\WC_Order' ) ) {
87 return [
88 'status' => 'error',
89 'message' => __( '\WC_Order class not found.', 'suretriggers' ),
90
91 ];
92 }
93
94 if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
95 return [
96 'status' => 'error',
97 'message' => __( 'WC_Subscriptions_Product class not found.', 'suretriggers' ),
98
99 ];
100 }
101
102 $products = $selected_options['product_id'];
103 $user_id = ap_get_user_id_from_email( $selected_options['billing_email'] );
104
105 $order = wc_create_order( [ 'customer_id' => $user_id ] );
106
107 if ( ! $order instanceof \WC_Order ) {
108 return;
109 }
110
111 if ( is_object( $order ) ) {
112 $order = $order;
113 }
114
115 $quantity = $selected_options['quantity'] ? $selected_options['quantity'] : 1;
116
117 // add products.
118 /**
119 *
120 * Ignore line
121 *
122 * @phpstan-ignore-next-line
123 */
124 $order->add_product( wc_get_product( $selected_options['product_id'] ), $quantity );
125
126 // add billing and shipping addresses.
127 $billing_address = [
128 'first_name' => $selected_options['billing_first_name'],
129 'last_name' => $selected_options['billing_last_name'],
130 'company' => $selected_options['billing_company'],
131 'country' => $selected_options['billing_country'],
132 'address_1' => $selected_options['billing_address_1'],
133 'address_2' => $selected_options['billing_address_2'],
134 'city' => $selected_options['billing_city'],
135 'state' => $selected_options['billing_state'],
136 'postcode' => $selected_options['billing_zip_code'],
137 'phone' => $selected_options['billing_phone'],
138 'email' => $selected_options['billing_email'],
139 ];
140
141 $shipping_address = [
142 'first_name' => $selected_options['shipping_first_name'] ? $selected_options['shipping_first_name'] : $selected_options['billing_first_name'],
143 'last_name' => $selected_options['shipping_last_name'] ? $selected_options['shipping_last_name'] : $selected_options['billing_last_name'],
144 'company' => $selected_options['shipping_company'] ? $selected_options['shipping_company'] : $selected_options['billing_company'],
145 'country' => $selected_options['shipping_country'] ? $selected_options['shipping_country'] : $selected_options['billing_country'],
146 'address_1' => $selected_options['shipping_address_1'] ? $selected_options['shipping_address_1'] : $selected_options['billing_address_1'],
147 'address_2' => $selected_options['shipping_address_2'] ? $selected_options['shipping_address_2'] : $selected_options['billing_address_2'],
148 'city' => $selected_options['shipping_city'] ? $selected_options['shipping_city'] : $selected_options['billing_city'],
149 'state' => $selected_options['shipping_state'] ? $selected_options['shipping_state'] : $selected_options['billing_state'],
150 'postcode' => $selected_options['shipping_zip_code'] ? $selected_options['shipping_zip_code'] : $selected_options['billing_zip_code'],
151 'phone' => $selected_options['shipping_phone'] ? $selected_options['shipping_phone'] : $selected_options['billing_phone'],
152 'email' => $selected_options['shipping_email'] ? $selected_options['shipping_email'] : $selected_options['billing_email'],
153 ];
154
155 $order->set_address( $billing_address, 'billing' );
156 $order->set_address( $selected_options['shipping_billing_address'] ? $billing_address : $shipping_address, 'shipping' );
157
158 // order status.
159 $order->set_status( 'completed' );
160
161 // calculate and save.
162 $order->calculate_totals();
163
164 if ( isset( $selected_options['product_id'] ) ) {
165
166 $sub = wcs_create_subscription(
167 [
168 'order_id' => $order->get_id(),
169 // Status should be initially set to pending to match how normal checkout process goes.
170 'billing_period' => WC_Subscriptions_Product::get_period( intval( $selected_options['product_id'] ) ),
171 'billing_interval' => WC_Subscriptions_Product::get_interval( intval( $selected_options['product_id'] ) ),
172 ]
173 );
174
175 if ( is_wp_error( $sub ) ) {
176 wp_delete_post( $order->get_id(), true );
177 return [
178 'status' => 'error',
179 'message' => 'Failed to create a subscription.',
180 ];
181 }
182
183 $sub->add_product( wc_get_product( intval( $selected_options['product_id'] ) ), intval( $quantity ) );
184 $sub->apply_coupon( $selected_options['coupon_code'] );
185 $start_date = gmdate( 'Y-m-d H:i:s' );
186
187 $trial_end_days = $selected_options['trial_end_days'];
188
189 if ( '' != $trial_end_days ) {
190 $now = strtotime( 'now' );
191 $trial_end_timestamp = strtotime( "+$trial_end_days days", $now );
192
193 if ( false !== $trial_end_timestamp ) {
194 $trial_end_date = gmdate( 'Y-m-d H:i:s', $trial_end_timestamp );
195 $dates['trial_end'] = $trial_end_date;
196
197 $trial_end_timestamp = strtotime( $trial_end_date );
198 if ( false !== $trial_end_timestamp ) {
199 $next_payment_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day', $trial_end_timestamp ) );
200 $dates['next_payment'] = WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $next_payment_date );
201 }
202 }
203
204 $start_date = $sub->get_date_created();
205 $end_date = WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $start_date );
206
207 $dates['end'] = $end_date;
208 } else {
209 $dates = [
210 'trial_end' => WC_Subscriptions_Product::get_trial_expiration_date( intval( $selected_options['product_id'] ), $start_date ),
211 'next_payment' => WC_Subscriptions_Product::get_first_renewal_payment_date( intval( $selected_options['product_id'] ), $start_date ),
212 'end' => WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $start_date ),
213 ];
214 }
215
216 $sub->update_dates( $dates );
217 $sub->update_status( $selected_options['status'] );
218 $sub->calculate_totals();
219 }
220
221 $order->update_status( 'completed' );
222 $order->calculate_totals();
223 $order->save();
224 if ( ! empty( $sub ) ) {
225 $context['subscription'] = [
226 'id' => $sub->get_id(),
227 'status' => $sub->get_status(),
228 'start_date' => $sub->get_date_created(),
229 'next_payment_date' => $sub->get_date( 'next_payment' ),
230 'trial_end_date' => $sub->get_date( 'trial_end' ),
231 'end_date' => $sub->get_date( 'end' ),
232 ];
233 $order_details = WooCommerce::get_order_context( $order->get_id() );
234 if ( is_array( $order_details ) ) {
235 return array_merge( $context, $order_details );
236 }
237 } else {
238 return WooCommerce::get_order_context( $order->get_id() );
239 }
240 }
241 }
242
243 CreateSubscriptionOrderProduct::get_instance();
244