We add vendor information in the admin order details for each order via:
Now I also want to add this information also in the order preview. I found this answer
We change the hook to woocommerce_admin_order_preview_end
but now when we want to open the preview nothing happend.
Do we have to adjust the whole code in order that it works for the order preview or why is our approach not working?
function action_woocommerce_admin_order_vendor_data( $order ) { // Empty array $shop_names = array(); // Output echo '<strong>' . __( 'Vendor(s): ', 'woocommerce' ) . '</strong>'; // Loop through order items foreach ( $order->get_items() as $item ) { // Get product object $product = $item->get_product(); // Author id $author_id = $product->post->post_author; // Shopname $vendor = dokan()->vendor->get( $author_id ); $shop_name = $vendor->get_shop_name(); // OR JUST USE THIS FOR SHOPNAME // Shop name // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); // NOT in array if ( ! in_array( $shop_name, $shop_names ) ) { // Push to array $shop_names[] = $shop_name; // Output echo $shop_name . ', '; } } } add_action('woocommerce_admin_order_preview_end', 'action_woocommerce_admin_order_vendor_data', 10, 1 );
Advertisement
Answer
As already explained in the link you referred to (By LoicTheAztec). You can’t get the order
object as it’s a template that loads specific data via Ajax and there is no arguments for woocommerce_admin_order_preview_start
action hook
Instead the filter hook woocommerce_admin_order_preview_get_order_details
will allow you first to add some custom data that you will be able to call and display it via woocommerce_admin_order_preview_start
or woocommerce_admin_order_preview_end
action hook
So you get:
// Add custom order meta data to make it accessible in order preview template function filter_woocommerce_admin_order_preview_get_order_details( $data, $order ) { // Empty array $shop_names = array(); // Loop through order items foreach ( $order->get_items() as $item ) { // Get product object $product = $item->get_product(); // Author id $author_id = $product->post->post_author; // Shopname $vendor = dokan()->vendor->get( $author_id ); $shop_name = $vendor->get_shop_name(); // OR JUST USE THIS FOR SHOPNAME // Shop name // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); // NOT in array if ( ! in_array( $shop_name, $shop_names ) ) { // Push to array $shop_names[] = $shop_name; } } // NOT empty if ( ! empty ( $shop_names ) ) { // Store the value in the data array $data['shop_names'] = implode( '<br>', $shop_names ); } return $data; } add_filter( 'woocommerce_admin_order_preview_get_order_details', 'filter_woocommerce_admin_order_preview_get_order_details', 10, 2 ); // Display custom values in order preview function action_woocommerce_admin_order_preview_start() { // Output echo '<div class="wc-order-preview-wrapper">'; echo '<div class="wc-order-preview-shop-names" style="padding:1.5em 1.5em 0; box-sizing:border-box;">'; // H2 echo '<h2>' . __( 'Vendor(s)', 'woocommerce' ) . '</h2>'; // Call the stored value and display it echo '{{{ data.shop_names }}}'; // Close echo '</div></div>'; } add_action( 'woocommerce_admin_order_preview_start', 'action_woocommerce_admin_order_preview_start', 10, 0 );