首页 > 解决方案 > 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 编号。所以任何人都有解决方案然后请帮助我。

谢谢你,
凯坦

标签: wordpresswoocommerceordersskuwordpress-admin

解决方案


只需替换为以下代码片段即可添加 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 />';
      }
    }
  } 
}

推荐阅读