wordpress - Woocommerce programmatic add to order with product add-on -
i'm using woocommerce product add-ons. i'm generating order programmatically, need include add-on data part of line item. how can done?
my basic order setup so:
$orderdata = array( 'status' => 'on-tab', 'customer_id' => 999, 'customer_note' => '', 'created_via' => 'api' ); $order = wc_create_order($orderdata); foreach ($lineitems $lineitem) { //need add-on data in here somehow $order->add_product(wc_get_product(999), 1); }
when use add_product, item_id in return. see code https://docs.woocommerce.com/wc-apidocs/source-class-wc_abstract_order.html#838-889
this item_id can use item using get_item function. refer https://docs.woocommerce.com/wc-apidocs/source-class-wc_abstract_order.html#760-769
than after getting item can add item meta using add_meta_data function. refer code https://docs.woocommerce.com/wc-apidocs/source-class-wc_data.html#315-332
combining below sample code :-
$orderdata = array( 'status' => 'on-tab', 'customer_id' => 999, 'customer_note' => '', 'created_via' => 'api' ); $order = wc_create_order($orderdata); foreach ($lineitems $lineitem) { //need add-on data in here somehow $item_id = $order->add_product(wc_get_product(999), 1); $item = $order->get_item($item_id); $item->add_meta_data( 'label', 'value', true ); }
hope helps... let me know if need else...
Comments
Post a Comment