简体   繁体   中英

Display product ACF field value in Woocommerce transaction emails

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM