Woocommerce Product Display with Product Images

I have a wordpress site running the Wordpress WooCommerce plugin. Due to the huge volume of products that this site processes, we manage the list of products outside the site and load it. Many products do not yet have images, but they have a URL with a hard-coded image, so we can add them when we receive them. To get around broken images, I just looked a bit for the size of the image, and if I can’t find it and replace it with a placeholder.

$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
if (@getimagesize($src[0])) {
    //display product image
} else {
    //display placeholder image
}

In most cases, this works fine, but now I'm working on displaying products in categories. First I want to display all products with images, and then display products without images. The problem is that when the cycle starts, if I exclude products without images, it will go through the first 12 products and only display a subset of 12 that have images. I want him to continue the cycle until I have 12 products with images (if there are 12 products with images).

This is what I have right now that doesn't work.

<?php if ( have_posts() ) : ?>
    <ul class="products">
        <?php while ( have_posts() ) : the_post(); ?>
            <?php 
                $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
                if (@getimagesize($src[0])) {
                    woocommerce_get_template_part( 'content', 'product' );
                }
            ?>
        <?php endwhile; // end of the loop. ?>
    </ul>
<?php endif; ?>

Possible logical solutions that I could not execute would be to ignore some product during the loop (so that it would make another run if there is no image) or somehow encode my request as part of the loop requirement, i.e. drink it in $ args?

.

+5
1

. , . , , , , . , Wordpress , .

" " woocommerce. , " " " " "Woocommerce → → ", .

! 17000 , . , woocommerce, . , " " , .

, :

/**
 * This function sets the value of the menu_order of a product to 0 if the product contains an image and 1 if it does not
 * @param {int} $offset this is the start number for the batch
 * @param {int} $batch The number of products to process in the batch
 */
function setProductMenuOrder($offset, $batch) {
    global $post;
    $number_completed = 0;

    //define the arguments to be used in the loop
    $args = array( 'post_type' => 'product','offset' => $offset, 'numberposts' => $batch );
    $myposts = get_posts( $args );

    foreach( $myposts as $post ) : setup_postdata($post);
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID)); //getting image source

        //define post to be updated
        $my_post = array();
        $my_post['ID'] = $post->ID;

        if (@getimagesize($src[0])) { //if image source points to actual image menu order is set to 0
            $my_post['menu_order'] = '0';
            wp_update_post( $my_post ); //Update the post into the database
            $number_completed+=1;
        } else { //if it doesn't menu order is set to 1
            $my_post['menu_order'] = '1';
            wp_update_post( $my_post ); //Update the post into the database
            $number_completed+=1;
        }
    endforeach;
    echo '<p>Number of products edited: <strong>'.$number_completed.'</strong>.</p>';
}

, . 2000 . php config.php

define('WP_MAX_MEMORY_LIMIT', '256M');

, , .

+1

All Articles