From 0a68c8963b2d975e1253a64d567ba92e2a474c74 Mon Sep 17 00:00:00 2001 From: David Parker Date: Wed, 8 Jan 2025 15:22:31 -0500 Subject: [PATCH] Using the discount code set on the level object when possible --- includes/checkout.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/includes/checkout.php b/includes/checkout.php index f5c03f1fb..5f3264a83 100644 --- a/includes/checkout.php +++ b/includes/checkout.php @@ -69,6 +69,7 @@ function pmpro_save_checkout_data_to_order( $order ) { update_pmpro_membership_order_meta( $order->id, 'checkout_level', $pmpro_level_arr ); // Save the discount code. + // @TODO: Remove this in v4.0. Discount codes should be set on the level object. update_pmpro_membership_order_meta( $order->id, 'checkout_discount_code', $discount_code ); // Save any files that were uploaded. @@ -156,6 +157,7 @@ function pmpro_pull_checkout_data_from_order( $order ) { $pmpro_level = (object) $checkout_level_arr; // Set $discount_code_id. + // @TODO: Remove this in v4.0. Discount codes should be set on the level object. $discount_code = get_pmpro_membership_order_meta( $order->id, 'checkout_discount_code', true ); // Set $_REQUEST. @@ -218,7 +220,13 @@ function pmpro_complete_checkout( $order ) { $enddate = apply_filters( "pmpro_checkout_end_date", $enddate, $order->user_id, $pmpro_level, $startdate ); // If we have a discount code but not the ID, get the ID. - if ( ! empty( $discount_code ) && empty( $discount_code_id ) ) { + if ( ! empty( $pmpro_level->discount_code ) ) { + $discount_code = $pmpro_level->discount_code; + $discount_code_id = empty( $pmpro_level->code_id ) ? $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql( $discount_code ) . "' LIMIT 1" ) : $pmpro_level->code_id; + } elseif ( ! empty( $discount_code ) && empty( $discount_code_id ) ) { + // Throw a doing it wrong warning. If a discount code is being used, it should be set on the level. + // @TODO: Remove this in v4.0 along with references to the discount code globals. Discount codes should be set on the level object. + _doing_it_wrong( __FUNCTION__, __( 'Discount codes should be set on the $pmpro_level object.', 'paid-memberships-pro' ), 'TBD' ); $discount_code_id = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql( $discount_code ) . "' LIMIT 1" ); }