3

Is it possible to have a custom field (created using ACF) next to each item in Woocommerce transaction details?

I have a field called ‘shpng’ which contains the shipping date, and it is changed daily from syncsd import file and ends up in the database under the shpng field of each product.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Wed
  • 323
  • 1
  • 9
  • 20

1 Answers1

3

Try the following that should display your ACF value under the item name in email notifications:

// Display Items Shipping ACF custom field value in email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
    // Targeting email notifications only
    if( is_wc_endpoint_url() ) 
        return $item_name;

    // Get the WC_Product object (from order item)
    $product = $item->get_product();

    if( $shpng_value = get_field('shpng', $product->get_id()) ) {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping Date', 'woocommerce' ) . ': </strong>' . $shpng_value . '</p>';
    }
    return $item_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Continuation of: Display product custom fields in cart next to each item name in Woocommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • that works like a charm. In case I want to display SKU value (and use it for IF condition) how do I put it into variable? Is this correct: $item_sku = $product->get_sku() – Wed Feb 21 '19 at 17:13