我的代码在提交重力形式时以编程方式创建了woocommerce订单。 我想知道是否有人会知道如何返回订单ID?
我想使用代码将用户重定向到该订单ID的结帐处,以便他们付款。
我是否可以将订单ID发送回表单项?到订单ID字段?
这是我的代码:
$product_id = rgar( $entry, '16' );
$quantity = 1;
$price = rgar( $entry, '11' );
$note = rgar( $entry, '22' );
$product = wc_get_product($product_id);
// Set price
$product->set_price( $price );
$address = array(
'first_name' => rgar( $entry, '27' ),
'last_name' => rgar( $entry, '28' ),
'email' => rgar( $entry, '24' ),
'phone' => rgar( $entry, '25' ),
'address_1' => rgar( $entry, '33.1' ),
'address_2' => rgar( $entry, '33.2' ),
'city' => rgar( $entry, '33.3' ),
'state' => rgar( $entry, '33.4' ),
'postcode' => rgar( $entry, '33.5' ),
'country' => rgar( $entry, '33.6' ),
);
// Create the order object
$order = wc_create_order();
$order->set_customer_id( $user_id );
// add product to order
$order->add_product( $product, $quantity );
foreach ($order->get_items() as $item_key => $item ) {
$item->add_meta_data( 'Label', $note, true );
}
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'pending payment', 'pending', TRUE);
$order->add_order_note( $note );
$new_ship_price = rgar( $entry, '21' );
$item->set_total( $new_ship_price ); // (optional)```