简体   繁体   English

如何在 wordpress 插件中创建 foreach 循环?

[英]How do I make a foreach loop in a wordpress plugin?

I am trying to make a plugin for woocommerce that, at each new order, take all the data that I need, split it by item in a foreach loop and then send each item data as a separate post through cURL.我正在尝试为 woocommerce 制作一个插件,在每个新订单中,获取我需要的所有数据,在 foreach 循环中按项目拆分,然后通过 cURL 将每个项目数据作为单独的帖子发送。 But my code don't seem to ignore the foreach loop.但是我的代码似乎没有忽略 foreach 循环。 Here is the code for my loop:这是我的循环的代码:

add_action('woocommerce_new_order', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext( $order_id ){

    // get order object and order details
    $order = wc_get_order($order_id);

    print_r(" Start ");
    if($order->get_items()>0)
    {
        print_r(" Item exist ");
        foreach($order->get_items() as $item_id => $item_data){
            print_r(" Foreach loop executing for item ID: $item_id ");
        }
    }
    else
    {
        print_r(" No Item ");
    }
    print_r(" End ");
 }

and my result is: Start Item exist End edit: When i used print_r on $order->get_items(), it printed as Array ( )我的结果是:开始项目存在结束编辑:当我在 $order->get_items() 上使用 print_r 时,它打印为数组 ()

The quickest way to find out whether you're able to execute a foreach loop is to strip out everything that isn't related to the foreach loop, via:确定您是否能够执行 foreach 循环的最快方法是通过以下方式去除与 foreach 循环无关的所有内容:

add_action('woocommerce_new_order', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext( $order_id ){

    // get order object and order details
    $order = wc_get_order($order_id);

    $endpoint = "https://enwlq927zckka.x.pipedream.net"; 
    foreach($order->get_items() as $item_id => $item_data){
        echo "Foreach loop executing for item ID: $item_id.";
    }
 }

If you're seeing output, then you know you're executing the foreach loop.如果您看到输出,那么您就知道您正在执行 foreach 循环。 To debug these kinds of things, you need to get comfortable with stripping out the unnecessary and building off of what you can discern one piece at a time.要调试这些类型的东西,您需要习惯于去除不必要的东西并建立一次您可以辨别的东西。

One thing you may be encountering (but is outside the scope of the way you worded your question) is that cURL calls can take a very long time to respond.您可能遇到的一件事(但超出了您提出问题的方式的范围)是 cURL 调用可能需要很长时间才能响应。 Depending on how your server is configured, it may abort executing the PHP code if it doesn't finish executing after X number of seconds.根据您的服务器的配置方式,如果 PHP 代码在 X 秒后没有完成执行,它可能会中止执行。 Since I'm not familiar with the command $order->get_items() or with how many records it could be possibly returning, my guess is that you could be easily spending more than a minute executing hundreds or more cURL calls.由于我不熟悉$order->get_items()命令或它可能返回的记录数,我的猜测是您可能很容易花费一分钟以上的时间来执行数百个或更多的 cURL 调用。

For things like this, verify each step along the way.对于此类情况,请验证整个过程中的每一步。

Also, it would help if you describe exactly what is happening instead of what does not seem to be happening.此外,如果您准确描述正在发生的事情而不是似乎没有发生的事情,这将有所帮助。 That way answers can be more impactful.这样,答案会更有影响力。 When you're not sure if something is getting executed, you can always add in an echo statement to verify if the code is getting reached.当您不确定某事是否正在执行时,您始终可以添加 echo 语句来验证是否已到达代码。 Hope this helps!希望这可以帮助!

我终于找到了我的问题:挂钩 woocommerce_new_order 在项目添加到订单之前被激活,而不是我使用 woocommerce_thankyou 并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM