LLMS_AJAX_Handler::validate_coupon_code( array $request )

Validate a Coupon via the Checkout Form


Parameters Parameters

$request

(array) (Required) $_POST data.


Top ↑

Return Return

(array|WP_Error) On success, returns an array containing HTML parts used to update the interface of the checkout screen. On error, returns an error object with details of the encountered error.


Top ↑

Source Source

File: includes/class.llms.ajax.handler.php

1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
    if ( empty( $request['code'] ) ) {
 
        $error->add( 'error', __( 'Please enter a coupon code.', 'lifterlms' ) );
 
    } elseif ( empty( $request['plan_id'] ) ) {
 
        $error->add( 'error', __( 'Please enter a plan ID.', 'lifterlms' ) );
 
    } else {
 
        $cid = llms_find_coupon( $request['code'] );
 
        if ( ! $cid ) {
 
            // Translators: %s = coupon code.
            $error->add( 'error', sprintf( __( 'Coupon code "%s" not found.', 'lifterlms' ), $request['code'] ) );
 
        } else {
 
            $coupon = new LLMS_Coupon( $cid );
            $valid  = $coupon->is_valid( $request['plan_id'] );
 
            if ( is_wp_error( $valid ) ) {
 
                $error = $valid;
 
            } else {
 
                llms()->session->set(
                    'llms_coupon',
                    array(
                        'plan_id'   => $request['plan_id'],
                        'coupon_id' => $coupon->get( 'id' ),
                    )
                );
 
                $plan = new LLMS_Access_Plan( $request['plan_id'] );
 
                ob_start();
                llms_get_template(
                    'checkout/form-coupon.php',
                    array(
                        'coupon' => $coupon,
                    )
                );
                $coupon_html = ob_get_clean();
 
                ob_start();
                llms_get_template(
                    'checkout/form-gateways.php',
                    array(
                        'coupon'           => $coupon,
                        'gateways'         => llms()->payment_gateways()->get_enabled_payment_gateways(),
                        'selected_gateway' => llms()->payment_gateways()->get_default_gateway(),
                        'plan'             => $plan,
                    )
                );
                $gateways_html = ob_get_clean();
 
                ob_start();
                llms_get_template(
                    'checkout/form-summary.php',
                    array(
                        'coupon'  => $coupon,
                        'plan'    => $plan,
                        'product' => $plan->get_product(),
                    )
                );
                $summary_html = ob_get_clean();
 
                return array(
                    'code'          => $coupon->get( 'title' ),
                    'coupon_html'   => $coupon_html,
                    'gateways_html' => $gateways_html,
                    'summary_html'  => $summary_html,
                );
 
            }
        }
    }
 
    return $error;
 
}
 
/**
 * Create course's section.
 *
 * @since Unknown


Top ↑

Changelog Changelog

Changelog
Version Description
4.21.1 Sanitize user-submitted coupon code before outputting in error messages.
3.39.0 Minor changes to code for readability with no changes to function behavior.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.