3

We use this code to display the used coupons on a order. This is working fine! But now we want to show this information also in the order quick view.

I found this hook here: woocommerce_admin_order_preview_end

So i tried to change the hook from the code below with this hook. But then the quick view function does not work at all. When we click on the "eye" to open the quick view - nothing happend. Do we have to adjust the code more or what is here the problem?

add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );


/**
 * Add used coupons to the order edit page
 *
*/
function custom_checkout_field_display_admin_order_meta($order){

    if( $order->get_used_coupons() ) {
    
        $coupons_count = count( $order->get_used_coupons() );
    
        echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
         
        echo '<p><strong>' . __('Coupons used') . ':</strong> ';
        
        $i = 1;
        
        foreach( $order->get_used_coupons() as $coupon) {
            echo $coupon;
            if( $i < $coupons_count )
                echo ', ';
            $i++;
        }
        
        echo '</p>';
    }

}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nik7
  • 346
  • 2
  • 16

1 Answers1

3

Try to use the following to display coupon used in admin order quick view (preview):

// Add custom order data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_data', 10, 2 );
function admin_order_preview_add_custom_data( $data, $order ) {
    // Replace '_custom_meta_key' by the correct postmeta key
    if( $coupons = $order->get_used_coupons() ) {
        $data['coupons_count'] = count($coupons); // <= Store the count in the data array.
        $data['coupons_codes'] = implode(', ', $coupons); // <= Store the count in the data array.
    }
        

    return $data;
}

// Display The data in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
    // Call the stored value and display it
    echo '<div><strong>' . __('Coupons used') . ' ({{data.coupons_count}})<strong>: {{data.coupons_codes}}</div><br>';
}

Code goes in functions.php file of the active child theme (or active theme). Untested it should works.

Based on: Display custom data on Woocommerce admin order preview

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! This is working. But "woocommerce_admin_order_preview_end" is not the best place. So I try to use the "woocommerce_admin_order_preview_start" but this us also not nice. It would be nice if we can insert this between the invoice details and order table details (https://ibb.co/09Tk512) . For this I tried to use the "woocommerce_admin_order_preview_get_order_details" hook. But then is does not work. – Nik7 Jan 09 '21 at 21:21
  • I have one last question. Sorry :-) .. How do I have to adjust the code in order to display also the coupon description there if there is one? – Nik7 Jan 09 '21 at 21:38
  • 1
    @Nik7 You should better ask a new question, as this can't be answered as a comment. – LoicTheAztec Jan 09 '21 at 21:45
  • Okey. Thanks a lot for this solution here. Will create a other question :) – Nik7 Jan 09 '21 at 23:11