我需要以編程方式創建Woocommerce訂單,但是使用「舊」Woocommerce使這是一個非常髒的過程。以編程方式使用line_item創建Woocommerce訂單
我不得不手動插入所有類型的數據庫記錄,使用許多update_post_meta調用。
尋找更好的解決方案。
我需要以編程方式創建Woocommerce訂單,但是使用「舊」Woocommerce使這是一個非常髒的過程。以編程方式使用line_item創建Woocommerce訂單
我不得不手動插入所有類型的數據庫記錄,使用許多update_post_meta調用。
尋找更好的解決方案。
隨着WooCommerce的最新版本,可以試試這個,就像這樣
$address = array(
'first_name' => 'Fresher',
'last_name' => 'StAcK OvErFloW',
'company' => 'stackoverflow',
'email' => '[email protected]',
'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();
調用此上面的代碼與你的函數,那麼會相應工作。
請注意,它不適用於舊版本的WooCommerce 2.1.12,它僅適用於WooCommerce的2.2版本。
希望它可以幫助
隨着新版本的WC 2,它好多了。
但是:
說實話不適合我用, WooCom的API Docs是有限的,也許他們仍在更新它的過程中。他們目前不告訴我如何創建一個新的訂單,哪些參數是必需的等。
任何方式,我想出瞭如何創建一個命令行訂單(您的產品)使用的類和函數REST API和我想分享它!在你的插件目錄
我創造了我自己的PHP類:
class WP_MyPlugin_woocommerce
{
public static function init()
{
// required classes to create an order
require_once WOOCOMMERCE_API_DIR . 'class-wc-api-exception.php';
require_once WOOCOMMERCE_API_DIR . 'class-wc-api-server.php';
require_once WOOCOMMERCE_API_DIR . 'class-wc-api-resource.php';
require_once WOOCOMMERCE_API_DIR . 'interface-wc-api-handler.php';
require_once WOOCOMMERCE_API_DIR . 'class-wc-api-json-handler.php';
require_once WOOCOMMERCE_API_DIR . 'class-wc-api-orders.php';
}
public static function create_order()
{
global $wp;
// create order
$server = new WC_API_Server($wp->query_vars['wc-api-route']);
$order = new WC_API_Orders($server);
$order_id = $order->create_order(array
(
'order' => array
(
'status' => 'processing'
, 'customer_id' => get_current_user_id()
// , 'order_meta' => array
// (
// 'some order meta' => 'a value
// , some more order meta' => 1
// )
, 'shipping_address' => array
(
'first_name' => $firstname
, 'last_name' => $lastname
, 'address_1' => $address
, 'address_2' => $address2
, 'city' => $city
, 'postcode' => $postcode
, 'state' => $state
, 'country' => $country
)
, 'billing_address' => array(..can be same as shipping)
, 'line_items' => array
(
array
(
'product_id' => 258
, 'quantity' => 1
)
)
)
));
var_dump($order_id);
die();
}
}
重要。
訂單分配給客戶,在我的情況下是當前登錄的用戶。確保您的用戶具有能夠閱讀,編輯,創建和刪除訂單的角色。我的角色看起來是這樣的:
$result = add_role(
'customer'
, __('Customer')
, array
(
'read' => true
// , 'read_private_posts' => true
// , 'read_private_products' => true
, 'read_private_shop_orders' => true
, 'edit_private_shop_orders' => true
, 'delete_private_shop_orders' => true
, 'publish_shop_orders' => true
// , 'read_private_shop_coupons' => true
, 'edit_posts' => false
, 'delete_posts' => false
, 'show_admin_bar_front' => false
)
);
如果你想看看店長的rights,檢查
的var_dump(get_option( 'wp_user_roles'));
我的create_order函數很好地在order_items表中使用lineitem創建一個訂單。
希望我幫了你,我花了一段時間才弄清楚。
能否請你檢查我的問題http://stackoverflow.com/questions/36526268/sql-query-to-download-order-report-in-woocommerce。 – Manik 2016-04-11 06:21:43
是的,這將繞過REST API類。好點子。你可能不需要包含任何文件來完成這項工作,對吧?更短的解決方案,不錯! 我會嘗試並讓你知道。 – Mattijs 2014-10-27 23:02:36
使用這個函數'get_class_methods($ order))'來查看當你有訂單對象時你可以使用哪些函數! – Mattijs 2014-10-31 05:28:16
不錯。我打電話來下這個訂單? – Kirby 2015-04-21 18:44:01