diff --git a/docs/developer/testing.md b/docs/developer/testing.md index 198dd27d61..02820a9365 100644 --- a/docs/developer/testing.md +++ b/docs/developer/testing.md @@ -204,4 +204,4 @@ By default, Pro PRs run `--grep @smoke`. Add `Suite: full` (or `Full-Suite: true | `wp-config.php is missing` | Run the installer from inside the plugin directory (`wp-content/plugins/tutor`) of a WordPress install. | | Database connection failed | Make sure MySQL is running and the `WP_TEST_DB_*` values in `.env` are correct. | | Tests hit the live database | Check that the database routing block above is present in `wp-config.php`. | -| Need to undo `wp-config.php` changes | Restore `wp-config.php.bak`. | \ No newline at end of file +| Need to undo `wp-config.php` changes | Restore `wp-config.php.bak`. | diff --git a/ecommerce/BillingController.php b/ecommerce/BillingController.php index b921de6a09..70184cba72 100644 --- a/ecommerce/BillingController.php +++ b/ecommerce/BillingController.php @@ -265,14 +265,12 @@ protected function validate( array $data ) { 'billing_first_name' => 'required', 'billing_last_name' => 'required', 'billing_email' => 'required|email', - 'billing_phone' => 'required', - 'billing_zip_code' => 'required', - 'billing_address' => 'required', - 'billing_country' => 'required', - 'billing_state' => 'required', - 'billing_city' => 'required', ); + foreach ( Settings::get_required_billing_fields() as $field => $is_required ) { + $validation_rules[ $field ] = $is_required ? 'required' : ''; + } + // Countries without states have nothing to select. if ( ! $this->model->country_has_states( wp_unslash( $data['billing_country'] ?? '' ) ) ) { unset( $validation_rules['billing_state'] ); @@ -285,7 +283,16 @@ protected function validate( array $data ) { } } - return ValidationHelper::validate( $validation_rules, $data ); + $validation = ValidationHelper::validate( $validation_rules, $data ); + + // Format checks run only on fields that passed the rules above. + $format_errors = array_diff_key( BillingModel::validate_field_formats( $data ), $validation->errors ); + if ( ! empty( $format_errors ) ) { + $validation->errors = array_merge( $validation->errors, $format_errors ); + $validation->success = false; + } + + return $validation; } /** @@ -354,4 +361,55 @@ public static function get_default_values() { return $default_values; } + + /** + * Print HTML `pattern` and `title` attributes for a billing input. + * + * @since 4.3.0 + * + * @param string $field Billing field name. + * + * @return void + */ + public static function render_pattern_attrs( string $field ): void { + $formats = BillingModel::get_field_formats(); + if ( ! isset( $formats[ $field ] ) ) { + return; + } + + printf( + ' pattern="%s" title="%s"', + esc_attr( $formats[ $field ]['pattern'] ), + esc_attr( $formats[ $field ]['message'] ) + ); + } + + /** + * Build the client-side validation rules passed to the form's register() call. + * + * @since 4.3.0 + * + * @param string $field Billing field name. + * @param bool $required Whether the field is required. + * + * @return string JavaScript object literal, such as "{ required: true, pattern: {...} }". + */ + public static function get_client_rules( string $field, bool $required ): string { + $rules = array(); + $formats = BillingModel::get_field_formats(); + + if ( $required ) { + $rules[] = 'required: true'; + } + + if ( isset( $formats[ $field ] ) ) { + $rules[] = sprintf( + "pattern: { value: /^(?:%s)$/u, message: '%s' }", + $formats[ $field ]['pattern'], + esc_js( $formats[ $field ]['message'] ) + ); + } + + return '{ ' . implode( ', ', $rules ) . ' }'; + } } diff --git a/ecommerce/CheckoutController.php b/ecommerce/CheckoutController.php index bef50bb3fe..0f577a7fd6 100644 --- a/ecommerce/CheckoutController.php +++ b/ecommerce/CheckoutController.php @@ -622,9 +622,10 @@ private function resolve_checkout_user_id() { */ protected function validate_pay_now_request( array $request, int $current_user_id = 0 ) { // Reset on new validation request. - $errors = array(); - $this->order_data = null; - $this->validated_consent = array(); + $errors = array(); + $this->order_data = null; + $this->validated_consent = array(); + $billing_validation_rules = $this->billing_model->get_validation_rules(); $order_types_str = implode( ',', array( OrderModel::TYPE_SINGLE_ORDER, OrderModel::TYPE_SUBSCRIPTION, OrderModel::TYPE_RENEWAL ) ); $validation_rules = array_merge( @@ -632,9 +633,9 @@ protected function validate_pay_now_request( array $request, int $current_user_i 'object_ids' => 'required|regex:/^[0-9]+(,[0-9]+)*$/', 'order_type' => "required|match_string:{$order_types_str}", 'payment_method' => 'required', - 'payment_type' => 'if_input|match_string:automate,manual' + 'payment_type' => 'if_input|match_string:automate,manual', ), - $this->billing_model->get_validation_rules() + $billing_validation_rules ); $validation = ValidationHelper::validate( $validation_rules, $request ); @@ -644,8 +645,8 @@ protected function validate_pay_now_request( array $request, int $current_user_i $errors = array_merge( $errors, $this->billing_model->validate_billing_location( - $request['billing_country'], - $request['billing_state'] + $request['billing_country'] ?? '', + $request['billing_state'] ?? '' ) ); @@ -722,7 +723,7 @@ protected function get_allowed_order_fields() { * @return array An array of error messages. */ protected function save_billing_info( $user_id, $fields ) { - $billing_info = $this->billing_model->get_info( $user_id ); + $billing_info = $this->billing_model->get_info( $user_id ); if ( $billing_info ) { $update_billing = $this->billing_model->update( $fields, array( 'user_id' => $user_id ) ); @@ -879,7 +880,7 @@ public static function get_checkout_errors( int $user_id ): array { * * @return string */ - protected static function prepare_checkout_alert_key( int $user_id ):string { + protected static function prepare_checkout_alert_key( int $user_id ): string { return self::PAY_NOW_ALERT_MSG_TRANSIENT_KEY . $user_id; } @@ -1485,6 +1486,11 @@ public function pay_incomplete_order() { exit; } + $format_errors = BillingModel::validate_field_formats( $billing_fillable_fields ); + if ( ! empty( $format_errors ) ) { + tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_id, implode( ' ', array_merge( ...array_values( $format_errors ) ) ) ); + } + if ( $order_id ) { $order_model = new OrderModel(); $order_data = $order_model->get_order_by_id( $order_id ); diff --git a/ecommerce/OptionKeys.php b/ecommerce/OptionKeys.php index e473e82dcb..0d7bcadf77 100644 --- a/ecommerce/OptionKeys.php +++ b/ecommerce/OptionKeys.php @@ -44,6 +44,19 @@ class OptionKeys { const BUY_NOW = 'is_enable_buy_now'; + /** + * Whether the billing address fields are required. + * + * @since 4.3.0 + */ + const IS_BILLING_COUNTRY_REQUIRED = 'is_billing_country_required'; + const IS_BILLING_STATE_REQUIRED = 'is_billing_state_required'; + const IS_BILLING_CITY_REQUIRED = 'is_billing_city_required'; + const IS_BILLING_ZIP_CODE_REQUIRED = 'is_billing_zip_code_required'; + const IS_BILLING_PHONE_REQUIRED = 'is_billing_phone_required'; + const IS_BILLING_ADDRESS_REQUIRED = 'is_billing_address_required'; + + /** * Get billing field options * diff --git a/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php b/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php index 6382bdcf12..4850774e02 100644 --- a/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php +++ b/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php @@ -253,34 +253,6 @@ function ( $message, $issue ) { return $final_message; } - /** - * Creates the shipping information array for a payment request. - * - * This method constructs and returns an array containing the shipping type, receiver's full name, - * and address for a payment request. - * - * @param object $shipping The shipping data containing the receiver's name and address. - * @return array The formatted shipping information including type, name, and address. - * @since 3.0.0 - * @since 4.2.0 Uses Utils::splitAddress() instead of System::splitAddress(). - */ - public static function getShippingInfo( $shipping ): array { - [$address1, $address2] = Utils::splitAddress( $shipping, 300 ); - - return array( - 'type' => 'SHIPPING', - 'name' => array( 'full_name' => $shipping->name ), - 'address' => array( - 'address_line_1' => $address1, - 'address_line_2' => $address2, - 'admin_area_2' => $shipping->city, - 'admin_area_1' => $shipping->region, - 'postal_code' => $shipping->postal_code, - 'country_code' => $shipping->country->alpha_2, - ), - ); - } - /** * Retrieves the URL from an array of links that matches a specified condition. * @@ -338,8 +310,7 @@ private static function built_payment_source_from_vault( $previous_payload ): ?a return null; } - $paypal_source = $vault_details->payment_source->paypal; - $shipping_address = $paypal_source->shipping->address; + $paypal_source = $vault_details->payment_source->paypal; return array( 'paypal' => array( @@ -357,32 +328,10 @@ private static function built_payment_source_from_vault( $previous_payload ): ?a 'experience_context' => array( 'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED', ), - 'address' => self::format_address( $shipping_address ), ), ); } - /** - * Format a shipping address object into an associative array. - * - * @since 3.9.0 - * - * @param object $shipping_address The PayPal shipping address object. - * - * @return array The formatted address as an associative array. - */ - private static function format_address( $shipping_address ): array { - - return array( - 'address_line_1' => $shipping_address->address_line_1, - 'address_line_2' => $shipping_address->address_line_2, - 'admin_area_1' => $shipping_address->admin_area_1 ?? '', - 'admin_area_2' => $shipping_address->admin_area_2 ?? '', - 'postal_code' => $shipping_address->postal_code, - 'country_code' => $shipping_address->country_code, - ); - } - /** * Build a PayPal payment source array using order details. * @@ -402,8 +351,7 @@ private static function built_payment_source_from_order_details( $previous_paylo return null; } - $paypal_source = $order_details->payment_source->paypal; - $shipping_address = $order_details->purchase_units[0]->shipping->address; + $paypal_source = $order_details->payment_source->paypal; return array( 'paypal' => array( @@ -421,7 +369,6 @@ private static function built_payment_source_from_order_details( $previous_paylo 'experience_context' => array( 'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED', ), - 'address' => self::format_address( $shipping_address ), ), ); } diff --git a/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Paypal.php b/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Paypal.php index a5cac57fe3..26b3c46677 100644 --- a/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Paypal.php +++ b/ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Paypal.php @@ -145,23 +145,14 @@ public function prepareData( $data ): array { $items = 'one-time' === $type ? Helper::getItems( $data ) : null; $amount = 'one-time' === $type ? Helper::createAmountData( $data ) : Helper::createAmountForRecurring( $data ); $tutor_metadata = Utils::prepareMerchantMetadata( $data->order_user_id, $this->config->get( 'mode' ) ); - $description = implode( - ',', - array_map( - fn ( $key, $value ) => "{$key}: {$value}", - array_keys( $tutor_metadata ), - $tutor_metadata - ) - ); $returnData = array( 'purchase_units' => array( array( - 'custom_id' => $data->order_id, - 'items' => $items, - 'amount' => $amount, - 'payee' => array( 'email_address' => $this->config->get( 'merchant_email' ) ), - 'description' => $description, + 'custom_id' => $data->order_id, + 'items' => $items, + 'amount' => $amount, + 'payee' => array( 'email_address' => $this->config->get( 'merchant_email' ) ), ), ), 'intent' => 'CAPTURE', @@ -176,10 +167,6 @@ public function prepareData( $data ): array { Helper::getPaymentSourceForRecurring( $returnData, $this->previousPayload ); } - if ( isset( $data->shipping_address ) && ! empty( $data->shipping_address ) ) { - $returnData['purchase_units'][0]['shipping'] = Helper::getShippingInfo( $data->shipping_address ); - } - return $returnData; } diff --git a/ecommerce/Settings.php b/ecommerce/Settings.php index ae825e30b2..1fc8e13673 100644 --- a/ecommerce/Settings.php +++ b/ecommerce/Settings.php @@ -241,6 +241,56 @@ public static function add_ecommerce_settings( $fields ) { ), ), ), + array( + 'label' => __( 'Billing Address Configuration', 'tutor' ), + 'desc' => __( 'Customize your billing address field to suit your preferences.', 'tutor' ), + 'slug' => 'billing_address_configuration', + 'block_type' => 'uniform', + 'fields' => array( + array( + 'key' => OptionKeys::IS_BILLING_COUNTRY_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'Country Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to select a country.', 'tutor' ), + ), + array( + 'key' => OptionKeys::IS_BILLING_STATE_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'State Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to select a state.', 'tutor' ), + ), + array( + 'key' => OptionKeys::IS_BILLING_CITY_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'City Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to add a city.', 'tutor' ), + ), + array( + 'key' => OptionKeys::IS_BILLING_ZIP_CODE_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'Postal/ZIP Code Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to add postal/zip code.', 'tutor' ), + ), + array( + 'key' => OptionKeys::IS_BILLING_PHONE_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'Phone Number Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to add phone number.', 'tutor' ), + ), + array( + 'key' => OptionKeys::IS_BILLING_ADDRESS_REQUIRED, + 'type' => 'toggle_switch', + 'label' => __( 'Address Required', 'tutor' ), + 'default' => 'on', + 'desc' => __( 'Require users to add address.', 'tutor' ), + ), + ), + ), ), ); @@ -504,4 +554,54 @@ public static function get_paypal_config_fields() { return $config_fields; } + + /** + * Check whether a billing field is required. + * + * First name, last name and email are always required. The other fields + * follow their "Billing Address Configuration" setting. + * + * @since 4.3.0 + * + * @param string $field Billing field name, such as billing_city. + * + * @return boolean + */ + public static function is_billing_field_required( string $field ) { + if ( in_array( $field, array( 'billing_first_name', 'billing_last_name', 'billing_email' ), true ) ) { + return true; + } + + $fields = self::get_configurable_billing_fields(); + if ( isset( $fields[ $field ] ) ) { + return (bool) tutor_utils()->get_option( $fields[ $field ], true ); + } + + return false; + } + + /** + * Billing fields whose required state is controlled by a setting, mapped to their option key. + * + * @since 4.3.0 + * + * @return array + */ + public static function get_configurable_billing_fields() { + return array( + 'billing_country' => OptionKeys::IS_BILLING_COUNTRY_REQUIRED, + 'billing_state' => OptionKeys::IS_BILLING_STATE_REQUIRED, + 'billing_city' => OptionKeys::IS_BILLING_CITY_REQUIRED, + 'billing_zip_code' => OptionKeys::IS_BILLING_ZIP_CODE_REQUIRED, + 'billing_phone' => OptionKeys::IS_BILLING_PHONE_REQUIRED, + 'billing_address' => OptionKeys::IS_BILLING_ADDRESS_REQUIRED, + ); + } + + public static function get_required_billing_fields() { + return array_map( + fn( $option_key ) => (bool) tutor_utils()->get_option( $option_key, true ), + self::get_configurable_billing_fields() + ); + } } diff --git a/models/BillingModel.php b/models/BillingModel.php index a469a6097d..64eaea7b02 100644 --- a/models/BillingModel.php +++ b/models/BillingModel.php @@ -12,6 +12,7 @@ use Tutor\Core\DB; use Tutor\Core\Input; +use Tutor\Ecommerce\Settings; use Tutor\Helpers\QueryHelper; /** @@ -93,17 +94,27 @@ public function get_required_fields() { public function get_validation_rules() { /** * The `billing_state` field is excluded because not all countries have states. + * It is checked by validate_billing_location(). */ - return array( + $rules = array( 'billing_first_name' => 'required', 'billing_last_name' => 'required', 'billing_email' => 'required|email', - 'billing_phone' => 'required', - 'billing_zip_code' => 'required', - 'billing_address' => 'required', - 'billing_country' => 'required', - 'billing_city' => 'required', ); + + foreach ( Settings::get_required_billing_fields() as $field => $is_required ) { + if ( 'billing_state' !== $field ) { + $rules[ $field ] = $is_required ? 'required' : 'if_input'; + } + } + + foreach ( self::get_field_formats() as $field => $format ) { + if ( isset( $rules[ $field ] ) ) { + $rules[ $field ] .= '|regex:/^(' . $format['pattern'] . ')?$/u'; + } + } + + return $rules; } /** @@ -120,13 +131,21 @@ public function get_validation_rules() { * @return string[] List of error messages, empty when valid. */ public function validate_billing_location( string $country, string $state ): array { - $country_info = tutor_get_country_info_by_name( $country ); + // An optional country may be left empty, but then a state makes no sense. + if ( empty( $country ) && ! Settings::is_billing_field_required( 'billing_country' ) ) { + return empty( $state ) ? array() : array( __( 'Invalid Country for the given state', 'tutor' ) ); + } + $country_info = tutor_get_country_info_by_name( $country ); if ( ! $country_info ) { return array( __( 'Invalid country', 'tutor' ) ); } $state_names = array_column( $country_info['states'] ?? array(), 'name' ); + // An optional state may be left empty. + if ( empty( $state ) && ! Settings::is_billing_field_required( 'billing_state' ) ) { + return array(); + } // Countries without states don't require one. if ( ! empty( $state_names ) && ! in_array( $state, $state_names, true ) ) { @@ -215,7 +234,7 @@ public function get_info( $user_id ) { * @return bool */ public function has_complete_billing_info( array $data ) { - foreach ( $this->get_required_fields() as $field ) { + foreach ( $this->get_active_required_fields() as $field ) { if ( 'billing_state' === $field && ! $this->country_has_states( $data['billing_country'] ?? '' ) ) { continue; } @@ -225,6 +244,11 @@ public function has_complete_billing_info( array $data ) { } } + $format_errors = self::validate_field_formats( $data ); + if ( ! empty( $format_errors ) ) { + return false; + } + return true; } @@ -287,4 +311,96 @@ public function country_has_states( string $country ) { public function get_partial_update_fields() { return $this->partial_update_fields; } + + /** + * Check billing values against their allowed formats. + * + * @since 4.3.0 + * + * @param array $data Un-slashed billing data keyed by field name. + * + * @return array Errors keyed by field name. + */ + public static function validate_field_formats( array $data ): array { + $errors = array(); + + foreach ( self::get_field_formats() as $field => $format ) { + $value = trim( (string) ( $data[ $field ] ?? '' ) ); + if ( empty( $value ) ) { + continue; + } + + if ( ! preg_match( '~^(?:' . $format['pattern'] . ')$~u', $value ) ) { + $errors[ $field ][] = $format['message']; + } + } + + return $errors; + } + + /** + * Allowed character formats for billing fields. + * + * @since 4.3.0 + * + * @return array + */ + public static function get_field_formats(): array { + // Matches one or more letters (any language), combining marks, spaces, apostrophes, dots, or hyphens. + $name_pattern = "[\\p{L}\\p{M}\\s'.\\-]+"; + + $formats = array( + 'billing_first_name' => array( + 'pattern' => $name_pattern, + 'message' => __( 'First name can only contain letters, spaces, apostrophes, dots and hyphens.', 'tutor' ), + ), + 'billing_last_name' => array( + 'pattern' => $name_pattern, + 'message' => __( 'Last name can only contain letters, spaces, apostrophes, dots and hyphens.', 'tutor' ), + ), + 'billing_phone' => array( + 'pattern' => '\\+?[0-9\\s\\(\\)\\-]{6,20}', + 'message' => __( 'Phone can only contain digits, spaces, parentheses, hyphens and a leading +.', 'tutor' ), + ), + 'billing_city' => array( + 'pattern' => "[\\p{L}\\p{M}\\p{N}\\s'.\\-]+", + 'message' => __( 'City can only contain letters, spaces, apostrophes, dots, numbers and hyphens.', 'tutor' ), + ), + 'billing_zip_code' => array( + 'pattern' => '[A-Za-z0-9\\s\\-]{2,12}', + 'message' => __( 'Postcode/ZIP can only contain letters, digits, spaces and hyphens.', 'tutor' ), + ), + 'billing_address' => array( + 'pattern' => "[\\p{L}\\p{M}\\p{N}\\s,.#&'\\/\\(\\)\\-]+", + 'message' => __( 'Address can only contain letters, digits, spaces and , . # / \' ( ) -', 'tutor' ), + ), + ); + + /** + * Filters the allowed character formats for billing fields. + * + * @since 4.3.0 + * + * @param array $formats Field name => array( 'pattern' => string, 'message' => string ). + */ + return apply_filters( 'tutor_billing_field_formats', $formats ); + } + + /** + * Get the billing fields that are required by the current settings. + * + * @since 4.3.0 + * + * @return string[] + */ + public function get_active_required_fields() { + return array_values( + array_filter( + $this->required_fields, + function ( $field ) { + return Settings::is_billing_field_required( $field ); + } + ) + ); + } } diff --git a/templates/ecommerce/billing-form-fields.php b/templates/ecommerce/billing-form-fields.php index 53bb7ce8ff..61a2517a1c 100644 --- a/templates/ecommerce/billing-form-fields.php +++ b/templates/ecommerce/billing-form-fields.php @@ -23,12 +23,15 @@ use Tutor\Components\Constants\Variant; use Tutor\Components\Constants\InputType; use Tutor\Components\InputField; +use Tutor\Ecommerce\BillingController; +use Tutor\Ecommerce\Settings; use TUTOR\Icon; $show_close_button = $show_close_button ?? false; $close_action = $close_action ?? 'editBilling = false'; $country_options = $country_options ?? array(); $initial_states = $initial_states ?? array(); +$required = Settings::get_required_billing_fields(); ?> @@ -67,7 +70,7 @@ ->id( 'billing_first_name' ) ->required() ->placeholder( __( 'Enter your first name', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_first_name', { required: true })" ) + ->attr( 'x-bind', "register('billing_first_name', " . BillingController::get_client_rules( 'billing_first_name', true ) . ')' ) ->render(); InputField::make() @@ -76,7 +79,7 @@ ->id( 'billing_last_name' ) ->required() ->placeholder( __( 'Enter your last name', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_last_name', { required: true })" ) + ->attr( 'x-bind', "register('billing_last_name', " . BillingController::get_client_rules( 'billing_last_name', true ) . ')' ) ->render(); ?> @@ -96,9 +99,9 @@ ->name( 'billing_phone' ) ->label( __( 'Phone', 'tutor' ) ) ->id( 'billing_phone' ) - ->required() + ->required( $required['billing_phone'] ) ->placeholder( __( 'Enter your phone number', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_phone', { required: true })" ) + ->attr( 'x-bind', "register('billing_phone', " . BillingController::get_client_rules( 'billing_phone', $required['billing_phone'] ) . ')' ) ->render(); InputField::make() @@ -108,9 +111,9 @@ ->options( $country_options ) ->searchable() ->id( 'billing_country' ) - ->required() + ->required( $required['billing_country'] ) ->placeholder( __( 'Enter your country', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_country', { required: true })" ) + ->attr( 'x-bind', "register('billing_country', " . BillingController::get_client_rules( 'billing_country', $required['billing_country'] ) . ')' ) ->render(); InputField::make() @@ -132,18 +135,18 @@ ->name( 'billing_city' ) ->label( __( 'City', 'tutor' ) ) ->id( 'billing_city' ) - ->required() + ->required( $required['billing_city'] ) ->placeholder( __( 'Enter your city', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_city', { required: true })" ) + ->attr( 'x-bind', "register('billing_city', " . BillingController::get_client_rules( 'billing_city', $required['billing_city'] ) . ')' ) ->render(); InputField::make() ->name( 'billing_zip_code' ) ->label( __( 'Postcode/ Zip', 'tutor' ) ) ->id( 'billing_zip_code' ) - ->required() + ->required( $required['billing_zip_code'] ) ->placeholder( __( 'Enter your postcode/ zip', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_zip_code', { required: true })" ) + ->attr( 'x-bind', "register('billing_zip_code', " . BillingController::get_client_rules( 'billing_zip_code', $required['billing_zip_code'] ) . ')' ) ->render(); ?> @@ -155,8 +158,8 @@ ->name( 'billing_address' ) ->label( __( 'Address', 'tutor' ) ) ->id( 'billing_address' ) - ->required() + ->required( $required['billing_address'] ) ->placeholder( __( 'Enter your address', 'tutor' ) ) - ->attr( 'x-bind', "register('billing_address', { required: true })" ) + ->attr( 'x-bind', "register('billing_address', " . BillingController::get_client_rules( 'billing_address', $required['billing_address'] ) . ')' ) ->render(); ?> diff --git a/templates/ecommerce/checkout-billing-form-fields.php b/templates/ecommerce/checkout-billing-form-fields.php index 4187ade127..fa68d989ba 100644 --- a/templates/ecommerce/checkout-billing-form-fields.php +++ b/templates/ecommerce/checkout-billing-form-fields.php @@ -10,6 +10,7 @@ */ use Tutor\Ecommerce\BillingController; +use Tutor\Ecommerce\Settings; $billing_controller = new BillingController( false ); $billing_info = $billing_controller->get_billing_info(); @@ -26,6 +27,10 @@ $country_info = tutor_get_country_info_by_name( $billing_country ); $states = $country_info && isset( $country_info['states'] ) ? $country_info['states'] : array(); + +$required_attr = function ( $field ) { + return Settings::is_billing_field_required( $field ) ? 'required' : ''; +}; ?>
@@ -34,7 +39,7 @@ - + >
@@ -43,7 +48,7 @@ - + > @@ -60,7 +65,7 @@ - > - + > @@ -111,7 +116,7 @@ - + > @@ -120,7 +125,7 @@ - + > @@ -129,7 +134,7 @@ - + >