在Woocommerce中以编程方式创建包含订单项的订单 [英] Create an order programmatically with line items in Woocommerce
本文介绍了在Woocommerce中以编程方式创建包含订单项的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以编程方式创建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,仅适用于2.2的WooCommerce.
希望对您有帮助
这篇关于在Woocommerce中以编程方式创建包含订单项的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文