Last active
December 3, 2024 07:05
-
-
Save barbareshet/dd32c275a7278381c784940f579d161c to your computer and use it in GitHub Desktop.
Adding Coupon Column to Woocommerce Orders admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add a new column to the orders list | |
add_filter( 'manage_edit-shop_order_columns', 'add_coupon_code_column', 20 ); | |
function add_coupon_code_column( $columns ) { | |
// Insert the new column after the 'order_status' column | |
$new_columns = array(); | |
foreach ( $columns as $key => $column ) { | |
$new_columns[ $key ] = $column; | |
if ( 'order_status' === $key ) { | |
$new_columns['coupons_used'] = __( 'Coupons Used', 'your-textdomain' ); | |
} | |
} | |
return $new_columns; | |
} | |
// Populate the custom column with coupon data | |
add_action( 'manage_shop_order_posts_custom_column', 'populate_coupon_code_column' ); | |
function populate_coupon_code_column( $column ) { | |
global $post; | |
if ( 'coupons_used' === $column ) { | |
$order = wc_get_order( $post->ID ); | |
$coupons = $order->get_coupon_codes(); // Get the coupon codes | |
if ( ! empty( $coupons ) ) { | |
echo implode( ', ', $coupons ); | |
} else { | |
echo 'N/A'; // If no coupon was used | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment