向 Woocommerce 添加手动订单的脚本

Script to add manual order to Woocommerce

我正在尝试向 woocommerce 添加手动订单。网上商店是我拥有股票价值的地方。我也有商店。当客户在商店购买时,我必须添加订单。当我想通过 Orders->Add order 添加它时,它无法正常工作,因为我需要手动添加税值(Automattic,为什么?)。 我想有隐藏页面,添加订单。

我看过

这是我尝试过的: 我在主文件夹中得到 order.php 文件:

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

最后我需要的代码是:

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