• Resolved DodoDog

    (@dododog)


    Hopefully someone can help me out with this display problem,… it’s driving me nuts!
    I build a theme with Underscore displaying the Featured Products in a sidebar. It all worked great until I updated to WooCommerce Version 3.0.5.
    The problem:
    * If I deselect the “Star” to mark a product as Featured it still shows in the sidebar
    * If I choose a new product and mark it as Featured it doesn’t show in the sidebar.

    WooCommerce now uses “product_visibility taxonomy instead of meta for featured products” but i have no clue how to use that information in my code
    (see: https://woocommerce.wordpress.com/2017/04/04/say-hello-to-woocommerce-3-0-bionic-butterfly/)

    This is the code I use for showing Featured Products:

    $bandproduct_args = array(
    ‘post_type’ => array( ‘product’ ),
    ‘meta_key’ => ‘_featured’,
    ‘meta_value’ => ‘yes’,
    ‘orderby’ => ‘modified’,
    ‘order’ => ‘DESC’,
    ‘posts_per_page’ => ‘-1’,
    );

    But as said, it’s not displaying the Featured products properly,…

    Your help is appreciated, thank you!
    Cheers, Eva

Viewing 6 replies - 1 through 6 (of 6 total)
  • Looking at WooCommerce core code, something like this:

    $meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
    	'taxonomy' => 'product_visibility',
    	'field'    => 'name',
    	'terms'    => 'featured',
    	'operator' => 'IN',
    	);
    
    $bandproduct_args = array(
    	'post_type'           => 'product',
    	'post_status'         => 'publish',
    	'posts_per_page'      => '-1',
    	'orderby'             => 'modified',
    	'order'               => 'DESC',
    	'meta_query'          => $meta_query,
    	'tax_query'           => $tax_query,
    );

    With example output code (found on a Stack Exchange thread):

    <?php
    $meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
    	'taxonomy' => 'product_visibility',
    	'field'    => 'name',
    	'terms'    => 'featured',
    	'operator' => 'IN',
    	);
    
    $bandproduct_args = array(
    	'post_type'           => 'product',
    	'post_status'         => 'publish',
    	'posts_per_page'      => '-1',
    	'orderby'             => 'modified',
    	'order'               => 'DESC',
    	'meta_query'          => $meta_query,
    	'tax_query'           => $tax_query,
    );
    $loop = new WP_Query( $bandproduct_args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
        
           <li>    
                <?php 
                    if ( has_post_thumbnail( $loop->post->ID ) ) 
                        echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 
                    else 
                        echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="65px" height="115px" />'; 
                ?>
                <h3><?php the_title(); ?></h3>
    
                <?php 
                    echo $product->get_price_html(); 
                    woocommerce_template_loop_add_to_cart( $loop->post, $product );
                ?>    
            </li>
    
    <?php 
        endwhile;
        wp_reset_query(); 
    ?>
    Thread Starter DodoDog

    (@dododog)

    Thank you so much purbeckpixels, your answer totally solved the problem! The featured products list exactly how they should, marking products as featured in the admin as well. I’m so gratefull you took the time to help out, a HUGE thank you to you!
    Eva

    You’re welcome. Always happy to help when I can.

    I have the following code that doesnt seem to work with WordPress 3. Could you help fix it?

    <?php

    $args = array(
    ‘post_type’ => ‘product’,
    ‘meta_key’ => ‘_featured’,
    ‘meta_value’ => ‘yes’,
    ‘posts_per_page’ => 1,
    );

    $featured_query = new WP_Query( $args );
    if ($featured_query->have_posts()) : while ($featured_query->have_posts()) : $featured_query->the_post(); ?>
    <div class=”botm cont-centre”>
    <h2>Featured Book</h2>

    <div class=”book-feature”>
    <?php if(has_post_thumbnail()): ?>
    <?php
    $image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
    $image = get_the_post_thumbnail( $post->ID, ‘product-thumb’ , array(
    ‘title’ => ‘Book cover of’ . $image_title,
    ‘alt’ => ‘Book cover of’ . $image_title
    ));

    echo $image;
    ?>
    <?php else: ?>
    /images/no-cover.jpg” alt=”No book cover available, blank cover” title=”No book cover available for this result” />
    <?php endif; ?>
    </div>

    <?php
    $title = get_the_title();
    $authors = get_field(‘book_author_select’);
    $publisher = get_field(‘book_publisher’);
    $pubished = get_field(‘book_year_published’);
    $condition = get_field(‘book_condition’);
    $cover_type = get_field(‘book_cover_type’);
    $edition = get_field(‘book_edition’);
    $book_id = get_field(‘book_id’);
    $isbn = get_field(‘book_isbn-13_number’);
    $isbn10 = get_field(‘book_isbn-10_number’);
    $price = $product->get_price_html();
    $narrative = get_field(‘book_narrative’);
    ?>

    <div class=”book-content”>
    <ul class=”book-details”>

    • Title: <?php echo $title; ?>
    • Author: <?php echo $authors[0]->post_title; ?>
    • Publisher: <?php echo $publisher->post_title; ?>
    • Published: <?php echo $pubished; ?>
    • Edition: <?php echo $edition; ?>
    • Ref: <?php echo $book_id; ?>
    • <div class=”book-desc”>
      <h3>About The Book</h3>
      <p><?php echo $narrative; ?></p>

    </div>
    </div>
    <?php endwhile; ?>
    </div>
    <?php endif; wp_reset_query();?>

    Please wrap your PHP code in code tags, it’s hard to read and it’s breaking the forum’s page layout.

    Try swapping:

    $args = array(
    ‘post_type’ => ‘product’,
    ‘meta_key’ => ‘_featured’,
    ‘meta_value’ => ‘yes’,
    ‘posts_per_page’ => 1,
    );

    For:

    $meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
    	'taxonomy' => 'product_visibility',
    	'field'    => 'name',
    	'terms'    => 'featured',
    	'operator' => 'IN',
    );
    
    $args = array(
    	'post_type'           => 'product',
    	'post_status'         => 'publish',
    	'posts_per_page'      => '1',
    	'meta_query'          => $meta_query,
    	'tax_query'           => $tax_query,
    );

    thanks for this, it doesn’t seem to let me edit my initial post but can confirm this worked

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Featured products display problem in custom build theme WooCommerce 3.0’ is closed to new replies.