• Resolved buzut

    (@buzut)


    Hi!

    I’m having a PHP Notice:
    Undefined index: loop in /wp-content/plugins/duracelltomi-google-tag-manager/integration/woocommerce.php.

    I don’t really get what’s wrong, it happens when I request featured products in my theme. Here’s the function I use to query the featured products :

    
    function featured_products() {
        $query = new WP_Query([
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => 10,
            'orderby' => 'date',
            'order' => 'desc',
            'tax_query' => [[
                'taxonomy' => 'product_visibility',
                'field'    => 'term_id',
                'terms'    => 'featured',
                'operator' => 'IN',
            ]]
        ]);
    
        if ($query->have_posts()): while ($query->have_posts()) : $query->the_post();
            wc_get_template_part('content', 'product-slider');
        endwhile; endif;
        wp_reset_postdata();
    }
    
    • This topic was modified 4 years, 2 months ago by buzut.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    A similar topic was discussed earlier but unfortunately I was not able to provide a resolution. Normally, WooCommerce adds the loop key into the $woocommerce_loop global variable witch is missing in your case. Probably you need to manually replicate this functionality with your code. The loop key should show the index of the product in the product list currently being processed.

    Thread Starter buzut

    (@buzut)

    Thank you for getting back to me. So in the end, I solved it by using the native WooCommerce loop. Might be useful for those who encounter the same issue.

    
    function featured_products() {
        $products_ids = wc_get_products([
            'status' => 'publish',
            'return' => 'ids',
            'featured' => true
        ]);
    
        foreach($products_ids as $id) {
            $post_object = get_post($id);
            setup_postdata($GLOBALS['post'] =& $post_object);
            wc_get_template_part('content', 'product-slider');
        }
    
        wp_reset_postdata();
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Getting Notice Undefined index’ is closed to new replies.