'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 .= '
  • ' . $issue . '
  • '; } $requirement_html = '
    ' . '

    To ensure Subscriptions guest checkout functions correctly, please enable the following settings in WooCommerce. ' . 'Click here to go to the settings.

    ' . '
    '; echo wp_kses_post( $requirement_html ); } if ( $account_after_checkout ) { $settings_url = admin_url( 'admin.php?page=wc-settings&tab=account' ); $requirement_html = '
    ' . '

    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.

    '; echo wp_kses_post( $requirement_html ); } } /** * Validate guest checkout. */ public function validate_guest_checkout() { if ( ! $this->is_subs_and_guest_checkout_allowed() ) { wc_add_notice( __( 'You are trying to buy a subscription. You must be logged in to continue.', 'subscription' ), 'error' ); return; } } /** * Validate guest checkout on storeAPI. * * @param \WP_Error $errors Errors object. * @return \WP_Error */ public function validate_guest_checkout_storeapi( $errors ) { if ( ! $this->is_subs_and_guest_checkout_allowed() ) { $errors->add( 'wp_subscription_login_required', __( 'You are trying to buy a subscription. You must be logged in to continue.', 'subscription' ) ); return $errors; } } /** * Enforce account creation in checkout. */ public function require_account_creation() { if ( is_user_logged_in() || ! self::is_guest_checkout_allowed() ) { return; } // Check cart for subscriptions. $cart_have_subscription = false; 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; } // phpcs:ignore WordPress.Security.NonceVerification $is_create_account = isset( $_POST['createaccount'] ) ? (bool) sanitize_text_field( wp_unslash( $_POST['createaccount'] ) ) : false; $is_login_enforced = self::is_guest_login_enforced(); if ( $is_login_enforced && ! $is_create_account ) { wc_add_notice( wp_kses_post( __( 'You are ordering a subscription product. You must be either logged in or check the "Create an account" option to continue the checkout.', 'subscription' ) ), 'error' ); } } /** * Enforce account creation in Store API checkout. * * @param WC_Order $order Order object. * @param WP_REST_Request $request Request object. * * @throws \WC_Data_Exception If account creation is required but not selected. */ public function require_account_creation_store_api( $order, $request ) { if ( is_user_logged_in() || ! self::is_guest_checkout_allowed() ) { return; } $is_subscription_order = Helper::order_has_subscription_item( $order ); if ( ! $is_subscription_order ) { return; } $request_body = $request->get_json_params(); $is_create_account = $request_body['create_account'] ?? false; $is_login_enforced = self::is_guest_login_enforced(); if ( $is_login_enforced && ! $is_create_account ) { throw new \WC_Data_Exception( 'wp_subscription_account_required', wp_kses_post( __( 'You are ordering a subscription product. You must be either logged in or check the "Create an account" option to continue the checkout.', 'subscription' ) ), 400 ); } } }