更改订单项目自定义元数据在WooCommerce Admin订单中显示的标签和值 [英] Change order item custom meta data displayed label and value in WooCommerce Admin orders

查看:63
本文介绍了更改订单项目自定义元数据在WooCommerce Admin订单中显示的标签和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce管理员屏幕中,我试图使用订单行元数据来显示一个按钮,该按钮将打开一个包含货运公司供应商URL的新窗口.我已经成功从订单中的产品中提取了供应商URL,并将其推送到订单行项目.

In the Woocommerce admin screen, I'm attempting to use the order line meta data to display a button which will open up a new window with the URL of the dropship supplier. I have successfully pulled the supplier URL from the product on order and pushed it to the order line item.

我能够将元数据更改为按钮,但是这样做的结果是擦除了包含自定义选项的其他自定义字段.

I am able to change the meta data to a button but the consequence of that is the other custom fields which contain the custom options are wiped.

这是我添加到 functions.php 文件

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $item->get_product_id(), 'supplier_url', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        $item->update_meta_data( '_supplier', $custom_field_value );
    }
}

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change display_key
    if( $meta->key === '_supplier' && is_admin() )
        $display_key = __("Supplier", "woocommerce" );

    return $display_key;    
}

add_filter( 'woocommerce_order_item_display_meta_value', 'change_order_item_meta_value', 20, 3 );
function change_order_item_meta_value( $value, $meta, $item ) {

    // Display supplier meta value as a button
    if( $meta->key === '_supplier' && is_admin() ) {
        $display_value = __('<a class="button" target="_blank" href="'.$value.'">Order</a>', 'woocommerce' );

        return $display_value;    
    }
}

这些图像显示了使用最后一个代码块的前后.

These images show the before and after of using the last block of code.

之前:

之后:

我的代码哪里出错了,我正在努力实现什么目标?

Where have I gone wrong with my code and is what i'm trying to achieve possible?

推荐答案

主要错误在于最后一个函数,其中应将 $ display_value 替换为 $ value ,然后 return $ value; 应该位于最后一个括号之前的末尾.

The main mistake is on last function where $display_value should be replaced with just $value and then return $value; should be located at the end before last closing bracket.

我还重新访问了您的所有代码:

I have also revisited all your code:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    $supplier_url = $values['data']->get_meta( 'supplier_url' ); // Get product custom field value
    
    // add product custom field as custom order item meta data
    if ( ! empty($supplier_url) ){
        $item->update_meta_data( '_supplier', $supplier_url );
    }
}

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change displayed label for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_supplier' ) {
        $display_key = __("Supplier", "woocommerce" );
    }
    return $display_key;
}

add_filter( 'woocommerce_order_item_display_meta_value', 'change_order_item_meta_value', 20, 3 );
function change_order_item_meta_value( $value, $meta, $item ) {

    // Change displayed value for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_supplier' ) {
        $value = __('<a class="button" target="_blank" href="'.$value.'">Order</a>', 'woocommerce' );
    }
    return $value;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于更改订单项目自定义元数据在WooCommerce Admin订单中显示的标签和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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