skip to Main Content

I have added a custom column in Woocommerce admin side order listing page to display Ordered item Products name and total Quantity like this -> [http://prntscr.com/p11rdl] by using the following code:

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 />';
      }
    }
  } 
}

Now I also want to display ordered product SKU number after the product name. So anyone have solution for this then please provide help me.

Thank you,
Ketan

2

Answers


  1. Just replace with follows code snippets to add 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 />';
          }
        }
      } 
    }
    
    Login or Signup to reply.
  2. Thank you so much I’ve found this thread extremely helpful, I couldn’t thank the original poster enough as well the help from @itzmekhokan and various other contributors, this community is awesome.

    What I’m needing now is to add product shelve location straight after SKU.
    I have the below code for location fields but not sure how to amend to the code above to make it work. I’m new to coding in general, but have found snippets of code and made it work so far.

    Your help and guidance would be greatly appreciated.

    // Add Location field
    add_action( 'woocommerce_product_options_stock_fields', 'product_shelf' ); 
    function product_shelf() {
        global $woocommerce, $post;
        woocommerce_wp_text_input(
            array(
                'id'          => 'product_shelf',
                'placeholder' => 'Enter product shelf location here',
                'label'       => 'Shelf Location',
                'description' => 'Shelf location of product',
                'desc_tip'    => 'true',
            )
        );
    }
    
    // Save location data
    add_action( 'woocommerce_process_product_meta', 'product_shelf_data' );
    function product_shelf_data( $post_id ) {   
        // grab the location from $_POST
        $product_shelf = isset( $_POST[ 'product_shelf' ] ) ? sanitize_text_field( $_POST[ 'product_shelf' ] ) : '';
    
        // grab the product
        $product = wc_get_product( $post_id );
    
        // save the location using WooCommerce built-in functions
        $product->update_meta_data( 'product_shelf', $product_shelf_data ); 
        $product->save();   
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search