首页 > 解决方案 > 如何在functions.php中只运行一次函数

问题描述

有时我需要用代码输入我的外部订单,我有一个可以正常工作的代码,但是如果我把它放在 functions.php 中,它会多次创建订单。我正在寻找一种代码只会创建一个订单/只触发一次的方法

下面的代码有效,但很多时候会创建 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 个订单?

标签: phpwordpresswoocommercehook-woocommerceorders

解决方案


更新 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不再需要。


推荐阅读