有时我需要用代码输入我的外部订单,我有一个代码可以正常工作但是如果我把它放在functions.php中,它会多次创建订单。我正在寻找一种代码只创建 1 个订单/只触发一次的方法
下面的代码有效,但多次创建相同顺序的 5-20
function create_vip_order() {
global $woocommerce;
$address = array(
'first_name' => '',
'last_name' => '',
'company' => '',
'email' => '',
'phone' => '',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'postcode' => '',
'country' => ''
);
// Now we create the order
$order = wc_create_order();
// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( get_product( '2494' ), 1 ); // This is an existing SIMPLE product
$order->set_address( $address, 'billing' );
//
$order->calculate_totals();
$order->update_status("Processing", 'Imported order', TRUE);
}
add_action( 'init', 'create_vip_order' );
/**
* Run code only once
*/
function my_run_only_once() {
if ( get_option( 'my_run_only_once_01' ) != 'completed' ) {
function create_vip_order() {
global $woocommerce;
$address = array(
'first_name' => 'a',
'last_name' => 'a',
'company' => 'a',
'email' => 'a',
'phone' => 'a',
'address_1' => 'a',
'address_2' => 'a',
'city' => 'a',
'state' => 'fl',
'postcode' => '',
'country' => 'usa'
);
}
}
add_action( 'admin_init', 'my_run_only_once' );
试过了,但后来什么也没发生
如何强制该代码仅创建 1 个订单?
动漫人物