• Resolved greencode

    (@greencode)


    Hi. Not sure how to replace the following code so I don’t get warnings in the error log. I have this error:

    [07-Dec-2022 09:03:49 UTC] woocommerce_add_order_item_meta is deprecated since version 3.0.0! Use woocommerce_new_order_item instead.
    

    From this code:

    add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
    if($values['product_id']==230)
      {
        woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] );
      }
    }

    Any help would be much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @greencode,

    Thank you for reaching out. The action that you are using (woocommerce_add_order_item_meta) has been deprecated and you should use woocommerce_new_order_item instead.

    In the code that you shared, the action hook woocommerce_add_order_item_meta is also being used as a method. That’s not expected for a hook.

    In summary, your code should look like this:

    add_action ('woocommerce_new_order_item', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
       // your code will be here.
    }

    I hope my answer helps. Please, take a look at the hooks documentation as well.

    Thanks!

    Thread Starter greencode

    (@greencode)

    @fermarichal Thanks for the reply. I have tried the following and neither works:

    add_action ('woocommerce_new_order_item', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
       woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] );
    }
    add_action ('woocommerce_new_order_item', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
       if ( empty( $values['_goodlen'] ) ) {
    		return;
    	}
    	woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] );
    }

    Hey @greencode,

    As I mentioned before, you are trying to use a hook as a method.

    
    add_action ('woocommerce_new_order_item', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
    ---->  woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] );
    }
    

    So instead of this line woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] ); you should add your code to extend the WooCommerce functionalities.

    Please take a look at these two articles:
    https://developer.www.ads-software.com/plugins/hooks/actions/
    https://developer.www.ads-software.com/plugins/hooks/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘woocommerce_add_order_item_meta is deprecated’ is closed to new replies.