Search code examples
phpwordpresswoocommerceorders

WooCommerce Add item to Order


I'm trying to add an item to an existent order but it's not working properly... Everytime I execute the code below it ads and empty item, any help?

woocommerce_add_order_item($Novo_PostId, $item);

$item is the var witch receives the item itself from another order I have, I've put a print_r($item) and it appears to be ok.


Solution

  • After you create your item with woocommerce_add_order_item you have to set the meta data with woocommerce_add_order_item_meta(), see: http://docs.woothemes.com/wc-apidocs/function-woocommerce_add_order_item_meta.html

    Something like:

                $item_id = woocommerce_add_order_item( $order_id, array(
                        'order_item_name'       => '',
                        'order_item_type'       => 'line_item'
                ) );
    
                if ( $item_id ) {
    
                        $foreach($metavalues as $key=>$value)
                        {
                        woocommerce_add_order_item_meta( $item_id,$key,$value);
                        }
                 }