0

I am trying to add the product name to the sku when the sku is empty using the following method. So far this works but the last thing is, how do I call the product name within this code?

add_action( 'woocommerce_add_order_item_meta', 'so_28193771', 10, 3 );
function so_28193771( $item_id, $values, $cart_item_key ) {

    $item_sku  .=  get_post_meta( $values[ 'product_id' ], '_sku', true );

    if ( empty( $item_sku ) ) {

        wc_add_order_item_meta( $item_id, 'sku', $item_sku , false );
    }
}
Paul
  • 53
  • 4

1 Answers1

0

Use the below code inside your function.

WooCommerce 3.0+

Get Product id from order id in Woocommerce

$order = wc_get_order( $order_id );
$items = $order->get_items();

Then if you loop through the items, you can get all the relevant data:

foreach ( $items as $item ) {
    $product_name = $item->get_name();
    $product_id = $item->get_product_id();
    $product_variation_id = $item->get_variation_id();
}

Pre-WooCommerce 3.0

$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_variation_id = $item['variation_id'];
}
Rajeev
  • 1,376
  • 2
  • 15
  • 33