我有一个很大的问题要解决下面的 case .你能帮帮我吗?
What I need to do:个
-
我需要创建一个额外的产品自定义域,比如技术规范(TECH_SPEC),管理员在创建/编辑产品时会填写这个域,所以我使用ACF插件.
-
这些技术信息只为管理员保留,所以我不想向客户展示它,但我需要在客户创建新订单时自动将它们添加到订单面板(后端),以及仅在新订单通知(邮箱)中将它们发送给管理员.
What I've tried to do:个
-
我使用ACF插件创建了一个tech_spec字段,并将其显示在单一产品编辑页面上.
-
我使用钩子:woocommerce_before_add_to_cart_button在单个产品页面上实现了这个字段的数据,并使用了CSS来隐藏它们.
PHP (functions.php):个
add_action('woocommerce_before_add_to_cart_button','techspec_field');
function techspec_field(){
global $product;
ob_start(); ?>
<div class="techspec-info">
<textarea name="tech_spec"><?php the_field('tech_spec'); ?></textarea>
</div>
<?php
$content = ob_get_contents();
ob_end_flush();
return $content;
}
CSS (style.css):个
.techspec-info textarea {
display: none !important;
visibility: hidden;}
- 我创建了一个向购物车添加字段数据的函数.
add_filter('woocommerce_add_cart_item_data','techspec_add_item_data', 10,3);
function techspec_add_item_data($cart_item_data, $product_id, $variation_id)
{
if(isset($_REQUEST['tech_spec']))
{
$cart_item_data['tech_spec'] = sanitize_text_field($_REQUEST['tech_spec']);
}
return $cart_item_data;
}
- 接下来,我实现了另一个在购物车和 checkout 页面中显示数据的函数.
add_filter('woocommerce_get_item_data','techspec_add_item_meta',10,2);
function techspec_add_item_meta($item_data, $cart_item)
{
if(array_key_exists('tech_spec', $cart_item))
{
$custom_details = $cart_item['tech_spec'];
$item_data[] = array(
'key' => 'Technical Specification',
'value' => $custom_details
);
}
return $item_data;
}
- 在那之后,我创建了一个向新订单添加数据的函数.
add_action( 'woocommerce_checkout_create_order_line_item', 'techspec_add_custom_order_line_item_meta', 10,4 );
function techspec_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order)
{
if(array_key_exists('tech_spec', $values))
{
$item->add_meta_data('Technical Specification', $values['tech_spec']);
}
}
到目前为止,一切都很正常,但tech_Spec field的数据正在发送给所有收件人,包括客户.如何限制仅向管理员发送这些数据?我是说新的订单通知.如果你愿意帮助我,我将不胜感激.
我试过使用不同的解决方案,比如if($sent_to_admin),以及使用woocommerce_order_item_get_formatted_meta_data隐藏这些数据,但都不起作用,或者我做错了什么.首先要感谢您的帮助.