我为商店的每种商品添加了一个自定义字段:
add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_rangement_to_variations', 10, 3 );
function bbloomer_add_rangement_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'rangement[' . $loop . ']',
'placeholder' => 'Ex : Armoire 2',
'label' => __('Rangement', 'woocommerce'),
'desc_tip' => 'true',
'class' => 'short',
'wrapper_class' => 'form-row',
'value' => get_post_meta( $variation->ID, 'rangement', true )
)
);
}
并且我已将其存储到变体元数据中
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'bbloomer_add_rangement_variation_data' );
function bbloomer_add_rangement_variation_data( $variations ) {
$variations['rangement'] = '<div class="woocommerce_rangement">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'rangement', true ) . '</span></div>';
return $variations;
}
现在,我想在管理员收到新订单时在发送给管理员的邮件中显示此元数据,但我不想在付款页面或客户的邮件中显示它。
您能帮我解决这个问题吗?
我发现了如何使用以下代码在购物车和电子邮件中显示我的自定义字段:
// display custom field in cart and email
add_filter( 'woocommerce_cart_item_name', 'showing_rgt_in_cart_items', 99, 3 );
function showing_rgt_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
// The WC_Product object
$product = $cart_item['data'];
// Get the rangement
$rgt = get_post_meta( $product->get_id(), 'rangement', true );
// When rgt doesn't exist
if(empty($rgt)) return $item_name;
// Add the rangement field
$item_name .= '<br><small class="product-sku">' . __( "Rangement: ", "woocommerce") . $rgt . '</small>';
return $item_name;
}
但是我怎么只在管理员电子邮件中显示呢?