Last active
February 21, 2024 21:21
-
-
Save psaikali/dcb49e07792230bf64bcd20db3732c99 to your computer and use it in GitHub Desktop.
Ajouter de nouveaux paramètres de requête à wc_get_products()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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