如何向产品类别端点添加额外数据?我可以通过应用过滤器woocommerce_rest_prepare_product_object来更新产品端点
似乎找不到为类别端点应用筛选器的函数。
我就是这样修改产品端点的:
add_filter('woocommerce_rest_prepare_product_object', 'custom_change_product_response', 20, 3);
add_filter('woocommerce_rest_prepare_product_variation_object', 'custom_change_product_response', 20, 3);
function custom_change_product_response($response, $object, $request) {
$response->data['custom_field'] = "Custom Data";
return $response;
}我想将所有的子节点发送到/products/categories端点。
发布于 2022-03-13 16:11:05
我只是在找了很多东西之后才找到的。我不得不修改过滤器方法:
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );在这里,类别的分类是:product_cat
因此,要添加数据,必须使用筛选器:
add_filter('woocommerce_rest_prepare_product_cat', 'custom_change_category_response', 20, 3);在发送子类别时,我们将使用:
$catTerms = get_terms('product_cat', array(
'hide_empty' => 0,
'orderby' => 'ASC',
'child_of'=> $id
));所以整个代码是这样的:
function custom_change_category_response($response, $object, $request) {
$id = $response->data['id'];
// Get subcategories of the current category
$catTerms = get_terms('product_cat', array(
'hide_empty' => 0,
'orderby' => 'ASC',
'child_of'=> $id
));
$response->data['children_count'] = count($catTerms);
$response->data['childs_array'] = $catTerms;
return $response;
}
add_filter('woocommerce_rest_prepare_product_cat', 'custom_change_category_response', 20, 3);https://stackoverflow.com/questions/71458546
复制相似问题