【问题标题】:Getting "per_page must be between 1 (inclusive) and 100 (inclusive)" while requesting the categories in WooCommerce在 WooCommerce 中请求类别时获取“per_page 必须介于 1(含)和 100(含)之间”
【发布时间】:2020-08-10 19:31:52
【问题描述】:

https://example.com/wp-json/wc/v2/products/categories/?per_page=150&consumer_key=ck_991580c4b11ae619bef763195f67c311aa2bc&consumer_secret=cs_07cacbb022086cf185beb0145fe0e5db01c57&currentLanguage=en

   {
        "code": "rest_invalid_param",
        "message": "Invalid parameter(s): per_page",
        "data": {
            "status": 400,
            "params": {
                "per_page": "per_page must be between 1 (inclusive) and 100 (inclusive)"
            }
        }
    }

我正在尝试使用 wp JSON API 从 woo-commerce 中获取 150 个类别。我无法达到 100。有没有办法使用挂钩来解决这个问题。

【问题讨论】:

  • 答案是否定的。您可以在这里获取更多信息:developer.wordpress.org/rest-api/using-the-rest-api/pagination 但是您可以在您的情况下传递页码以正确获取数据
  • @BrijeshDhanani 有什么办法可以进行硬编码 150。因为我是从我的移动应用程序中请求的。所以我不能在那里改变
  • 检查我的答案
  • 或者有什么方法可以在我们调用的时候编写查询

标签: php wordpress woocommerce pagination woocommerce-rest-api


【解决方案1】:

查看这里的文档: https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

看起来 per_page 参数可以有 1 到 100 之间的任何整数值。所以你的问题的答案是 NO

如果你想硬编码

/**
 * Retrieves the query params for the collections.
 *
 * @since 4.7.0
 *
 * @return array Query parameters for the collection.
 */
public function get_collection_params() {
    return array(
        'context'  => $this->get_context_param(),
        'page'     => array(
            'description'       => __( 'Current page of the collection.' ),
            'type'              => 'integer',
            'default'           => 1,
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
            'minimum'           => 1,
        ),
        'per_page' => array(
            'description'       => __( 'Maximum number of items to be returned in result set.' ),
            'type'              => 'integer',
            'default'           => 10,
            'minimum'           => 1,
            'maximum'           => 1000,
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
        ),
        'search'   => array(
            'description'       => __( 'Limit results to those matching a string.' ),
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'validate_callback' => 'rest_validate_request_arg',
        ),
    );
}

/wp-includes/rest-api/endpoints/class-wp-rest-controller.php #L381

【讨论】:

  • 我读到了重点。但是有什么方法可以做这件事我可以在核心文件中编码。我在移动应用程序中使用的端点我无法在那里更新。我已经准备好在核心文件中进行硬编码了。
【解决方案2】:
add_action( 'rest_product_query', 'product_override_per_page' );

/* 
 * params is the query array passed to WP_Query
*/
function product_override_per_page( $params ) {
    if ( isset( $params ) AND isset( $params[ 'posts_per_page' ] ) ) {
        $params[ 'posts_per_page' ] = "200";
    }

return $params;
}

不要错过在钩子中用您的帖子类型替换客户

【讨论】:

  • 成功了吗??
  • { "code": "rest_invalid_param", "message": "Invalid parameter(s): per_page", "data": { "status": 400, "params": { "per_page": "per_page must be between 1 (inclusive) and 100 (inclusive)" } } }
  • 您在 functions.php 文件中尝试过的内容。可以分享一下吗?
  • add_action('rest_product_cat_query', 'customer_override_per_page'); /* * params 是传递给 WP_Query 的查询数组 */ function customer_override_per_page( $params ) { if ( isset( $params ) AND isset( $params[ 'posts_per_page' ] ) ) { $params[ 'posts_per_page' ] = "200 "; } 返回 $params; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多