From 7f89cd5090bbf45e8f74e9dd0359fd93ad933c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mar=C3=ADn?= <292309+davefx@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:00:37 +0200 Subject: [PATCH] Using new coupon_info metadata, if it exists, to determine the actual coupon id (#48262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Using new coupon_info metadata to determine the actual coupon id Changes to make use of the new coupon_info meta data, if it exists, to determine the actually-used coupon. If coupon_info meta data doesn't exist, I'm also adding an extra filter so, in case there are more than one active coupons with the same name, we filter out the ones that were created after the order. --------- Co-authored-by: github-actions Co-authored-by: NĂ©stor Soriano --- plugins/woocommerce/changelog/48262-patch-16 | 4 ++++ .../admin/meta-boxes/views/html-order-items.php | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/48262-patch-16 diff --git a/plugins/woocommerce/changelog/48262-patch-16 b/plugins/woocommerce/changelog/48262-patch-16 new file mode 100644 index 00000000000..0bb1bf32a6e --- /dev/null +++ b/plugins/woocommerce/changelog/48262-patch-16 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix the order-items view for coupons, so we show the actually-used coupon info, if it exists. \ No newline at end of file diff --git a/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php b/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php index f7f40056007..bb2cf596d79 100644 --- a/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php +++ b/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php @@ -118,8 +118,14 @@ if ( wc_tax_enabled() ) {
  • $item ) : - $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' LIMIT 1;", $item->get_code() ) ); // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited - $class = $order->is_editable() ? 'code editable' : 'code'; + $coupon_info = $item->get_meta( 'coupon_info' ); + if ( $coupon_info ) { + $coupon_info = json_decode( $coupon_info, true ); + $post_id = $coupon_info[0]; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } else { + $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' AND post_date < %s LIMIT 1;", $item->get_code(), $order->get_date_created()->format( 'Y-m-d H:i:s' ) ) ); // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited + } + $class = $order->is_editable() ? 'code editable' : 'code'; ?>