Sameer
Sameer

Reputation: 62

How to display Advanced custom field data in new order email in woocommerce

I have advanced custom field plugin and i have added a custom field to my product the label name is (stock_number) my question is how do it display/show this field data in the new order email.

Upvotes: 2

Views: 957

Answers (1)

Rajeev
Rajeev

Reputation: 1488

The below code should do the trick.

Option1:

add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text ) {

    if( $stock_number = get_field( 'stock_number', $item->get_product_id() ) ;
        echo $stock_number;
}

Option2:

add_action( 'woocommerce_email_order_details', 'display_stock_email_order_details', 10, 4 );

function display_stock_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {

    foreach( $order->get_items() as $item ) {
        if( $stock_number = get_field( "stock_number", $item->get_product_id() ) ){
            echo '<p><strong>'.__('Stock Number').': </strong>'.$stock_number.'</p>';
        }
    }
}

Option3:

The below code will replace the Product Title with the ACF Custom Value.

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( $stock_number = get_field('stock_number', $product->get_id()) ) {
        $item_name = '<p class="item-stck" style="margin:12px 0 0;">
        <strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
    }
    return $item_name;
}

Option4:

The below code will replace the Product Name with your custom field.

add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {

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

    if( $stock_number = get_field('stock_number', $product->get_id()) ) {
        $item_name = '<p class="item-stck" style="margin:12px 0 0;">
        <strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
    }
    return $item_name;
}

Upvotes: 4

Related Questions