WooCommerce挂钩– woocommerce_update_order问题

时间:2019-10-15 11:23:39

标签: wordpress woocommerce hook-woocommerce

我已经注册了以下woocommerce钩子:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
  // ...
}

但是,我有一些问题:

这会触发多次,而不是仅在更新订单时结束。它按旧命令触发两次,一次按一次触发。

我也尝试了以下方法:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func');
    // ...
}

也不会更改它。

此外,我尝试修改remove_action以包括优先级和参数计数,例如:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func', 300, 2);
    // ...
}

现在,它只执行一次,但它给了我旧命令而不是新命令。

我正在使用WooCommerce 3.7.0。

关于我如何在更新后如何获得最新定单的建议,而仅 一次 发出钩子?

谢谢!

2 个答案:

答案 0 :(得分:1)

public class Counter {

    Map<String, Integer> endpoints = new ConcurrentHashMap<>();

    void load(String endpoint) {

        endpoints.put(endpoint, endpoints.get(endpoint) + 1);
    }

    void accessEndpoint(String endpoint, int times, int numberOfThreads) throws InterruptedException {

        ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);

        for (int i = 0; i < times; i++) {
            executor.submit(() -> load(endpoint));
        }

        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.MINUTES);

    }

    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();
        counter.endpoints.put("www.google.com", 2);
        counter.accessEndpoint("www.google.com", 100, 10);
        System.out.println(counter.endpoints.get("www.google.com"));
    }
}

在发布条件内执行操作

答案 1 :(得分:0)

function mysite_pending($order_id) {
    error_log("$order_id set to PENDING", 0);
    }
    function mysite_failed($order_id) {
    error_log("$order_id set to FAILED", 0);
    }
    function mysite_hold($order_id) {
    error_log("$order_id set to ON HOLD", 0);
    }
    function mysite_processing($order_id) {
    error_log("$order_id set to PROCESSING", 0);
    }
    function mysite_completed($order_id) {
    error_log("$order_id set to COMPLETED", 0);
    }
    function mysite_refunded($order_id) {
    error_log("$order_id set to REFUNDED", 0);
    }
    function mysite_cancelled($order_id) {
    error_log("$order_id set to CANCELLED", 0);
    }

    add_action( ‘woocommerce_order_status_pending’, ‘mysite_pending’);
    add_action( ‘woocommerce_order_status_failed’, ‘mysite_failed’);
    add_action( ‘woocommerce_order_status_on-hold’, ‘mysite_hold’);
    // Note that it’s woocommerce_order_status_on-hold, not on_hold.
    add_action( ‘woocommerce_order_status_processing’, ‘mysite_processing’);
    add_action( ‘woocommerce_order_status_completed’, ‘mysite_completed’);
    add_action( ‘woocommerce_order_status_refunded’, ‘mysite_refunded’);
    add_action( ‘woocommerce_order_status_cancelled’, ‘mysite_cancelled’);

有很多状态更改挂钩。您可以使用所需的特定设备。希望对您有所帮助