With the below code, I’m trying to have the IF statement do the following:
If there is a Catalog to display, post it.
If there are Accessories to display, post it.
My issue:
The loop only works if there are Accessories to display, even if there is a Catalog to display. If there are no Accessories to display, the Catalog won’t display either.
How can this be modified to where this can be an either/or statement?
<?php
/**
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>
<?php
global $product, $woocommerce_loop;
$upsells = $product->get_upsells();
if ( sizeof( $upsells ) === 0 ) {
return;
}
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="upsells products">
<a id="trigger" class="product btn"><?php _e( 'Accessories', 'woocommerce' ) ?> <span class="glyphicon glyphicon-chevron-down"> </span></a>
<?php
$file = get_field('catalog');
if( $file ) {
$url = wp_get_attachment_url( $file );
?><a id="catalog" target="_blank" class="product btn" href="<?php echo $url; ?>" >DOWNLOAD CATALOG</a><?php
}
?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'accessories' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
?>