繁体   English   中英

如何在woocommerce的新订单电子邮件中显示高级自定义字段数据

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

我有高级自定义字段插件,我在我的产品中添加了一个自定义字段,标签名称是 (stock_number) 我的问题是它如何在新订单电子邮件中显示/显示此字段数据。

下面的代码应该可以解决问题。

选项1:

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

选项2:

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

选项3:

以下代码将使用 ACF 自定义值替换产品标题。

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

选项4:

以下代码将用您的自定义字段替换产品名称。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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