Support

Account

Home Forums Front-end Issues Two IFs, one conflict

Solving

Two IFs, one conflict

  • I could use some help understanding why this code isn’t working as expected.
    `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;
    `
    Scenario 1:Not all units are going to have ‘Accessories’. Therefore, if there are no accessories, do not post the link. This works.

    Scenario 2: If there is no Catalog, then don’t post the catalog link. This works.

    If there is both a catalog and Accessories, the links post. Which is great.

    Issue:
    If a product only has a Catalog and not Accessories, the catalog link will not appear.

    Is my code above faulty?

  • Hi @toad78

    Does the following code works on a product that has a catalog but no accessories?

    if ( $products->have_posts() ) {
        echo "No accessories";
    }

    Looking at your code, it seems your product is not detected as a product if it doesn’t have accessories.

    Thanks!

  • Nothing posts. Not even the ‘No accessories’ message.

  • Hi @toad78

    It means that that product is not queried by your code. Could you please tell me how did you set the accessories product? Did you use a plugin to do that?

    Also, could you please share the code you use to query the $products?

    Thanks!

  • The Accessories are set using WooCommerce’s UpSells section. If there are no items in the UpSells, I still want the DOWNLOAD CATALOG to display.

    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">&nbsp;</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();
    
    ?>

    What does work, but not layout I want, is to move the DOWNLOAD CATALOG link outside the loop. But then I would have to make modifications to my CSS to force the layout that I want if there was/wasn’t any Accessories to post.

  • @toad78

    Then you need to move the if statement or duplicate it like this:

    <div class="upsells products">
    
    	<a id="trigger" class="product btn"><?php _e( 'Accessories', 'woocommerce' ) ?> <span class="glyphicon glyphicon-chevron-down">&nbsp;</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 if ( $products->have_posts() ) : ?>
    		<?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(); ?>
    	<?php
    		endif;
    		wp_reset_postdata();
    	?>
            
    </div>

    Or this one:

    <?php if ( $products->have_posts() ) : ?>
    
     	<div class="upsells products">
    
    		<a id="trigger" class="product btn"><?php _e( 'Accessories', 'woocommerce' ) ?> <span class="glyphicon glyphicon-chevron-down">&nbsp;</span></a>
    		
    <?php endif; ?>
    		
    		<?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 if ( $products->have_posts() ) : ?>
    		<?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();
    ?>

    Hope this makes sense.

  • Thanks for all your work, James, but the above didn’t work either. Logically your code makes sense, but the way WP seems to be posting it still ignores posting the DOWNLOAD CATALOG with products that do not have an ‘Accessories’ associated with it.

  • Hi @toad78

    I believe the “catalog” is assigned to the parent product, not the upsells product, right? Could you please debug the returned value like the following?

    $file = get_field('catalog');
    var_dump($file);

    If it returns an empty value, could you please try the following?

    $file = get_field('catalog', $product->ID);
    var_dump($file);

    Thanks!

  • Each product is assigned a catalog, whether they have Upsells or not.

    WITH YOUR FIRST SET OF CODE:
    Products that have Upsells:
    string(80) “http://domainname.com/wp-content/uploads/Fire-Catalog.pdf&#8221;

    Products that have no Upsells are blank

    WITH YOUR SECOND SET OF CODE:
    Products that have Upsells:
    string(80) “http://domainname.com/wp-content/uploads/Fire-Catalog.pdf&#8221;

    Products that have no Upsells are blank

  • @toad78

    That is weird. Do you have a staging site where I can check this issue for you? You can provide the credentials to that site by setting the post visibility to private.

    Also, could you please share some screenshots of the issue with notes on how you want it to be?

    Thanks!

  • I wound up having to create a jQuery condition to post the correct file, as I couldn’t work out the ACF code. When I have more time, I’ll go back to it.

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Two IFs, one conflict’ is closed to new replies.