WooCommerce REST API v3 - 按修改日期过滤产品 [英] WooCommerce REST API v3 - Filter products by modified date

查看:26
本文介绍了WooCommerce REST API v3 - 按修改日期过滤产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WooCommerce 开发的新手,在 Wordpress 插件开发方面的经验很少.我正在尝试使用 WooCommerce REST API 来检索在特定日期之后修改的所有产品.

I'm new to WooCommerce development and have little experience in Wordpress plugin development. I'm trying to use the WooCommerce REST API for retrieving all the products that were modified after a particular date.

从此处提供的文档中,https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products,我可以看到可以使用一个名为date的参数按日期过滤产品这是指产品的创建日期.但是我想通过产品的修改日期来检索它.

From the documentation that is available here, https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products, I can see that products can be filtered by date by using a parameter called date which refers to the created date of the product. But I wanted to retrieve it by using the modified date of the products.

我找不到有关 WooCommerce 挂钩来扩展此功能的任何适当文档.对此的任何参考或指导将不胜感激.

I'm unable to find any proper documentation for WooCommerce hooks to extend this functionality. Any reference or guidance on this would be greatly appreciated.

推荐答案

在处理不同的文档和 API 几个小时后,这里是对我有用的解决方案.

After several hours of meddling with different documentation and APIs, here's the solution that worked for me.

add_filter('woocommerce_rest_product_object_query', function(array $args, \WP_REST_Request $request) {
    $modified_after = $request->get_param('modified_after');

    if (!$modified_after) {
        return $args;
    }

    $args['date_query'][0]['column'] = 'post_modified';
    $args['date_query'][0]['after']  = $modified_after;

    return $args;

}, 10, 2);

需要注意的是,这是针对 v3 版本(编写此答案时的最新版本),它可能适用于也可能不适用于以前的版本.

It's to be noted that this is for version v3 (latest when this answer is written) and it may or may not work for the previous versions.

这篇关于WooCommerce REST API v3 - 按修改日期过滤产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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