Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ajouter de nouveaux paramètres de requête à wc_get_products()
<?php
namespace Mosaika;
/**
* Handle a custom 'vendor' query var to get products of a specific Vendor.
* Handle a custom 'featured_by_vendor' query var to get featured products of a specific Vendor.
*
* @param array $query_args - WP_Query args
* @param array $query_vars - WC_Product_Query args
* @return array Modified $query_args
*/
function add_vendor_custom_wc_query_args( $query_args, $query_vars ) {
// Filter by a unique vendor.
if ( ! empty( $query_vars['vendor'] ) ) {
$query_args['tax_query'][] = [
'taxonomy' => WC_PRODUCT_VENDORS_TAXONOMY,
'field' => 'id',
'terms' => (int) $query_vars['vendor'],
];
}
// Only get vendor featured products, and order by featured time (descending).
if ( ! empty( $query_vars['featured_by_vendor'] ) && (int) $query_vars['featured_by_vendor'] > 0 ) {
$query_args['meta_query'][] = [
'key' => '_featured_by_vendor',
'value' => (int) $query_vars['featured_by_vendor'],
'compare' => '>',
];
$query_args['order'] = 'DESC';
$query_args['orderby'] = 'meta_value_num';
$query_args['meta_key'] = '_featured_by_vendor';
}
return $query_args;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', __NAMESPACE__ . '\\add_vendor_custom_wc_query_args', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.