LLMS_AJAX_Handler::validate_coupon_code( array $request )
Validate a Coupon via the Checkout Form
Parameters Parameters
- $request
-
(array) (Required) $_POST data.
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.
Source Source
File: includes/class.llms.ajax.handler.php
if ( ! in_array( $request['status'], array( 'add', 'remove', 'delete' ), true ) ) {
return new WP_Error( 400, __( 'Invalid status', 'lifterlms' ) );
}
$student_id = intval( $request['student_id'] );
$post_id = intval( $request['post_id'] );
switch ( $request['status'] ) {
case 'add':
$res = llms_enroll_student( $student_id, $post_id, 'admin_' . get_current_user_id() );
break;
case 'remove':
$res = llms_unenroll_student( $student_id, $post_id, 'cancelled', 'any' );
break;
case 'delete':
$res = llms_delete_student_enrollment( $student_id, $post_id, 'any' );
break;
}
if ( ! $res ) {
// Translators: %s = action add|remove|delete.
return new WP_Error( 400, sprintf( __( 'Action "%1$s" failed. Please try again', 'lifterlms' ), $request['status'] ) );
}
return array(
'success' => true,
);
}
/**
* Validate a Coupon via the Checkout Form
*
* @since 3.0.0
* @since 3.39.0 Minor changes to code for readability with no changes to function behavior.
* @since 4.21.1 Sanitize user-submitted coupon code before outputting in error messages.
*
* @param array $request $_POST data.
* @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.
*/
public static function validate_coupon_code( $request ) {
$error = new WP_Error();
$request['code'] = ! empty( $request['code'] ) ? sanitize_text_field( $request['code'] ) : '';
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',
Expand full source code Collapse full source code View on GitHub
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. |