简体   繁体   English

在 WooCommerce 中的 WC_Product_Query 上按名称“LIKE”过滤产品

[英]Filter products by name “LIKE” on a WC_Product_Query in WooCommerce

In WooCommerce using wc_get_products() function I would like to find a way to filter products by name using the LIKE operator.在 WooCommerce 中使用wc_get_products() function 我想找到一种使用LIKE运算符按名称过滤产品的方法。

Actually I am only able to filter products by a specific defined name with:实际上,我只能通过特定定义的名称过滤产品:

$args = array(
    'limit' => 5,
    'name' => 'Test',
);
$result = wc_get_products( $args );

Is it possible to filter products where the name is LIKE 'test' ?是否可以过滤名称为 LIKE 'test' 的产品?

If you look to WooCommerce official documentation for WC_Product_Query at the end on the section "Adding Custom Parameter Support" you will see that you can manipulate the WC_Product_Query with a custom hooked function.如果您查看“添加自定义参数支持”部分末尾WC_Product_Query的 WooCommerce 官方文档,您将看到您可以使用自定义挂钩 function 操作 WC_Product_Query。

So to filter the query with a product name "LIKE" parameter, you can extend the query with the search "s" argument, that will do the trick as follow:因此,要使用产品名称“LIKE”参数过滤查询,您可以使用搜索“s”参数扩展查询,这将起到如下作用:

add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
function handle_custom_query_var( $query, $query_vars ) {
    if ( isset( $query_vars['like_name'] ) && ! empty( $query_vars['like_name'] ) ) {
        $query['s'] = esc_attr( $query_vars['like_name'] );
    }

    return $query;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 tested and works.测试和工作。


USAGE example with custom argument "like_name":使用自定义参数“like_name”的用法示例

$args = array(
    'limit' => 5,
    'like_name' => 'test',
);

wc_get_products( $args );

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

相关问题 改进 Woocommerce 产品的 WC_Product_Query 变体 - Improve WC_Product_Query on Woocommerce products with variations 按 WooCommerce WC_Product_Query 中的产品属性项过滤 - Filter by product attribute terms in a WooCommerce WC_Product_Query 无法获得有库存和无库存的产品 WC_Product_Query Woocommerce - Unable to get products that are both In-Stock and Out-of-Stock WC_Product_Query Woocommerce 日期产品自定义字段在 Woocommerce WC_Product_Query 中的使用 - Date product custom field usage in Woocommerce WC_Product_Query 在 Woocommerce 3 上的 WC_Product_Query 中使用自定义元数据 - Use custom meta data in a WC_Product_Query on Woocommerce 3 WC_Product_Query 在函数中返回空。php 但在归档产品中工作。php - WC_Product_Query return empty in functions.php but working in archive-product.php 使用 wc_get_orders WooCommerce 查询时,MySQL LIKE %filter% 等效 - MySQL LIKE %filter% equivalent when query with wc_get_orders WooCommerce 使用 WooCommerce 中的 WC_Query 按作者 ID 获取产品? - Get products by author id using a WC_Query in WooCommerce? 从 Woocommerce 中的产品属性值过滤产品 - Filter products from product attributes values in Woocommerce WooCommerce 使用 ACF 和产品类别过滤产品 - WooCommerce Filter products with ACF and Product Category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM