Home › Forums › General Issues › True/False Product Category
Hello,
I am trying to use a true/false field called series_one_work
to define what product categories have only 1 product. I want to control the layout options of my theme (GeneratePress)
The following code works, but I would like to use the ACF instead of manually adding the category slugs.
add_filter( 'option_generate_woocommerce_settings','lh_custom_category_wc_columns' );
function lh_custom_category_wc_columns( $options ) {
if ( is_product_category( 'twilight' ) || is_product_category( 'biota' ) || is_product_category( 'astroculture-eternal-return' ) )
{
$options['columns'] = 1;
}
return $options;
}
How should I use the true/false field here? Any suggestions?
Thank you,
Raul
Hey Raul,
You can loop categories using a “meta_key” / “meta_value” filter to update only categories with the value TRUE in “series_one_work”
P.s.: sorry about my english
Thank you, I tried a few options but I am doing something wrong…
I get the 1 column layout applied to every category
/*Use 1 column layout for art series with one work*/
add_filter( 'option_generate_woocommerce_settings','lh_custom_category_wc_columns' );
function lh_custom_category_wc_columns( $options ) {
if ( $query->query_vars['product_cat'] ) {
$query->set('meta_key', 'series_one_work');
$query->set('meta_value', '1');
}
{
$options['columns'] = 1;
}
return $options;
}
Try this
function vv_custom_cat_layout( $options ) {
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => true,
'meta_key' => 'name_of_field',
'meta_value' => 'the_desired_value'
) );
foreach($terms as $term){
$options['columns'] = 1;
}
return $options;
}
add_filter( 'option_generate_woocommerce_settings','vv_custom_cat_layout' );
*not tested
Thanks so much @vverner!
I tested with:
'taxonomy' => 'post_tag',
and
'taxonomy' => 'product_cat',
But is not working. Remember this is a WooCommerce product category…
Here are the other values:
'hide_empty' => true,
'meta_key' => 'series_one_work',
'meta_value' => '1'
Any other suggestion?
<?php
function vv_custom_cat_layout( $options ) {
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'meta_key' => 'series_one_work',
'meta_value' => '1'
) );
$AcfFilter = array();
foreach($terms as $term){
array_push($AcfFilter, $term->name); # i'm never sure if is term_name or just name, sorry
}
if ( is_product_category( $AcfFilter ) )$options['columns'] = 1;
return $options;
}
add_filter( 'option_generate_woocommerce_settings','vv_custom_cat_layout' );
Not working yet…
I tested with name, term_name and $term->slug
. Nothing works.
I tried writing the slug manually, for example
if ( is_product_category( 'biota') )$options['columns'] = 1;
and it works, so the problem might be in the array_push?
A good friend on mine solved the issue. Here is the working code:
// Use 1 column layout for art series with one work ACF based
function vv_custom_cat_layout( $options ) {
$terms = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => true,
'meta_query' => [
'key' => 'series_one_work',
'value' => 1
]
]);
$AcfFilter = [];
foreach($terms as $term)
array_push($AcfFilter, $term->slug); // i'm never sure if is term_name or just name, sorry
if ( is_product_category( $AcfFilter ) )
$options['columns'] = 1;
return $options;
}
add_filter( 'option_generate_woocommerce_settings','vv_custom_cat_layout' );
Thanks @vverner for all your help! really appreciated.
wow thanks for the code and now it is working for me without any error. ones again thanks for the team.
titanium tv
The topic ‘True/False Product Category’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.