如何在woocommerce中手动创建订单


How to create order Manually in woocommerce?

我为woocommerce商店开发了一个phonegap应用程序。现在我想手动创建订单。所以任何人都可以告诉我什么时候在woocommerce中创建了新订单,存储了多少数据插入到数据库中的哪些表中。我指的是新订单生成时的数据,它们存储在哪个表中,以哪种方式存储。请帮助

WooCommerce中的订单存储为WordPress post, post_type = 'shop_order'在表wp_posts中。

订单中的每个订单项存储在wp_woocommerce_order_items中,每个订单项行的附加信息存储在表wp_woocommerce_order_itemmeta中。

添加新订单:

  1. wp_posts表中插入一个新订单
  2. 将请求的订单行添加到wp_woocommerce_order_items
  3. 对于每条订单线,将订单线详细信息添加到wp_woocommerce_order_itemmeta中。

MySQL转储的示例数据:

INSERT INTO wp_posts VALUES (38,1,'2014-06-09 10:02:00','2014-06-09 10:02:00','','Order – June 9, 2014 @ 10:02 AM','','publish','closed','closed','','order','','','2014-06-09 10:02:43','2014-06-09 10:02:43','',0,'http://example.com/?post_type=shop_order&p=38',0,'shop_order','',0);
INSERT INTO wp_woocommerce_order_items VALUES (33,'My product','line_item',38);
INSERT INTO wp_woocommerce_order_itemmeta VALUES (167,33,'_qty','1');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (168,33,'_tax_class','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (169,33,'_product_id','20');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (170,33,'_variation_id','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (171,33,'_line_subtotal','13');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (172,33,'_line_subtotal_tax','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (173,33,'_line_total','13');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (174,33,'_line_tax','');

我建议您尝试在数据库中手动创建一个订单,并使用您希望生成的订单具有的设置,这样您就可以看到需要手动设置的关系和各种设置。

我在这段代码上花了很多时间-所以我想分享它。它创建一个包含一个产品的新订单,并根据客户的邮政编码,以统一的发货率向订单添加发货。我用它来为我的客户创建订阅订单。

function create_order($user_id, $product_id)
{
    $order = wc_create_order();
    $order->set_created_via("subscription");
    $product = wc_get_product($product_id);
    $order->add_product($product, $quantity);
    $order_id = $order->get_id();
    $postcode = get_user_meta($user_id, 'shipping_postcode', true);
    $package = array('destination' => array('country' => 'IL', 'postcode' => $postcode));
    $zone = WC_Shipping_Zones::get_zone_matching_package($package);
    $method = WC_Shipping_Zones::get_shipping_method( $zone->get_id() );

这是最难发现的。get_shipping_方法返回类名。下一行创建它的实例。

$shipping = new $method;

现在得到货物的价格:

$rate = new WC_Shipping_Rate();
$rate->id = 'flat_rate';
$rate->label = 'shipment';
$rate->cost = $shipping->cost;
$rate->calc_tax = 'per_order'; // Not sure about this one
// Adding it to the order
$order->add_shipping($rate);
$order->calculate_totals();
// assign the order to the current user
update_post_meta($order->get_id(), '_customer_user', $user_id);
// payment_complete
$order->payment_complete();`

复制账单和发货信息

     foreach (array('billing_first_name',
 'billing_last_name',
 'billing_phone',
 'billing_address_1',
 'billing_address_2',
 'shipping_first_name',
 'shipping_last_name',
 'shipping_address_1',
 'shipping_address_2',
 'shipping_city',
 'shipping_postcode') as $key) {
 $values = get_user_meta($user_id, $key);
 update_post_meta($order_id, "_" . $key, $values[0]);
}