I have created 7 custom product fields (checkboxes). I want to use the output to make products visible on sub sites in my WooCommerce (WordPress network) multi shop.
The code below works but instead of filtering the products BEFORE the output starts, it filters the products AFTER the output starts. As a result I get almost empty shop pages.
I still have 89 shop pages but most of them have none or just a few products.
Hope I made myself clear.
Here is the code:
if ( woocommerce_product_loop() ) {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
global $product;
$blog_id = get_current_blog_id();
while ( have_posts() ) {
the_post();
if ($blog_id == '1') {
$visibility = get_post_meta( $product->get_id(), '_visibility_3rdmillennium', true );
} elseif ($blog_id == '2') {
$visibility = get_post_meta( $product->get_id(), '_visibility_fight2win', true );
} elseif ($blog_id == '3') {
$visibility = get_post_meta( $product->get_id(), '_visibility_muaythai', true );
} elseif ($blog_id == '4') {
$visibility = get_post_meta( $product->get_id(), '_visibility_taekwondo', true );
} elseif ($blog_id == '5') {
$visibility = get_post_meta( $product->get_id(), '_visibility_xprtfightgear', true );
} elseif ($blog_id == '6') {
$visibility = get_post_meta( $product->get_id(), '_visibility_hayabusashop', true );
} elseif ($blog_id == '7') {
$visibility = get_post_meta( $product->get_id(), '_visibility_kmushop', true );
}
if ($visibility == 'yes') {
/**
* Hook: woocommerce_shop_loop.
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
}
What I want to accomplish is that the whole product loop gets filtered before the output start so I won't get 89 (half filled) shop pages but (f.i.) 12 (fully filled) shop pages for each sub site.
Thank you in advance very much for your effort.
CodePudding user response:
I found out myself so let's share it. Filter a product loop with custom product fields meta goes like this:
if ( wc_get_loop_prop( 'total' ) ) {
global $product;
$blog_id = get_current_blog_id();
while ( have_posts() ) {
the_post();
if ($blog_id == '1') {
$visibility = get_post_meta( $product->get_id(), '_visibility_3rdmillennium', true );
} elseif ($blog_id == '2') {
$visibility = get_post_meta( $product->get_id(), '_visibility_fight2win', true );
} elseif ($blog_id == '3') {
$visibility = get_post_meta( $product->get_id(), '_visibility_muaythai', true );
} elseif ($blog_id == '4') {
$visibility = get_post_meta( $product->get_id(), '_visibility_taekwondo', true );
} elseif ($blog_id == '5') {
$visibility = get_post_meta( $product->get_id(), '_visibility_xprtfightgear', true );
} elseif ($blog_id == '6') {
$visibility = get_post_meta( $product->get_id(), '_visibility_hayabusashop', true );
} elseif ($blog_id == '7') {
$visibility = get_post_meta( $product->get_id(), '_visibility_kmushop', true );
}
if ($blog_id == '5') {
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_visibility_xprtfightgear',
'value' => 'yes',
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
endif;
wp_reset_postdata();
}
}
}