Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>';
    }

}
question from:https://stackoverflow.com/questions/65645923/show-used-coupon-in-woocommerce-order-quick-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
340 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...