【问题标题】:How to create order Manually in woocommerce?如何在 woocommerce 中手动创建订单?
【发布时间】:2014-07-28 01:57:06
【问题描述】:

我已经用我的 woocommerce 商店开发了一个 phonegap 应用程序。 现在我想手动创建订单。 因此,任何人都可以告诉我何时在 woocommerce 商店中创建新订单时,在数据库的哪些表中插入了多少数据。我的意思是新订单生成时的数据,它们存储在哪个表中以及以哪种方式存储。请帮忙

【问题讨论】:

    标签: php wordpress cordova woocommerce


    【解决方案1】:

    WooCommerce 中的订单以 WordPress 帖子的形式存储为 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','');
    

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

    【讨论】:

    • 你们能告诉我在创建订单时将哪些文件订单数据插入数据库中...
    • 您的意思是在 WordPress/WooCommerce 中的哪个 PHP 文件中存放了用于创建常规店内订单的代码?
    • 是的,告诉我一件事,数据库中存储的订单税在哪里
    • 检查插入wp_woocommerce_order_itemmeta 的最后一行 - _line_tax 元值。准确确定所涉及的行和字段的最简单方法是通过常规 WooCommerce 界面操作订单/产品,同时使用 MySQL 界面查看发生了什么变化。
    【解决方案2】:

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

    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_method 返回类名。下一行创建它的实例。

    $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]);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 2021-10-27
      • 2019-09-10
      • 2013-04-20
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 2018-08-23
      相关资源
      最近更新 更多