WooCommerce Add item to Order

别等时光非礼了梦想. 提交于 2021-01-20 16:57:19

问题


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.


回答1:


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);
                    }
             }    



回答2:


Since WC 2.2, adding an item to a new order is simpler:

$product = wc_get_product($product_id);

wc_get_order($order_id)->add_product($product, $quantity);



来源:https://stackoverflow.com/questions/16241467/woocommerce-add-item-to-order

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