• Resolved leighmaher

    (@leighmaher)


    I’ve had to create a custom query for featured products on the home page of this website. However, the prices only show in the base currency and don’t take tax into account.

    Is there any way for the plugin to hook into custom queries?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,
    Which function do you use to display price of your featured products?

    Thread Starter leighmaher

    (@leighmaher)

    Hi,

    Thanks for replying.

    It’s a custom WP_Query. Currently getting the price from this line (and looping through that for products with variant pricing):
    $prices = get_post_meta( get_the_ID(), ‘_price’ );

    Here’s the full code:

    add_shortcode (‘woo_featured_products’, ‘woo_featured_products’ );
    /**
    * Create WooCommerce Featured Image Loop Slider
    */
    function woo_featured_products() {
    ob_start();

    ?>

    <div class=”slick-container-home-page”>
    <ul class=”slick-arrows-home-page”>
    <li class=”slick-prev-custom”><span>Previous</span></i>
    <li class=”slick-next-custom”><span>Next</span></i>

    <div class=”home-featured-products”>
    <?php
    $args = array(
    ‘post_type’ => array(‘product’, ‘product_variation’),
    ‘posts_per_page’ => 12,
    ‘post_parent’ => array(0, 1000),
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘name’,
    ‘terms’ => ‘featured’,

    ),
    ),
    );

    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();

    $product_thumbnail = get_the_post_thumbnail (get_the_ID(), ‘small’);

    // if you leave “true” paramenter in, it will not access price variants
    // $price = get_post_meta( get_the_ID(), ‘_price’, true );

    // access the prices including variant price array
    $prices = get_post_meta( get_the_ID(), ‘_price’ );

    // $product_data = get_post_meta( get_the_ID() );
    // var_dump($product_data);

    ?>

    <div>
    “><?php echo $product_thumbnail; ?>
    <div class=”featured-prodcuts-name-price”>
    <h4><?php the_title(); ?></h4>
    <?php

    // We need to keep track of what variable price we are on
    $i = 0;
    $len = count($prices); // allows us to pinpoint the last variant when we loop through variable prices
    foreach ($prices as $price) {
    // var_dump($price);
    if ($i == 0) {
    echo wc_price($price); // echo’s out the first price for variants and only price for simple products
    } else if ($i == $len – 1) {
    echo ‘ – ‘ . wc_price($price); // this echo’s out the last variant price (highest price) allowing us to display the range of prices for a product
    }
    $i++;
    }

    ?>
    </div>
    </div>

    <?php endwhile;
    } else {
    echo __( ‘No products found’ );
    }
    wp_reset_postdata();
    ?>
    </div>
    </div>

    <?php
    return ob_get_clean();
    }

    Instead of using get_post_meta to get product price, please use it like this inside your loop:

    $product_id=get_the_ID();
    $product=wc_get_product($product_id);
    if($product){
    echo $product->get_price_html();
    }

    If you still want to use it your way, just use the function wmc_get_price() to convert your price before printing out:

    if(function_exists('wmc_get_price')){
    $price=wmc_get_price($price);
    }
    echo wc_price($price);

    And please remember to wrap your code snippet inside the code tag in your future replies.

    • This reply was modified 4 years, 2 months ago by kimvt1991.
    Thread Starter leighmaher

    (@leighmaher)

    Hi, thanks so much for your reply. The first option worked and is a way cleaner solution. Not sure how I missed the get_price_html() function when I was researching this. Thanks again.

    You’re welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom WP_Query for featured products’ is closed to new replies.