Woocommerce - Create an order manually via code Woocommerce - Create an order manually via code wordpress wordpress

Woocommerce - Create an order manually via code


NOTE: the variable $prices is not set in your original code example

A example

// set some variables$user_id = 1;$product_id = 30;$quantity = 1;$price = 10;$note = 'my custom note';$product = wc_get_product($product_id);// Create the order object$order = wc_create_order();$order->add_product( $product, $quantity, $price);foreach ($order->get_items() as $item_key => $item ) {    $item->add_meta_data( 'Label', 'Value', true );}$order->set_customer_id( $user_id );$order->set_address( $address, 'billing' );$order->calculate_totals();$order->update_status('on-hold', 'pending', TRUE); $order->add_order_note( $note );

So in your code, this part

$order = wc_create_order();$order->set_customer_id( $user_id );$order->add_product( wc_get_product($product_id), $quantity, $prices); $order->set_address( $address, 'billing' );$order->calculate_totals();$order->update_status("on hold", 'pending', TRUE); $order->add_order_note( $note );

Becomes this

$order = wc_create_order();$order->set_customer_id( $user_id );$order->add_product( wc_get_product($product_id), $quantity, $prices);foreach ($order->get_items() as $item_key => $item ) {    $item->add_meta_data( 'Label', 'Value', true );}$order->set_address( $address, 'billing' );$order->calculate_totals();$order->update_status( 'on-hold', 'pending', TRUE); $order->add_order_note( $note );