Display product custom fields as order items in Woocommerce 3

强颜欢笑 提交于 2019-12-06 01:04:41

There is a better hook for that since Woocommerce 3. The following that should solve your errors:

// Save and display custom field in orders and email notifications (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_fields_update_order_item_meta', 20, 4 );
function custom_fields_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['custom_data']['date'] ) ){
        $date = date( 'd.m.Y', strtotime( $values['custom_data']['date'] ) );
        $item->update_meta_data( __( 'Choosen Date', 'woocommerce' ), $date );
    }
    if ( isset( $values['custom_data']['rental'] ) ){
        $rental = $values['custom_data']['rental'] == '30' ? __("2 days") : __("4 days");
        $item->update_meta_data( __( 'Period Rental', 'woocommerce' ), $rental );
    }
}

Code goes in function.php file of your active child theme (or active theme). It should work.


Related:
Woocommerce: which hook to use instead of deprecated "woocommerce_add_order_item_meta"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!