【问题标题】:How to display Advanced custom field data in new order email in woocommerce如何在 woocommerce 的新订单电子邮件中显示高级自定义字段数据
【发布时间】:2020-02-12 13:15:20
【问题描述】:

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

【问题讨论】:

  • 我已经检查过了,代码不起作用,下订单时显示内部服务器错误

标签: wordpress woocommerce hook-woocommerce


【解决方案1】:

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

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

【讨论】:

  • 您好,选项 2 确实有效,但有一个小问题,它在订单号上方显示库存编号,我希望它显示在下方或替换产品的标题/名称
  • 您要删除产品的标题吗?
  • 刚刚注意到这个代码只是在邮件中用库存号替换了产品标题,在订单确认页面和订单中没有。
  • 是的,但我要说的是我怎样才能改变订单中的标题
  • 我还有一个问题,如果你能回答的话,你可以在下面的链接中找到它。 stackoverflow.com/questions/60188986/…
猜你喜欢
  • 2020-12-29
  • 2020-09-09
  • 2021-03-29
  • 2019-05-09
  • 1970-01-01
  • 2019-09-22
  • 2020-09-29
  • 2018-08-29
  • 2019-05-21
相关资源
最近更新 更多