0

I am trying to add manual orders to woocommerce. Webshop is where I have stock values. I have store also. When customer buy in store, I have to add an order. When I want to add it via Orders->Add order it do not work properly, as I need to add tax value manual (Automattic, why?). I'd like to have hidden page, to add orders.

I've seen Programmatically creating new order in Woocommerce

Here is what I tried: I got order.php file in main folder:

<?php
/*
 * Create order dynamically
 */
require(dirname(__FILE__) . '/wp-load.php');
echo 'ok?';
function create_vip_order() {

  global $woocommerce;

  $address = array(
      'first_name' => 'John',
      'last_name'  => 'Doe',
      'email'      => 'test@gmail.com',
      'phone'      => '123456789',
      'address_1'  => '123 Main st.',
      'city'       => 'San Diego',
      'state'      => 'Ca',
      'postcode'   => '92121',
  );

  // Now we create the order
  $order = wc_create_order();

  // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
  $order->add_product( get_product( '376' ), 1 ); // This is an existing SIMPLE product
  $order->set_address( $address, 'billing' );
  //
  $order->calculate_totals();
  $order->update_status("Completed", 'Imported order', TRUE);

}

add_action( 'init', 'create_vip_order' );
Machavity
  • 30,841
  • 27
  • 92
  • 100
matliks
  • 71
  • 7

1 Answers1

0

Finally code that I need is:

<?php
/*
 * Create order manual
 */
require(dirname(__FILE__) . '/wp-load.php');

function create_new_order() {

  global $woocommerce;

  $address = array(
      'first_name' => 'Zakup',
      'last_name'  => 'Sklepowy',
      'email'      => 'test@test.pl',
      'phone'      => '123',
      'address_1'  => 'ul. Przykladowa 1',
      'address_2'  => 'm. 2',
      'city'       => 'Wroclaw',
      'postcode'   => '50-123',
  );

  $order = wc_create_order();

  $product = new WC_Product(wc_get_product_id_by_sku('*sku_here*'));
  $order->add_product( $product, 1 );
    $order->set_address( $address, 'billing' );

    // Set payment gateway
    $payment_gateways = WC()->payment_gateways->payment_gateways();
    $order->set_payment_method( $payment_gateways['cod'] );

    // Calculate totals
    $order->calculate_totals();
    $order->update_status('completed', 'In Store ', TRUE);
}

create_new_order();
?>
matliks
  • 71
  • 7