从WordPress cron调用WooCommerce对象时不可用 [英] WooCommerce Objects not available when calling them from a WordPress cron

查看:40
本文介绍了从WordPress cron调用WooCommerce对象时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从wp_cron作业中调用WooCommerce对象时出现奇怪"问题.

I have a "strange" problem with calling WooCommerce objects from a wp_cron job.

我创建了下一个函数,该函数每隔10分钟被调用一次,以从仓库中获取当前库存头寸并更新WooCommerce中的可用库存.

I created the next function which gets called at an interval of 10 minutes to fetch the current stock position from our warehouse and update the available stock in WooCommerce.

// Update the stock of our products every 10 minutes
function cron_yp_update_product_stock_b1f6051b() {
    $oConvexStock = YP_get_stock_per_SKU();

    $oQuery = new WC_Product_Query( array( "return" => "objects", "virtual" => false ) );
    $oResult = $oQuery->get_products();
    if ( is_array( $oResult ) ) {
        foreach( $oResult as $oProduct ) {
            if ( isset( $oConvexStock[$oProduct->get_sku()] ) ) {
                wc_update_product_stock($oProduct, $oConvexStock[$oProduct->get_sku()], "set" );
            }
        }
    }
}

它的作用是检索一个数组,其中SKU代码是键,而值是仓库中可用的当前库存.然后,它向WooCommerce产品查询所有非虚拟产品,并将其SKU与仓库阵列中的SKU进行比较.如果存在匹配项,它将更新当前的库存位置.

What it does is retrieve an array in which the SKU codes are the keys and the value is the current stock available in the warehouse. It then queries WooCommerce Products for all non virtual products and compares their SKUs with the once in the warehouse array. If there is a match it will update the current stock position.

没有什么真正的幻想或先进的.当我从管理面板(使用cron管理器)触发cronjob时,效果很好.但是,当cronjob触发自主事件时, WC_Product_Query 对象不会填充或其他内容.我可以 print_r $ oQuery 对象,它显示一个空对象,但是当我调用 get_products()方法时,它崩溃了.

Nothing really fancy or advanced. It works fine when I trigger the cronjob from the admin panel (using a cron manager). But when the cronjob triggers autonomous the WC_Product_Query object doesn't populate or something. I can print_r the $oQuery object and it shown an empty object, but when I call the get_products() method it crashes.

因此,当cron自主触发时,WooCommerce类似乎没有启动或发生什么,但是在通过管理面板调用它时确实/没有.

So it looks like the WooCommerce class doesn't get initiated or something when the cron is triggered autonomous, but it does/is when calling it via the admin panel.

我尝试在WordPress.org上进行谷歌搜索或询问,但无济于事(或者没有回答不良的搜索技能).

I tried googling or asking at WordPress.org but to no avail (either no answer of bad search skills).

在我开始分解WordPress和WooCommerce并花费大量时间之前,没有人知道为什么会发生这种情况以及如何解决此问题吗?

Before I start tearing apart WordPress and WooCommerce and burning massive amounts of hours does anybody know why this happens and how to fix this?

预先考虑,米歇尔

更新1.我整天都在努力找出问题所在.我终于找到了.但是我仍然不知道为什么在管理面板中而不是通过cron可以工作.

UPDATE 1. I have been working all day to figure out what is going wrong. I eventually found it. But I still have no clue as to why this works in admin panel and not via the cron.

问题出在WP_Query类中.

The problem is in the WP_Query class.

有一个名为query的变量和一个称为query的方法.查询功能如下:

There is a variable called query and a method called query. The query function looks like this :

public function query( $query ) {
    $this->init();
    $this->query = $this->query_vars = wp_parse_args( $query );
    return $this->get_posts();
}

如您所见,在此函数内,他们正在更新$ this-> query,但是由于未键入PHP,因此任何人都在猜测是否会更新变量或方法.更棘手的是,wp_parse_args在将数组传递给数组时返回对数组(指针)的引用.因此,PHP无法判断您是要使用变量还是要更新功能点.

As you can see, inside this function they are updating $this->query but since PHP isn't typed it is anybodies guess if it will update the variable or the method. Even trickier is that wp_parse_args returns a reference to an array (pointer) when passing an array into it. So PHP is not able to tell if you means a variable or if you are updating a function point.

最重要的是,由于某种原因,变量有一半会被更新,而函数指针有一半会被更新,这将结束整个PHP脚本的处理.

Bottomline is that for some reason half of the time the variable is updated and half of the time the funtion pointer, which ends the whole processing of the PHP scripts all together.

为什么他们将一个函数和一个变量都命名为相同的名称,我却一无所知,但是现在我被困住了,因为我试图将$ this-> query的所有出现重命名为其他名称,但这给了它比解决起来更多的问题.

Why they would ever name a function and a variable the same I have no clue, but now I am stuck because I tried to rename all occurences of $this->query to something else but that gave more problems than it solved.

顺便说一下,我的PHP版本是7.2.11

By the way my PHP version is 7.2.11

如果有人知道如何告诉PHP实际上应该更新这两个中的哪一个,那将会有很大帮助.

If anybody has any idea how to tell PHP which of the two it actually should update, that would help a lot.

推荐答案

问题已解决.最终,由于某种原因,对函数get_posts()的最后一次调用出错了.经过漫长的调试会话后,我注意到get_posts()的return语句看起来像

The problem was solved. Eventually It was the last call to the function get_posts() that for some reason went wrong. After a lengthy debug session I noticed that the return statement of get_posts(), that looks like

return $this->posts

实际上什么也没返回.当我将查询功能更新为:

Actually returned nothing at all. When I updated the query function to :

public function query( $query ) {
    $this->init();
    $this->query = $this->query_vars = wp_parse_args( $query );
    $this->get_posts();
    return $this->posts;
}

一切都解决了.我真的不知道为什么return语句在一个事件中不返回$ this-> posts的值,而在另一个事件中返回.

everything was solved. I really have no clue why the return statement didn't return the value of $this->posts in one event but did in the other.

至少对我来说,我可以继续从事这个项目.

For me atleast I can continue working on this project.

这篇关于从WordPress cron调用WooCommerce对象时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆