I am developing a WooCommerce shop for a customer. The shop data (products, product categories, customers) is imported to the shop via the WooCommerce API.
Some products should only be visible for certain customers. That's why the ERP system provides "product pools" for all products and customers. These product pools are stored as an array in a meta field. The arrays are stored as serialized data in the database.
Every customer can be assigned to multiple product pools, also every product can be assigned to multiple product pools.
I tried to use the pre_get_posts hook, to manipulate the product query, depending on the current user's product pools, but the query doesn't show any results.
That's my code:
if ( $query->is_main_query() && ! is_admin() && is_shop() ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$user_product_pools = explode(',',get_user_meta($user_id,'customer_product_pools', true));
// Check whether the meta field exists in the request array
if ($meta_value = $query->get('meta_query')) {
// Add our condition for the meta field
$meta_value[] = [
'key' => '_dpwc_anq_product_pools',
'value' => $user_product_pools,
'compare' => 'IN',
];
// Update the meta-query array
$query->set('meta_query', $meta_value);
} else {
// If the meta-query array does not yet exist, create it
$query->set('meta_query', [
[
'key' => '_dpwc_anq_product_pools',
'value' => $user_product_pools,
'compare' => 'IN',
],
]);
}
}
}
This doesn't work. I think the issue is, that I try to compare to arrays with my meta query. Is there any way to archive this? Do I have to change the way I am storeing the pool data? Would it be better to save all the product pools to seperate meta fields?
I would appreciate any help. Thank you.
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand