在 WooCommerce 管理订单预览中显示供应商商店名称 (Dokan) [英] Display vendor store-name (Dokan) on WooCommerce admin order preview

查看:44
本文介绍了在 WooCommerce 管理订单预览中显示供应商商店名称 (Dokan)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我们将钩子更改为 woocommerce_admin_order_preview_end 但现在当我们想打开预览时什么也没发生.

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

function action_woocommerce_admin_order_vendor_data( $order ) {//空数组$shop_names = array();//输出echo ''.__( '供应商:', 'woocommerce') .'</strong>';//遍历订单项foreach ( $order->get_items() as $item ) {//获取产品对象$product = $item->get_product();//作者 ID$author_id = $product->post->post_author;//店名$vendor = dokan()->vendor->get($author_id);$shop_name = $vendor->get_shop_name();//或仅将其用于 SHOPNAME//店铺名称//$shop_name = dokan()->vendor->get( $author_id )->get_shop_name();//不在数组中如果(!in_array($shop_name,$shop_names)){//推送到数组$shop_names[] = $shop_name;//输出回声 $shop_name .', ';}}}add_action('woocommerce_admin_order_preview_end', 'action_woocommerce_admin_order_vendor_data', 10, 1);

解决方案

如您所引用的链接中已经解释的那样 (By LoicTheAztec).您无法获得 order 对象,因为它是一个通过 Ajax 加载特定数据的模板,并且没有用于 woocommerce_admin_order_preview_start 动作挂钩的参数

相反,过滤器钩子 woocommerce_admin_order_preview_get_order_details 将允许您首先添加一些自定义数据,您可以通过woocommerce_admin_order_preview_startwoocommerce_admin_order_preview_end 调用和显示这些数据代码>动作钩子

所以你得到:

//添加自定义订单元数据,使其在订单预览模板中可访问函数 filter_woocommerce_admin_order_preview_get_order_details( $data, $order ) {//空数组$shop_names = array();//遍历订单项foreach ( $order->get_items() as $item ) {//获取产品对象$product = $item->get_product();//作者 ID$author_id = $product->post->post_author;//店名$vendor = dokan()->vendor->get($author_id);$shop_name = $vendor->get_shop_name();//或仅将其用于 SHOPNAME//店铺名称//$shop_name = dokan()->vendor->get( $author_id )->get_shop_name();//不在数组中如果(!in_array($shop_name,$shop_names)){//推送到数组$shop_names[] = $shop_name;}}//不是空的如果(!空($shop_names)){//将值存储在数据数组中$data['shop_names'] = implode('
', $shop_names);}返回 $data;}add_filter( 'woocommerce_admin_order_preview_get_order_details', 'filter_woocommerce_admin_order_preview_get_order_details', 10, 2 );//在订单预览中显示自定义值函数 action_woocommerce_admin_order_preview_start() {//输出echo '

';echo '

';//H2回声'<h2>'.__( '供应商', 'woocommerce') .'</h2>';//调用存储的值并显示它echo '{{{ data.shop_names }}}';//关闭回声'</div></div>';}add_action('woocommerce_admin_order_preview_start', 'action_woocommerce_admin_order_preview_start', 10, 0);

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

解决方案

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

这篇关于在 WooCommerce 管理订单预览中显示供应商商店名称 (Dokan)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆