在WooCommerce管理员订单详细信息页面中添加自定义列以订购项目并使其可排序 [英] Add a custom column to order items and make it sortable in WooCommerce admin order details page

查看:0
本文介绍了在WooCommerce管理员订单详细信息页面中添加自定义列以订购项目并使其可排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力将项目产品属性添加到我管理的管理订单详细信息页面。您可以看到标题为";包装重量&的额外专栏。

// Add custom column headers here
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
    // set the column name
    $column_name = 'Packing Weight';

    // display the column name
    echo '<th>' . $column_name . '</th>';
}

// Add custom column values here
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
    // get the post meta value from the associated product
    // $value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
    $value = array_shift( wc_get_product_terms( $_product->post->ID, 'pa_packing-order', array( 'fields' => 'names' ) ) );

    // display the value
    echo '<td>' . $value . '</td>';
}
但是,理想情况下,我希望按此列的顺序对项目进行排序,例如,非常软、软、硬。我很乐意将包装重量的值更改为1-10的数字范围,如果这会使订购更容易的话。

我该如何着手进行此操作?

推荐答案

  • 请注意,我已经添加了一些CSS类,以使其可排序
  • 还请注意,我使用了一个字符串数组作为输出,并随机显示它们。将其替换为您自己的代码。这是因为我的产品不包含特定术语

因此,要使添加到管理订单项目的自定义列可排序,您可以使用:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'Packing Weight', 'woocommerce' );
    
    // Display the column name
    echo '<th class="line_packing_weight sortable" data-sort="string-ins">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

//Add content
function action_woocommerce_admin_order_item_values( $product = null, $item, $item_id ) {
    //  WC_Order_Refund OR WC_Order_item
    if ( $item->get_type() == 'shop_order_refund' ) {
        $item = new WC_Order_Refund( $item_id );
    } else {
        $item = new WC_Order_Item_Product( $item_id );

        // Only for "line_item" items type, to avoid errors
        if ( ! $item->is_type( 'line_item' ) ) return;
    }
    
    // Replace this part with your own code
    $some_strings = array( 'Soft', 'Very soft', 'Hard' );
    
    // Replace this part with your own code
    $value = $some_strings[array_rand( $some_strings )];
    
    if ( $value ) { 
        echo '<td class="line_packing_weight" data-sort-value="' . $value . '">' . $value . '</td>';
    }
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

这篇关于在WooCommerce管理员订单详细信息页面中添加自定义列以订购项目并使其可排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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