第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在functions.php中只運行一次函數(shù)

如何在functions.php中只運行一次函數(shù)

PHP
ibeautiful 2022-01-02 19:35:49
有時我需要用代碼輸入我的外部訂單,我有一個代碼可以正常工作但是如果我把它放在functions.php中,它會多次創(chuàng)建訂單。我正在尋找一種代碼只創(chuàng)建 1 個訂單/只觸發(fā)一次的方法下面的代碼有效,但多次創(chuàng)建相同順序的 5-20function 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' );試過了,但后來什么也沒發(fā)生如何強制該代碼僅創(chuàng)建 1 個訂單?
查看完整描述

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不再需要它了。


查看完整回答
反對 回復(fù) 2022-01-02
  • 1 回答
  • 0 關(guān)注
  • 250 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號