Jessica Armstrong
Jessica Armstrong

Reputation: 99

Woocommerce - Create an order manually via code

I'm wondering if anybody would be able to help me with my code?

I would like to create an order programmatically in woocommerce when my gravity form is submitted. I have managed to make it work but would like some custom meta to be placed below the product item line in the order.

I have managed to get the meta into order notes but ideally it would best in a meta box.

Here is my code:

add_action( 'gform_after_submission_56', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
    global $woocommerce;
    // use this to find out $entry output
    var_dump($entry);


    $user_id =rgar( $entry, '97' );
    $note = rgar( $entry, '53' );

    $product_id = rgar( $entry, '71' );
    $quantity = rgar( $entry, '73' );


    $address = array(   
         'first_name' => rgar( $entry, '98' ),
         'last_name'  => rgar( $entry, '99' ),
         'company'    => rgar( $entry, '' ),
         'email'      => rgar( $entry, '83' ),
         'phone'      => rgar( $entry, '84' ),
         'address_1'  => rgar( $entry, '88.1' ),
         'address_2'  => rgar( $entry, '88.2' ),
         'city'       => rgar( $entry, '88.3' ),
         'state'      => rgar( $entry, '88.4' ),
         'postcode'   => rgar( $entry, '88.5' ),
         'country'    => rgar( $entry, '88.6' ),
    );



    $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", 'On Hold', TRUE); 

    $order->add_order_note( $note ); 
}

Upvotes: 2

Views: 2136

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29650

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

Upvotes: 3

Related Questions