在 Woocommerce 3+ 中使用订单项以编程方式创建订单 [英] Create an order programmatically with line items in Woocommerce 3+
本文介绍了在 Woocommerce 3+ 中使用订单项以编程方式创建订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以编程方式创建 Woocommerce 订单,但是使用旧的"Woocommerce 使这个过程变得非常脏.
I needed to create a Woocommerce order programatically, however using the 'old' Woocommerce made this a very dirty procedure.
我不得不手动插入所有类型的数据库记录,使用许多 update_post_meta 调用.
I had to insert all kind of database records manually, using many update_post_meta calls.
寻找更好的解决方案.
推荐答案
使用最新版本的 WooCommerce 可以尝试像这样的东西
With latest version of WooCommerce is possible try this as something like
$address = array(
'first_name' => 'Fresher',
'last_name' => 'StAcK OvErFloW',
'company' => 'stackoverflow',
'email' => 'test@test.com',
'phone' => '777-777-777-777',
'address_1' => '31 Main Street',
'address_2' => '',
'city' => 'Chennai',
'state' => 'TN',
'postcode' => '12345',
'country' => 'IN'
);
$order = wc_create_order();
$order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity)
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->add_coupon('Fresher','10','2'); // accepted param $couponcode, $couponamount,$coupon_tax
$order->calculate_totals();
用你的函数调用上面的代码,然后它就会相应地工作.
Call this above code with your function then it will work accordingly.
请注意,它不适用于 2.1.12 等旧版 WooCommerce,它仅适用于 WooCommerce 2.2.
希望能帮到你
这篇关于在 Woocommerce 3+ 中使用订单项以编程方式创建订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文