如何 运行 一个函数在 functions.php 中只执行一次
How to run a function only once in functions.php
有时我需要用代码输入我的外部订单,我有一个代码可以正常工作但是如果我把它放在 functions.php 中,它会多次创建订单。我正在寻找一种代码只会创建 1 order/be 只触发一次的方法
下面的代码有效,但很多时候会创建 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'
);
// 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( '3283' ), 3 );
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
//
$order->calculate_totals();
$order->update_status("Processing", 'Imported order', TRUE);
}
add_action( 'init', 'create_vip_order' );
update_option( 'my_run_only_once_01', 'completed' );
}
}
add_action( 'admin_init', 'my_run_only_once' );
试过了,但没有任何反应
我如何强制该代码只创建 1 个订单?
更新 2 - 自 WooCommerce 3 以来,您的代码中存在一些错误和弃用的内容。请尝试以下操作(已评论):
// 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 );
}
}
现在,创建订单的函数将按预期 运行 只执行一次。请注意,不再需要 global $woocommerce
。
有时我需要用代码输入我的外部订单,我有一个代码可以正常工作但是如果我把它放在 functions.php 中,它会多次创建订单。我正在寻找一种代码只会创建 1 order/be 只触发一次的方法
下面的代码有效,但很多时候会创建 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'
);
// 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( '3283' ), 3 );
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
//
$order->calculate_totals();
$order->update_status("Processing", 'Imported order', TRUE);
}
add_action( 'init', 'create_vip_order' );
update_option( 'my_run_only_once_01', 'completed' );
}
}
add_action( 'admin_init', 'my_run_only_once' );
试过了,但没有任何反应
我如何强制该代码只创建 1 个订单?
更新 2 - 自 WooCommerce 3 以来,您的代码中存在一些错误和弃用的内容。请尝试以下操作(已评论):
// 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 );
}
}
现在,创建订单的函数将按预期 运行 只执行一次。请注意,不再需要 global $woocommerce
。