问题描述
关于 Woocommerce。我有要添加到购物车的自定义数据。在functions.PHP文件中,我有以下功能。
// display cart item custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data','display_cart_item_custom_on_cart_and_checkout',10,2 );
function display_cart_item_custom_on_cart_and_checkout( $cart_item_data,$cart_item ){
if( isset($cart_item['custom_data']['label0']) && isset($cart_item['custom_data']['value0']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label0'],'value' => $cart_item['custom_data']['value0'],);
}
if( isset($cart_item['custom_data']['label']) && isset($cart_item['custom_data']['value']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label'],'value' => $cart_item['custom_data']['value'],);
}
if( isset($cart_item['custom_data']['label2']) && isset($cart_item['custom_data']['value2']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label2'],'value' => $cart_item['custom_data']['value2'],);
}
if( isset($cart_item['custom_data']['label3']) && isset($cart_item['custom_data']['value3']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label3'],'value' => $cart_item['custom_data']['value3'],);
}
if( isset($cart_item['custom_data']['label4']) && isset($cart_item['custom_data']['value4']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label4'],'value' => $cart_item['custom_data']['value4'],);
}
if( isset($cart_item['custom_data']['label5']) && isset($cart_item['custom_data']['value5']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label5'],'value' => $cart_item['custom_data']['value5'],);
}
if( isset($cart_item['custom_data']['label6']) && isset($cart_item['custom_data']['value6']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label6'],'value' => $cart_item['custom_data']['value6'],);
}
if( isset($cart_item['custom_data']['label7']) && isset($cart_item['custom_data']['value7']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label7'],'value' => $cart_item['custom_data']['value7'],);
}
if( isset($cart_item['custom_data']['label8']) && isset($cart_item['custom_data']['value8']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label8'],'value' => $cart_item['custom_data']['value8'],);
}
if( isset($cart_item['custom_data']['label9']) && isset($cart_item['custom_data']['value9']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label9'],'value' => $cart_item['custom_data']['value9'],);
}
if( isset($cart_item['custom_data']['label10']) && isset($cart_item['custom_data']['value10']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label10'],'value' => $cart_item['custom_data']['value10'],);
}
return $cart_item_data;
}
这有效,自定义数据显示在购物车中。但是,自定义数据不会显示在订单和订单电子邮件中。我已经看到,在 Stackoverflow 上有几个答案为这个问题提供了解决方案,但我无法让它们适合我的情况。我参考的解决方案是。
Save and display order item custom meta data in Woocommerce
Display and save added custom cart item data on Woocommerce Cart,Checkout and Orders
谁能告诉我“我的”功能应该是什么?
谢谢。
解决方法
首先,您可以通过这种方式优化和压缩您的函数:
// Display cart item custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data','display_cart_item_custom_on_cart_and_checkout',10,2 );
function display_cart_item_custom_on_cart_and_checkout( $cart_item_data,$cart_item ){
$keys = array('0','','2','3','4','5','6','7','8','9','10'); // Fields numbers part keys array
// Loop through Fields numbers part keys array
foreach( $keys as $key ) {
if( isset($cart_item['custom_data']['label'.$key]) && isset($cart_item['custom_data']['value'.$key]) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label'.$key],'value' => $cart_item['custom_data']['value'.$key],);
}
}
return $cart_item_data;
}
然后要将所有自定义购物车商品数据保存为自定义订单商品元数据并在订单和电子邮件中的任何地方显示,请使用以下内容:
// Save cart item custom data as order item meta data and display it everywhere in Orders and email notifications
add_action('woocommerce_checkout_create_order_line_item','save_as_custom_order_item_meta_data',4 );
function save_as_custom_order_item_meta_data( $item,$cart_item_key,$values,$order ) {
$keys = array('0','10'); // Fields numbers part keys array()
// Loop through Fields numbers part keys array
foreach( $keys as $key ) {
if( isset( $values['custom_data']['label'.$key] ) && isset( $values['custom_data']['value'.$key] ) ) {
$item->update_meta_data( $values['custom_data']['label'.$key],$values['custom_data']['value'.$key] );
}
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。