【发布时间】:2020-02-12 13:15:20
【问题描述】:
我有高级自定义字段插件,并且我在我的产品中添加了一个自定义字段,标签名称是 (stock_number) 我的问题是它如何在新订单电子邮件中显示/显示此字段数据。
【问题讨论】:
-
我已经检查过了,代码不起作用,下订单时显示内部服务器错误
标签: wordpress woocommerce hook-woocommerce
我有高级自定义字段插件,并且我在我的产品中添加了一个自定义字段,标签名称是 (stock_number) 我的问题是它如何在新订单电子邮件中显示/显示此字段数据。
【问题讨论】:
标签: wordpress woocommerce hook-woocommerce
下面的代码应该可以解决问题。
选项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;
}
【讨论】: