1 回答

TA貢獻(xiàn)1815條經(jīng)驗 獲得超10個贊
更新 2 - 自 WooCommerce 3 以來,您的代碼中存在一些錯誤和不推薦使用的內(nèi)容。嘗試以下操作(已評論):
// Function that create an order
function create_vip_order() {
// Create a WC_Order instance object
$order = wc_create_order();
$order->add_product( wc_get_product( '3283' ), 3 ); // <== get_product() is deprecated and replaced by wc_get_product()
$address = array(
'first_name' => 'a',
'last_name' => 'a',
'company' => 'a',
'address_1' => 'a',
'address_2' => 'a',
'city' => 'a',
'state' => 'FL', // <== UPERCASE
'postcode' => '',
'country' => 'USA', // <== UPERCASE
'email' => 'abc@abc.com', // <== EMAIL REQUIRED
'phone' => '',
);
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();
$order->update_status('processing', 'Imported order', true); // <== LOWERCASE for the WC status
}
// Triggered once
add_action( 'init', 'my_run_only_once' );
function my_run_only_once() {
if ( did_action( 'init' ) >= 2 )
return;
if( ! get_option('run_create_vip_order_once') ) {
create_vip_order(); // Run the function
update_option( 'run_create_vip_order_once', true );
}
}
現(xiàn)在,創(chuàng)建訂單的函數(shù)將按預(yù)期只運行一次。請注意,global $woocommerce不再需要它了。
- 1 回答
- 0 關(guān)注
- 250 瀏覽
添加回答
舉報