How to hide product based on user setting
-
Hi,
I have a function below which excludes WooCommerce products if certain criteria are met.
Currently, in the background, there is a drop-down list of regions/locations which the user have to select by default so products can be excluded by their location.
My question is how do I get the function below to work on non-archived pages ie “search/any page with a custom query that lists products”.
I have also rewritten the function to use pre_get_posts found here https://gist.github.com/anonymous/1af5ca94dcf38634a4c2a8cd41ef5b6a but I seem to be missing something.
If someone could help me out that would be fantastic.
Function tm_remove_product_stock_region($is_visible, $id) { $product = wc_get_product($id); $product_variable = new WC_Product_Variable($id); $product_variations = $product_variable->get_available_variations(); /** Get users region. */ $user_region = tm_get_user_region(); foreach ($product_variations as $variation) { /** Get products regions. */ $product_region = $variation['attributes']['attribute_pa_regions']; //if ( $user_region === $product_region && ! $variation['is_in_stock'] ) { if (!$product->is_in_stock() && $user_region === $product_region) { $is_visible = false; return $is_visible; } //else if ( $user_region === $product_region && $variation['is_in_stock'] ) { else if ($product->is_in_stock() && $user_region === $product_region) { $is_visible = true; return $is_visible; } } } add_filter('woocommerce_product_is_visible', 'tm_remove_product_stock_region', 10,2);
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to hide product based on user setting’ is closed to new replies.