更新:
我正在构建一个 WooCommerce 网站,用户从单个产品页面的下拉列表中选择一系列选项,然后显示在购物车页面中,并且感谢我在这里收到的帮助,也显示在结帐中。选择的选项也会影响产品的价格。我从两种产品变体开始:“打印”和“原始”(该网站正在销售古董 map )。
一切正常,直到结账时所有订单详细信息都正确显示,但是下订单后,详细信息不会出现在“订单详细信息”下的“已收到订单”屏幕上,也不会出现在客户中确认电子邮件。
为了提供一些背景知识,使用 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;
}
正如我所说,我想做的是在“已收到订单”页面和电子邮件中显示详细信息,但我无法使其工作。我知道对于电子邮件,我需要将其 Hook 到其中一个电子邮件 Hook ,但我不知道如何访问上述函数中发送到购物车的数据。
我尝试使用 woocommerce_checkout_create_order_line_item Hook 添加以下内容:
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);
}
总而言之,我的数据一直在工作并显示到结账时。之后,我希望它显示在客户确认电子邮件和“已收到订单”页面上,但我在访问数据时遇到问题。在查看了同一主题的其他问题后,我本以为这会在收到订单的页面上自动发生,但事实并非如此。我怀疑代码中缺少一个阶段,但我无法弄清楚它应该是什么。
关于我在这里做错了什么有什么提示吗?
提前致谢。
ps。我现在已经成功地使用以下函数显示确认电子邮件中的字段(以某种方式):
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>';
}
但这是一个黑客解决方案,并没有解决为什么信息没有出现在订单接收页面或直接出现在电子邮件中的订单行项目中的根本问题。
最佳答案
好的,我已经解决了。我将答案放在这里是为了其他遇到同样问题的人的利益。基本上,我的过程缺少一个阶段。为了实现上述目标,您需要执行以下操作:
定义您希望作为元数据传递的值。就我自己而言,我使用了隐藏字段,但这同样可以通过文本输入、下拉列表或其他输入字段进行设置。就我而言,我使用 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);
接下来,您将显示购物车中的值(value):
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/向我解释这个过程。
关于php - 在电子邮件、订单详细信息中显示自定义数据 woocommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53677459/