Using new coupon_info metadata, if it exists, to determine the actual coupon id (#48262)

* 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 <github-actions@github.com>
Co-authored-by: Néstor Soriano <konamiman@konamiman.com>
This commit is contained in:
David Marín 2024-09-09 17:00:37 +02:00 committed by GitHub
parent 72112bd5d4
commit 7f89cd5090
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View File

@ -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.

View File

@ -118,8 +118,14 @@ if ( wc_tax_enabled() ) {
<li><strong><?php esc_html_e( 'Coupon(s)', 'woocommerce' ); ?></strong></li>
<?php
foreach ( $coupons as $item_id => $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';
?>
<li class="<?php echo esc_attr( $class ); ?>">
<?php if ( $post_id ) : ?>