2

I am using "Programmatically creating new order in Woocommerce" answer code and I want when I clicking button that an order comes to admin panel.

Here is my attempt:

if (isset($_POST['action'])) {
   switch ($_POST['action']) {

        case 'new_order':
            new_order();
            break;
    }
}

function new_order() {

    global $woocommerce;

    $address = array(
       'first_name' => $shippingName,
       'email'      => $user_email_id,
       'phone'      => $billingPhone,
       'address_1'  => $shippingAddress,
       'address_2'  => $shippingAddress2,
       'city'       => $shippingCity,
       'state'      => $shippingStateCode,
       'postcode'   => $shippingZip,
       'country'    => 'US');
    );

    $order = wc_create_order();
    $order->add_product( get_product());
    $order->set_address( $address, 'billing' );  
    $order->calculate_totals();   
    $order->update_status("Completed", 'Imported order', TRUE);
}

And My button:

 <input type="submit" id="button" name="new_order" value="Submit" >

Is that the correct way? Any Help is welcome.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

3

Updated 2 - Your code is a bit outdated as get_product() is replaced by wc_get_product() since a while.

1) First the function that is going to create the order (with a variable that handle the product Id added to the order):

function trigger_new_order( $from_product_id ) {
    $address = array(
        'first_name' => "Joe",
        'last_name'  => "Doe",
        'email'      => "john.doe@gmail.com",
        'phone'      => "0123456789",
        'address_1'  => "1 St. James Street",
        'address_2'  => "",
        'city'       => "San Francisco",
        'state'      => "California",
        'postcode'   => "92105",
        'country'    => "US"
    );

    $order = wc_create_order( $from_product_id );

    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' ); // The Shipping address

    $product    = wc_get_product( $from_product_id );

    $order->add_product( $product );

    $order->calculate_totals();
    $order->update_status( 'completed', 'Imported order', true );

    // Display a message with the order number
    echo '<p>' . sprintf( __("Order #%d has been created"), $order->get_id() ) . '</p>';
}

Code goes in functions.php file of your active child theme (or active theme).


2) The form with the submit button that is going to create the order

In the example below, we use this code on single product pages (on front end) and we get dynamically the product Id:

if ( isset($_POST['new-order']) && $_POST['new-order'] === 'Submit' ) {
    trigger_new_order( get_the_id() );
} else {
    ?>
    <form class="new" method="post">
        <input class="button alt" type="submit" id="button" name="new-order" value="Submit" >
    </form>
    <?php
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • $from_product_id - undefinded variable it tels me, trigger_new_order - method call uses 1 parametres but method signatures uses 0 parametres –  Sep 19 '19 at 16:12
  • @ElVyganto Sorry a little oversight in my code… updated – LoicTheAztec Sep 19 '19 at 16:23
  • No. That not worked. By the way i trying to put all this to popup. https://www.part.lt/perziura/867bc1954ee91b23bcedf5f75f734fe8365.png –  Sep 19 '19 at 17:19
  • @ElVyganto I have tested the code on single product pages and it works (on last WooCommerce version)… The order is generated and visible on backend. – LoicTheAztec Sep 19 '19 at 17:22