在 WooCommerce 管理员订单预览中显示供应商商店名称 (Dokan)

Display vendor store-name (Dokan) on WooCommerce admin order preview

我们通过以下方式在每个订单的管理订单详细信息中添加供应商信息:

现在我也想在订单预览中添加此信息。我找到了这个答案

我们将挂钩更改为 woocommerce_admin_order_preview_end 但是现在当我们想要打开预览时没有任何反应。

我们是否必须调整整个代码才能使其适用于订单预览,或者为什么我们的方法不起作用?

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 );

正如您提到的 link 中所解释的(作者 LoicTheAztec)。您无法获取 order 对象,因为它是一个通过 Ajax 加载特定数据的模板,并且 woocommerce_admin_order_preview_start 操作挂钩

没有参数

过滤器挂钩 woocommerce_admin_order_preview_get_order_details 将允许您首先添加一些自定义数据,您可以通过 woocommerce_admin_order_preview_startwoocommerce_admin_order_preview_end 操作挂钩 [=17] 调用和显示这些数据=]

所以你得到:

// 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 );