【问题标题】:Script to add manual order to Woocommerce将手动订单添加到 Woocommerce 的脚本
【发布时间】:2019-06-03 17:17:21
【问题描述】:

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

我见过Programmatically creating new order in Woocommerce

这是我尝试过的: 我在主文件夹中有 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' );

【问题讨论】:

  • 您遇到的错误是什么,请在此处明确您的问题。,
  • 哦,脚本没有出错,但没有下新订单。
  • create_vip_order函数调用了吗?
  • 不明白你的问题。
  • 哦,我明白了。我从来没有调用过那个函数……真丢脸。

标签: php wordpress woocommerce orders


【解决方案1】:

最后我需要的代码是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2013-04-20
    • 1970-01-01
    • 2020-08-15
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多