To hide products in WooCommerce based on a custom field or Advanced Custom Fields (ACF) value, you’ll need to use some custom code. Specifically, you’ll have to hook into the WooCommerce product query and modify it to include your custom field or ACF value as a condition for hiding products. Here’s a step-by-step guide to achieving this:
Step 1: Install and Activate Required Plugins
Make sure you have the following plugins installed and activated:
1. WooCommerce: The main WooCommerce plugin for your online store.
2. Advanced Custom Fields (ACF): A powerful plugin for creating custom fields for various elements, including products.
Step 2: Add a Custom Field to Your Products
In this step, you’ll add a custom field (e.g., ‘hide_product’) to your products using ACF. You can do this by going to the ACF settings in your WordPress admin panel and creating a new field group for products. Add a new field of type “True / False” or “Checkbox” and name it ‘hide_product’. Assign this field group to the ‘Product’ post type.
Step 3: Get the Custom Field Value in the Product Loop
Now, you’ll need to retrieve the value of the ‘hide_product’ custom field for each product in the product loop. This can be done within the WooCommerce product query using the `woocommerce_product_query` hook. Add the following code to your theme’s functions.php file or in a custom plugin:
function custom_hide_products_based_on_acf_value($q) { if (!is_admin() && is_shop()) { $meta_query = $q->get('meta_query'); // Add the custom field condition to hide products $meta_query[] = array( 'key' => 'hide_product', // Replace 'hide_product' with your ACF field name 'value' => '1', 'compare' => '!=', // Show products where the 'hide_product' field is not equal to '1' ); $q->set('meta_query', $meta_query); } } add_action('woocommerce_product_query', 'custom_hide_products_based_on_acf_value');
Step 4: Save Changes and Test
After adding the code, save the changes to your theme’s functions.php file or your custom plugin. Now, when you set the ‘hide_product’ field to ‘1’ for a specific product, it should be hidden from the shop and archive pages.
Please note that this code will only hide the products from the shop and archive pages. The products will still be accessible via direct URLs or search, but they won’t appear in product listings. If you want to completely restrict access to the product, additional steps are needed, such as redirecting or blocking access to the individual product pages based on the custom field value.
Always make sure to create a backup of your website before making any changes to the code, and be careful when modifying functions.php or any core files to prevent potential issues. Additionally, consider using a child theme or custom plugin to keep your modifications separate from the main theme files.
To hide products in WooCommerce based on multiple custom fields or Advanced Custom Fields (ACF) values using the `woocommerce_product_query` hook and `meta_query`, you can follow the steps below:
Step 1: Create a Custom Field Using ACF
If you haven’t already done so, create a custom field (e.g., ‘hide_product’) using Advanced Custom Fields for your WooCommerce products. Make sure to assign this field to the ‘Product’ post type.
Step 2: Add Code to Functions.php or Custom Plugin
Add the following code to your theme’s functions.php file or create a custom plugin:
function custom_hide_products_based_on_acf_value($q) { if (!is_admin() && is_post_type_archive('product') && $q->is_main_query()) { $meta_query = $q->get('meta_query'); // Add the first custom field condition to hide products $meta_query[] = array( 'key' => 'hide_product', // Replace 'hide_product' with your ACF field name 'value' => '1', 'compare' => '!=', // Show products where the 'hide_product' field is not equal to '1' ); // Add more custom field conditions using OR relation $meta_query[] = array( 'key' => 'another_custom_field', // Replace with another custom field name 'value' => 'some_value', 'compare' => '=', // Show products where the 'another_custom_field' is equal to 'some_value' ); // Set the relation between the meta queries to 'OR' $meta_query['relation'] = 'OR'; $q->set('meta_query', $meta_query); } } add_action('woocommerce_product_query', 'custom_hide_products_based_on_acf_value');
Step 3: Customize the Meta Queries
In the code above, you can add more custom field conditions by appending additional arrays to the `$meta_query` variable. Set the `’key’` to the name of the custom field, the `’value’` to the desired value to match, and the `’compare’` to the comparison operator you want to use.
Make sure to replace `’hide_product’`, `’another_custom_field’`, and `’some_value’` with the correct names of your ACF custom fields and their corresponding values.
The code sets the relation between multiple meta queries to ‘OR’. This means that a product will be shown if it meets any of the specified conditions. If you want to apply an ‘AND’ relation, you can set the `’relation’` to ‘AND’.
Step 4: Save Changes and Test
Save the changes to your functions.php file or custom plugin and test the behavior on your WooCommerce shop/archive pages. Products with the specified custom field values will be hidden based on your defined conditions.