Woocommerce显示带有产品图像的产品

时间:2013-03-01 10:22:02

标签: php wordpress woocommerce

我有一个wordpress网站运行wordpress插件WooCommerce。由于该网站处理的产品数量庞大,我们一直在管理网站外的产品列表并上传它。很多产品还没有图像,但它们有一个硬编码的图像网址,所以我们可以在我们得到它们时添加它们。为了解决损坏的图像,我只是稍微搜索图像大小,如果我找不到它并用占位符替换它。

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

这在大多数情况下都运行良好,但现在我正在努力在一个类别中显示产品。我想首先显示带有图像的所有产品,然后显示没有图像的产品。问题是一旦循环开始,如果我排除没有图像的产品,它将循环通过前12个产品,只显示12个有图像的子集。我想要它做的是继续循环,直到我有12个带图像的产品(如果有12个带图像的产品)。

这就是我现在所做的不起作用。

<?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; ?>

我无法编码的可能逻辑解决方案是在循环中忽略某些产品(如果没有图像则会进行另一次运行)或以某种方式将我的查询编码为循环要求的一部分,即把它放在$ args?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我设法为我的问题找到了一个可行的解决方案。只是不可能通过单独的循环列出产品而不会造成混乱的分页。因此,合乎逻辑的步骤是使用循环并基本上根据图像是否存在来对产品进行排序。这会产生一个新问题,因为Wordpress排序无法确定图像链接是否指向文件。

但是,您可以在woocommerce中为产品设置“菜单顺序”。然后,如果您在“Woocommerce - &gt;设置 - &gt;目录”下将“默认产品排序”设置为“默认排序”,它将使用此菜单顺序在目录视图中订购产品。

大!但我仍然有17000个产品,我需要为每个产品指定一个菜单。我无法使用原本的woocommerce工具来完成这项工作。所以我决定写一个小插件来根据图像是否存在来更改每个产品的“菜单顺序”。

以下是用于写入post数据库的函数:

/**
 * 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件产品,没有失败。我确实需要在config.php中调整我的php内存限制

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

我仍然想知道是否有更简单的方法可以实现这一目标,但目前这种解决方案已足够。

相关问题