wordpress - WooCommerce:在管理端订单列表页面自定义列中显示 SKU 编号
问题描述
我在 Woocommerce 管理侧订单列表页面中添加了一个自定义列,以使用以下代码显示已订购项目产品名称和总数量 -> [ http://prntscr.com/p11rdl] :
add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
$order_columns['order_products'] = "Qty-Products Name-SKU";
return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '" target="_blank">'. $order_item['name'] .'</a><br />';
}
}
}
}
现在我还想在产品名称后显示订购的产品 SKU 编号。所以任何人都有解决方案然后请帮助我。
谢谢你,
凯坦
解决方案
只需替换为以下代码片段即可添加 SKU -
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
$product = $order_item->get_product();
// product checking
$sku = ( $product && $product->get_sku() ) ? ' - ' . $product->get_sku() : '';
echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '" target="_blank">'. $order_item['name'] . '</a>'. $sku . '<br />';
}
}
}
}
推荐阅读
- c++11 - 在这种情况下,我需要如何从“operator->()”返回值?“常量”还是“非常量”?
- csv - 允许普通用户下载 PloneFormGen CSV
- php - 如何通过 vimeo api 获取视频统计信息
- sql-server - Spring Data JPA“列名ID无效”
- c# - 触发后WPF控件未返回先前状态
- integration - 如何使用 IDOC_Asynchronous_Inbound 构建 IDOC 并将其从 MII 发送到 SAP ECC
- file - SSIS有条件地处理文件夹中的文件
- matlab - 使用多个 excel 文件的曲面图
- php - 如何在表单的“动作”属性中传递变量?
- javascript - JSON.stringify 多维