将 Woocommerce 购物车项目自定义数据保存为在订单和电子邮件中显示的订单项目元数据

Save Woocommerce cart item custom data as order item meta data displaying it on orders and emails

关于 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;
}

这有效,自定义数据显示在购物车中。但是,自定义数据不会显示在订单和订单电子邮件中。我在 Whosebug 上看到了几个提供此问题解决方案的答案,但我无法让它们适用于我的情况。我参考的解决方案是。

谁能告诉我“我的”功能应该是什么?

谢谢。

首先你可以这样优化和压缩你的函数:

// 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', 10, 4 );
function save_as_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
    $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( $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 文件。它应该有效。