'heading',
'group' => 'guest_checkout',
'priority' => 1,
'field_data' => [
'title' => __( 'Guest Checkout', 'subscription' ),
'description' => __( 'Manage guest checkout settings for subscriptions.', 'subscription' ),
],
],
[
'type' => 'toggle',
'group' => 'guest_checkout',
'priority' => 1,
'field_data' => [
'id' => 'wp_subscription_allow_guest_checkout',
'title' => __( 'Allow Guest Checkout', 'subscription' ),
'description' => __( 'Allow customers to checkout without logging in.', 'subscription' ) . '
' . __( 'Note: You will need to enable Guest checkout and Allow customers to create an account during checkout options in WooCommerce settings for this to work properly.', 'subscription' ) . '',
'value' => '1',
'checked' => '1' === get_option( 'wp_subscription_allow_guest_checkout', '0' ),
],
],
[
'type' => 'toggle',
'group' => 'guest_checkout',
'priority' => 2,
'field_data' => [
'id' => 'wp_subscription_enforce_login',
'title' => __( 'Enforce Login', 'subscription' ),
'description' => __( 'Force customers to login or check the "Create account" checkbox before checking out.', 'subscription' ),
'value' => '1',
'checked' => '1' === get_option( 'wp_subscription_enforce_login', '1' ),
],
],
];
return array_merge( $settings_fields, $guest_checkout_fields );
}
/**
* Register settings option.
*/
public function register_settings() {
register_setting(
'wp_subscription_settings',
'wp_subscription_allow_guest_checkout',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
register_setting(
'wp_subscription_settings',
'wp_subscription_enforce_login',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
}
/**
* Is guest checkout allowed
*/
public static function is_guest_checkout_allowed() {
return in_array( get_option( 'wp_subscription_allow_guest_checkout', '0' ), [ 1, '1' ], true );
}
/**
* Is guest login enforced
*/
public static function is_guest_login_enforced() {
return in_array( get_option( 'wp_subscription_enforce_login', '1' ), [ 1, '1' ], true );
}
/**
* Check if cart/order has subscription and guest checkout are allowed.
*/
public function is_subs_and_guest_checkout_allowed() {
$is_user_logged_in = is_user_logged_in();
$is_guest_checkout_allowed = self::is_guest_checkout_allowed();
$cart_have_subscription = false;
// Check in cart.
if ( function_exists( 'WC' ) ) {
$cart_items = WC()->cart->get_cart();
$recurrs = Helper::get_recurrs_from_cart( $cart_items );
$cart_have_subscription = count( $recurrs ) > 0;
}
if ( $cart_have_subscription ) {
return $is_user_logged_in || $is_guest_checkout_allowed;
} else {
return true;
}
}
/**
* Check WooCommerce checkout settings and show admin notice if guest checkout is disabled.
*/
public function check_woocommerce_checkout_settings() {
// Check if WooCommerce is active
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
$subs_guest_checkout_allowed = self::is_guest_checkout_allowed();
if ( ! $subs_guest_checkout_allowed ) {
return;
}
$guest_checkout_enabled = in_array( get_option( 'woocommerce_enable_guest_checkout' ), [ 1, '1', 'yes', 'on' ], true );
$account_during_checkout = in_array( get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), [ 1, '1', 'yes', 'on' ], true );
$account_after_checkout = in_array( get_option( 'woocommerce_enable_delayed_account_creation' ), [ 1, '1', 'yes', 'on' ], true );
$issues = [];
if ( ! $guest_checkout_enabled ) {
$issues[] = 'Guest checkout.';
}
if ( ! $account_during_checkout ) {
$issues[] = 'Account creation during checkout.';
}
if ( ! empty( $issues ) ) {
$settings_url = admin_url( 'admin.php?page=wc-settings&tab=account' );
$list_html = '';
foreach ( $issues as $issue ) {
$list_html .= '
To ensure Subscriptions guest checkout functions correctly, please enable the following settings in WooCommerce. ' . 'Click here to go to the settings.
' . 'Enabling Account creation after checkout in WooCommerce settings may lead to issues with subscription orders for guest users.
' . 'It\'s recommended to disable this option for optimal functionality with Subscriptions. Click here to go to the settings.