如何在functions.php中只运行一次函数

有时我需要用代码输入我的外部订单,我有一个代码可以正常工作但是如果我把它放在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 个订单?


ibeautiful
浏览 261回答 1
1回答

动漫人物

更新 2 - 自 WooCommerce 3 以来,您的代码中存在一些错误和不推荐使用的内容。尝试以下操作(已评论):// Function that create an orderfunction create_vip_order() {&nbsp; &nbsp; // Create a WC_Order instance object&nbsp; &nbsp; $order = wc_create_order();&nbsp; &nbsp; $order->add_product( wc_get_product( '3283' ), 3 ); // <== get_product() is deprecated and replaced by wc_get_product()&nbsp; &nbsp; $address = array(&nbsp; &nbsp; &nbsp; &nbsp; 'first_name' => 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'last_name'&nbsp; => 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'company'&nbsp; &nbsp; => 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'address_1'&nbsp; => 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'address_2'&nbsp; => 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'city'&nbsp; &nbsp; &nbsp; &nbsp;=> 'a',&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; &nbsp; => 'FL', // <== UPERCASE&nbsp; &nbsp; &nbsp; &nbsp; 'postcode'&nbsp; &nbsp;=> '',&nbsp; &nbsp; &nbsp; &nbsp; 'country'&nbsp; &nbsp; => 'USA', // <== UPERCASE&nbsp; &nbsp; &nbsp; &nbsp; 'email'&nbsp; &nbsp; &nbsp; => 'abc@abc.com', // <== EMAIL REQUIRED&nbsp; &nbsp; &nbsp; &nbsp; 'phone'&nbsp; &nbsp; &nbsp; => '',&nbsp; &nbsp; );&nbsp; &nbsp; $order->set_address( $address, 'billing' );&nbsp; &nbsp; $order->set_address( $address, 'shipping' );&nbsp; &nbsp; $order->calculate_totals();&nbsp; &nbsp; $order->update_status('processing', 'Imported order', true); // <== LOWERCASE for the WC status}// Triggered onceadd_action( 'init', 'my_run_only_once' );function my_run_only_once() {&nbsp; &nbsp; if ( did_action( 'init' ) >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if( ! get_option('run_create_vip_order_once') ) {&nbsp; &nbsp; &nbsp; &nbsp; create_vip_order(); // Run the function&nbsp; &nbsp; &nbsp; &nbsp; update_option( 'run_create_vip_order_once', true );&nbsp; &nbsp; }}现在,创建订单的函数将按预期只运行一次。请注意,global $woocommerce不再需要它了。
打开App,查看更多内容
随时随地看视频慕课网APP