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
1.8k views
in Technique[技术] by (71.8m points)

php - Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"

Need to add custom meta to order items. Googled it and most articles says to use "woocommerce_add_order_item_meta" hook. This hook is deprecated in the newest version 2.3.7. Someone, please tell me which hook to use instead.

http://docs.woothemes.com/wc-apidocs/function-woocommerce_add_order_item_meta.html

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

2017/2018 THE RIGHT WAY (Using new CRUD setters and Getters methods)

Related: Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4

Since woocommerce 3 that has improved many things making drastic changes, the action hook woocommerce_add_order_item_meta still work perfectly even in woocommerce version 3.3+.

This hook is enabled by WC_Checkout class methods and related functions in the checkout process and not in WC_Order Class where cart data is not anymore available.

Now as Woocommmerce 3 has introduced new CRUD setters and getters methods, the similar replacement hook to be used is woocommerce_checkout_create_order_line_item that has similar useful arguments as cart data.

The woocommerce_new_order_item is really NOT convenient as cart data is not accessible.

Let see how to work with woocommerce_checkout_create_order_line_item. It has 4 available arguments:

  • $item is an instance of WC_Order_Item_Product new introduced Class
  • $cart_item_key is the cart item unique hash key
  • $values is the cart item
  • $order an instance of the WC_Order object (This is a very useful additional argument in some specific cases)

In this hook we will replace the old working functions wc_add_order_item_meta() by the new WC_Data update_meta_data() method to be used with $item argument.

Example:

## --- New way --- ##
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $item->get_product_id(), '_meta_key', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        $item->update_meta_data( 'meta_key1', $custom_field_value );
    }
    // … … Or … …

    // Get cart item custom data and update order item meta
    if( isset( $values['custom_data'] ) ) {
        $item->update_meta_data( 'meta_key2', $values['custom_data'] );
    }
}

Finally we can do the same with old way using woocommerce_add_order_item_meta hook as it has nearly the same useful arguments:

## --- Old way --- ##
add_action( 'woocommerce_add_order_item_meta', 'custom_add_order_item_meta', 20, 3 );
function custom_add_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $values['data']->get_id(), '_meta_key', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        wc_add_order_item_meta( $item_id, 'meta_key1', $custom_field_value );
    }
    // … … Or … …

    // Get cart item custom data and update order item meta
    if( isset( $values['custom_data'] ) ) {
        wc_add_order_item_meta( $item_id, 'meta_key2', $values['custom_data'] );
    }
}

Conclusion: woocommerce_checkout_create_order_line_item is the right replacement hook to be used with WooCommerce 3+ and that new CRUD setters and getters methods.


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

...