php - 在电子邮件中显示自定义数据,订单详情 woocommerce
问题描述
更新:
我正在构建一个 WooCommerce 网站,用户在其中从单个产品页面的下拉列表中选择一系列选项,然后显示在购物车页面中,并且感谢我在这里收到的帮助,以及结帐时的帮助。选择的选项也会影响产品的价格。我从两个产品变体开始:“打印”和“原始”(该网站出售古董地图)。
一切正常,直到结帐时所有订单详细信息都正确显示,但是在下订单后,详细信息不会出现在“订单详细信息”下的“已收到订单”屏幕上,也不会出现在客户确认电子邮件中。
为了提供一些背景知识,使用 jQuery 选择不同的变体,并在下面的示例中添加到隐藏字段:
$( ":root" ).find("#mapchest-custom-fields").append("<input type='hidden'
name='test' value='wibble'>");
...然后引用这些隐藏字段以通过以下方式将详细信息添加到购物车:
add_filter('woocommerce_add_cart_item_data','add_custom_field_data', 20,3);
function add_custom_field_data($cart_item_data, $product_id, $variation_id)
{
if(isset($_REQUEST['test']) && ! empty( 'test' )) { // not
$mc_test = sanitize_text_field($_POST['test']);
$cart_item_data['custom_data']['test'] = array(
'label' => 'Test',
'value' => $mc_test
);
}
if(isset($_REQUEST['original_map_vendor_details']) && ! empty(
'original_map_vendor_details' )) {
$mc_original_map_size =
sanitize_text_field($_REQUEST['original_map_vendor_details']);
$cart_item_data['custom_data']['original_map_vendor_details'] =
array(
'label' => 'Vendor',
'value' => $mc_original_map_size
);
}
// process above repeated for other fields
return $cart_item_data;
}
使用以下功能将详细信息显示在购物车和结帐中:
add_filter('woocommerce_get_item_data','wdm_add_item_meta',10,2);
function wdm_add_item_meta($cart_data, $cart_item)
{
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( isset( $cart_item['custom_data'] ) ) {
foreach( $cart_item['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
$custom_items[] = array(
'name' => $custom_data['label'],
'value' => $custom_data['value'],
);
}
}
}
return $custom_items;
}
正如我所说,我想要做的是在“收到的订单”页面和电子邮件中显示详细信息,但我无法让它工作。我知道对于电子邮件,我需要将其连接到其中一个电子邮件挂钩,但我不知道如何访问上述函数中发送到购物车的数据。
我尝试使用 woocommerce_checkout_create_order_line_item 挂钩添加到以下几行:
add_action( 'woocommerce_checkout_create_order_line_item',
'add_custom_order_line_item_meta', 20,4 );
function add_custom_order_line_item_meta($item, $cart_item_key, $values,
$order)
{
if( array_key_exists('test', $values['custom_data']) ){
$item->update_meta_data( 'Test', $values['custom_data']['test'] );
}
}
...但是如果我在电子邮件中 var_dump 可以看到数据,如下所示:
add_action('woocommerce_email_customer_details',
'add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order,
$sent_to_admin, $plain_text, $email ) {
var_dump($order);
}
因此,总而言之,我的数据一直在工作并显示到结账时。之后,我希望它显示在客户确认电子邮件和“已收到订单”页面上,但我无法访问数据。在查看了关于同一主题的其他问题后,我会认为这会自动发生在收到的订单页面上,但事实并非如此。我怀疑代码中缺少一个阶段,但我无法弄清楚它应该是什么。
关于我在这里做错了什么的任何提示?
提前致谢。
附言。我现在已经设法使用以下功能在确认电子邮件中显示字段(经过一段时间):
add_action( 'woocommerce_checkout_create_order_line_item',
'add_custom_order_line_item_meta', 20,4 );
function add_custom_order_line_item_meta($item, $cart_item_key, $values,
$order)
{
if ( isset( $values['custom_data'] ) ) {
$item->update_meta_data( __('The Custom Data', 'woocommerce'),
$values['custom_data'] );
}
}
和
add_action('woocommerce_email_customer_details',
'add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order,
$sent_to_admin, $plain_text, $email ) {
// var_dump($order);
foreach( $order->get_items() as $item_id => $item ){
$custom_data = $item->get_meta( 'The Custom Data' );
foreach( $custom_data as $key => $value ){
foreach( $value as $key1 => $value1 ){
$output = '';
$output .= '<span class="text">' . $value1 . '</span>';
echo $output;
}
echo "<br>";
}
echo "<br><br>";
// var_dump($custom_data );
}
'</strong> <span class="text">' . $order->get_data() . '</span></div>';
}
但这是一个 hacky 解决方案,并没有解决为什么信息没有出现在收到的订单页面或直接出现在电子邮件的订单行项目中的根本问题。
解决方案
好的,我已经解决了这个问题。我把答案放在这里是为了让其他有同样问题的人受益。基本上,我的过程缺少一个阶段。为了实现上述目标,您可以执行以下操作:
定义您希望作为元数据传递的值。在我自己的情况下,我使用了一个隐藏字段,但这同样可以通过文本输入、下拉菜单或其他输入字段进行设置。在我的例子中,我使用 jquery 将它附加到一个 id 为“mapchest-custom-fields”的空 div 中,我在购物车按钮之前将其挂接到进程中。它同样可以设置为静态值。
<?php function define_container_div() { ?> <div id="mapchest-custom-fields"></div> <?php } add_action( 'woocommerce_before_add_to_cart_button', 'define_container_div', 20 ); ?>
...追加值的jQuery代码。值也可以是动态的:
$( ":root" ).find("#mapchest-custom-fields").append("<input type='hidden' name='test' value='wibble'>");
接下来,将值添加到购物车商品数据中:
function add_values_to_cart_item_data( $cart_item_data, $product_id, $variation_id ) { if(isset($_POST['test']) && ! empty( 'test' )) { $test = filter_input( INPUT_POST, 'test' ); $cart_item_data['test'] = $test; } return $cart_item_data; } add_filter( 'woocommerce_add_cart_item_data', 'add_values_to_cart_item_data', 10, 3);
接下来,您在购物车中显示价值:
function display_data_in_cart( $item_data, $cart_item ) { $item_data[] = array( 'key' => __( 'Test', 'mapchest' ), 'value' => wc_clean( $cart_item['test'] ), ); return $item_data; } add_filter( 'woocommerce_get_item_data', 'display_data_in_cart', 10, 2 );
最后,您将数据添加到您的订单商品中:
function add_data_to_order_items( $item, $cart_item_key, $values, $order ) { $item->add_meta_data( __( 'Test', 'mapchest' ), $values['test'] ); } add_action( 'woocommerce_checkout_create_order_line_item', 'add_data_to_order_items', 10, 4 );
上述过程对我有用。它在购物车和结帐中显示自定义数据,并将其保存到“已收到订单”页面存档订单和确认电子邮件(尚未检查其他电子邮件)。
感谢https://iconicwp.com/blog/add-custom-cart-item-data-woocommerce/向我解释这个过程。
推荐阅读
- python - 在函数中传递列名
- mathematical-optimization - CBC 求解器返回的解是否准确?
- postgresql - PostgreSQL => ALTER TABLE without timezone -> with timezone, using select for TZ
- java - 应用程序关闭后,Android 服务不存在
- c# - 在侦听用户输入时在不同的线程上运行方法
- c - Mini-GMP Arduino 打印
- c - 在 C 中为动态数字输入读取字符串时出现问题
- xamarin - 如何将通知的小图标设置为应用程序的图标?
- java - Java 语言练习与数组
- java - 为什么 Kotlin 没有显式类型?