Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

php - Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4

I have custom code use woocommerce_add_order_item_meta hook. But woocommerce 3.4.0 show error log: "woocommerce_add_order_item_meta is deprecated since version 3.0.0! Use woocommerce_new_order_item instead." How to fix it? Thank you very much. My code:

    // add data design to order
function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) {
    if( WC()->session->__isset( $cart_item_key.'_designer' ) ) {
        wc_add_order_item_meta( $item_id, "custom_designer", WC()->session->get( $cart_item_key.'_designer') );
    }
}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated

Since Woocommerce version 3, woocommerce_checkout_create_order_line_item action hook now replace outdated woocommerce_add_order_item_meta hook in a much better way using the new introduced CRUD getters and setters methods:

// Save custom data to order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'tshirt_order_meta_handler', 20, 4 );
function tshirt_order_meta_handler( $item, $cart_item_key, $values, $order ) {
    $custom_designer = WC()->session->get( $cart_item_key.'_designer' );
    if( ! empty($custom_designer) ) {
        $item->update_meta_data( 'custom_designer', $custom_designer );
    }
}

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

See this related thread:
Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...