PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.9.5
Subscriptions for WooCommerce with Stripe Recurring Payments v1.9.5
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / GuestCheckout.php

GuestCheckout.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.9.5, at includes/Illuminate/GuestCheckout.php

284 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Guest Checkout File
4 *
5 * @package SpringDevs\Subscription\Illuminate
6 */
7
8 namespace SpringDevs\Subscription\Illuminate;
9
10 /**
11 * Class GuestCheckout
12 *
13 * @package SpringDevs\Subscription\Illuminate
14 */
15 class GuestCheckout {
16 /**
17 * Initialize the class
18 */
19 public function __construct() {
20 // Add guest checkout settings
21 add_filter( 'subscrpt_settings_fields', [ $this, 'add_guest_checkout_settings_fields' ] );
22 add_action( 'subscrpt_register_settings', [ $this, 'register_settings' ] );
23
24 // Show warning if guest checkout is disabled in WooCommerce settings.
25 add_action( 'admin_notices', [ $this, 'check_woocommerce_checkout_settings' ] );
26
27 // Guest checkout validation.
28 add_action( 'woocommerce_checkout_process', [ $this, 'validate_guest_checkout' ] );
29 add_action( 'woocommerce_store_api_cart_errors', [ $this, 'validate_guest_checkout_storeapi' ] );
30
31 // Enforce Login/Registration in checkout
32 add_action( 'woocommerce_checkout_process', [ $this, 'require_account_creation' ] );
33 add_filter( 'woocommerce_store_api_checkout_update_order_from_request', [ $this,'require_account_creation_store_api' ], 10, 2 );
34 }
35
36 /**
37 * Add Guest Checkout Settings Fields
38 *
39 * @param array $settings_fields Settings fields.
40 * @return array
41 */
42 public function add_guest_checkout_settings_fields( $settings_fields ) {
43 $guest_checkout_fields = [
44 [
45 'type' => 'heading',
46 'group' => 'guest_checkout',
47 'priority' => 1,
48 'field_data' => [
49 'title' => __( 'Guest Checkout', 'subscription' ),
50 'description' => __( 'Manage guest checkout settings for subscriptions.', 'subscription' ),
51 ],
52 ],
53 [
54 'type' => 'toggle',
55 'group' => 'guest_checkout',
56 'priority' => 1,
57 'field_data' => [
58 'id' => 'wp_subscription_allow_guest_checkout',
59 'title' => __( 'Allow Guest Checkout', 'subscription' ),
60 'description' => __( 'Allow customers to checkout without logging in.', 'subscription' ) . '<br/><sub>' . __( 'Note: You will need to enable <strong>Guest checkout</strong> and <strong>Allow customers to create an account during checkout</strong> options in WooCommerce settings for this to work properly.', 'subscription' ) . '</sub>',
61 'value' => '1',
62 'checked' => '1' === get_option( 'wp_subscription_allow_guest_checkout', '0' ),
63 ],
64 ],
65 [
66 'type' => 'toggle',
67 'group' => 'guest_checkout',
68 'priority' => 2,
69 'field_data' => [
70 'id' => 'wp_subscription_enforce_login',
71 'title' => __( 'Enforce Login', 'subscription' ),
72 'description' => __( 'Force customers to login or check the "Create account" checkbox before checking out.', 'subscription' ),
73 'value' => '1',
74 'checked' => '1' === get_option( 'wp_subscription_enforce_login', '1' ),
75 ],
76 ],
77 ];
78
79 return array_merge( $settings_fields, $guest_checkout_fields );
80 }
81
82 /**
83 * Register settings option.
84 */
85 public function register_settings() {
86 register_setting(
87 'wp_subscription_settings',
88 'wp_subscription_allow_guest_checkout',
89 array(
90 'type' => 'string',
91 'sanitize_callback' => 'sanitize_text_field',
92 )
93 );
94
95 register_setting(
96 'wp_subscription_settings',
97 'wp_subscription_enforce_login',
98 array(
99 'type' => 'string',
100 'sanitize_callback' => 'sanitize_text_field',
101 )
102 );
103 }
104
105 /**
106 * Is guest checkout allowed
107 */
108 public static function is_guest_checkout_allowed() {
109 return in_array( get_option( 'wp_subscription_allow_guest_checkout', '0' ), [ 1, '1' ], true );
110 }
111
112 /**
113 * Is guest login enforced
114 */
115 public static function is_guest_login_enforced() {
116 return in_array( get_option( 'wp_subscription_enforce_login', '1' ), [ 1, '1' ], true );
117 }
118
119 /**
120 * Check if cart/order has subscription and guest checkout are allowed.
121 */
122 public function is_subs_and_guest_checkout_allowed() {
123 $is_user_logged_in = is_user_logged_in();
124 $is_guest_checkout_allowed = self::is_guest_checkout_allowed();
125 $cart_have_subscription = false;
126
127 // Check in cart.
128 if ( function_exists( 'WC' ) ) {
129 $cart_items = WC()->cart->get_cart();
130 $recurrs = Helper::get_recurrs_from_cart( $cart_items );
131 $cart_have_subscription = count( $recurrs ) > 0;
132 }
133
134 if ( $cart_have_subscription ) {
135 return $is_user_logged_in || $is_guest_checkout_allowed;
136 } else {
137 return true;
138 }
139 }
140
141 /**
142 * Check WooCommerce checkout settings and show admin notice if guest checkout is disabled.
143 */
144 public function check_woocommerce_checkout_settings() {
145 // Check if WooCommerce is active
146 if ( ! class_exists( 'WooCommerce' ) ) {
147 return;
148 }
149
150 $subs_guest_checkout_allowed = self::is_guest_checkout_allowed();
151 if ( ! $subs_guest_checkout_allowed ) {
152 return;
153 }
154
155 $guest_checkout_enabled = in_array( get_option( 'woocommerce_enable_guest_checkout' ), [ 1, '1', 'yes', 'on' ], true );
156 $account_during_checkout = in_array( get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), [ 1, '1', 'yes', 'on' ], true );
157 $account_after_checkout = in_array( get_option( 'woocommerce_enable_delayed_account_creation' ), [ 1, '1', 'yes', 'on' ], true );
158
159 $issues = [];
160 if ( ! $guest_checkout_enabled ) {
161 $issues[] = 'Guest checkout.';
162 }
163 if ( ! $account_during_checkout ) {
164 $issues[] = 'Account creation during checkout.';
165 }
166
167 if ( ! empty( $issues ) ) {
168 $settings_url = admin_url( 'admin.php?page=wc-settings&tab=account' );
169
170 $list_html = '';
171 foreach ( $issues as $issue ) {
172 $list_html .= '<li><span class="dashicons dashicons-arrow-right"></span> <strong>' . $issue . '</strong></li>';
173 }
174
175 $requirement_html = '<div class="notice notice-error is-dismissible">' .
176 '<p>To ensure Subscriptions guest checkout functions correctly, please enable the following settings in WooCommerce. ' .
177 'Click <a href="' . $settings_url . '">here</a> to go to the settings.</p>' .
178 '<ul>' . $list_html . '</ul></div>';
179
180 echo wp_kses_post( $requirement_html );
181 }
182
183 if ( $account_after_checkout ) {
184 $settings_url = admin_url( 'admin.php?page=wc-settings&tab=account' );
185
186 $requirement_html = '<div class="notice notice-warning is-dismissible">' .
187 '<p>Enabling <strong>Account creation after checkout</strong> in WooCommerce settings may lead to issues with subscription orders for guest users.</p>' .
188 '<p>It\'s recommended to disable this option for optimal functionality with Subscriptions. Click <a href="' . $settings_url . '">here</a> to go to the settings.</p></div>';
189
190 echo wp_kses_post( $requirement_html );
191 }
192 }
193
194 /**
195 * Validate guest checkout.
196 */
197 public function validate_guest_checkout() {
198 if ( ! $this->is_subs_and_guest_checkout_allowed() ) {
199 wc_add_notice( __( 'You are trying to buy a subscription. You must be logged in to continue.', 'subscription' ), 'error' );
200 return;
201 }
202 }
203
204 /**
205 * Validate guest checkout on storeAPI.
206 *
207 * @param \WP_Error $errors Errors object.
208 * @return \WP_Error
209 */
210 public function validate_guest_checkout_storeapi( $errors ) {
211 if ( ! $this->is_subs_and_guest_checkout_allowed() ) {
212 $errors->add( 'wp_subscription_login_required', __( 'You are trying to buy a subscription. You must be logged in to continue.', 'subscription' ) );
213 return $errors;
214 }
215 }
216
217 /**
218 * Enforce account creation in checkout.
219 */
220 public function require_account_creation() {
221 if ( is_user_logged_in() || ! self::is_guest_checkout_allowed() ) {
222 return;
223 }
224
225 // Check cart for subscriptions.
226 $cart_have_subscription = false;
227 if ( function_exists( 'WC' ) ) {
228 $cart_items = WC()->cart->get_cart();
229 $recurrs = Helper::get_recurrs_from_cart( $cart_items );
230 $cart_have_subscription = count( $recurrs ) > 0;
231 }
232
233 if ( ! $cart_have_subscription ) {
234 return;
235 }
236
237 // phpcs:ignore WordPress.Security.NonceVerification
238 $is_create_account = isset( $_POST['createaccount'] ) ? (bool) sanitize_text_field( wp_unslash( $_POST['createaccount'] ) ) : false;
239 $is_login_enforced = self::is_guest_login_enforced();
240
241 if ( $is_login_enforced && ! $is_create_account ) {
242 wc_add_notice(
243 wp_kses_post(
244 __( 'You are ordering a subscription product. You must be either <strong>logged in</strong> or check the "<strong>Create an account</strong>" option to continue the checkout.', 'subscription' )
245 ),
246 'error'
247 );
248 }
249 }
250
251 /**
252 * Enforce account creation in Store API checkout.
253 *
254 * @param WC_Order $order Order object.
255 * @param WP_REST_Request $request Request object.
256 *
257 * @throws \WC_Data_Exception If account creation is required but not selected.
258 */
259 public function require_account_creation_store_api( $order, $request ) {
260 if ( is_user_logged_in() || ! self::is_guest_checkout_allowed() ) {
261 return;
262 }
263
264 $is_subscription_order = Helper::order_has_subscription_item( $order );
265 if ( ! $is_subscription_order ) {
266 return;
267 }
268
269 $request_body = $request->get_json_params();
270 $is_create_account = $request_body['create_account'] ?? false;
271 $is_login_enforced = self::is_guest_login_enforced();
272
273 if ( $is_login_enforced && ! $is_create_account ) {
274 throw new \WC_Data_Exception(
275 'wp_subscription_account_required',
276 wp_kses_post(
277 __( 'You are ordering a subscription product. You must be either <strong>logged in</strong> or check the "<strong>Create an account</strong>" option to continue the checkout.', 'subscription' )
278 ),
279 400
280 );
281 }
282 }
283 }
284