php Woocommerce 显示带有产品图片的产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15155338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce Displaying Products With Product Images
提问by Jeffrey Jenkinson
I have a wordpress site running the wordpress plugin WooCommerce. Because of the sheer volume of products this site is handling we have been managing the product list outside of the site and uploading it. A lot of the products don't have images yet but they have a hard coded image url so we can add them when we get them. To get around broken images I just do a little search for the image size and if I can't find it and replace it with a placeholder.
我有一个运行 wordpress 插件 WooCommerce 的 wordpress 站点。由于本网站处理的产品数量庞大,我们一直在管理网站外的产品列表并将其上传。许多产品还没有图像,但它们有一个硬编码的图像 url,因此我们可以在获得它们时添加它们。为了绕过损坏的图像,我只需稍微搜索图像大小,如果找不到,就用占位符替换它。
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
if (@getimagesize($src[0])) {
//display product image
} else {
//display placeholder image
}
This is working fine in most cases but now I am working on displaying the products in a category. I want to display all the products with images first and then display the products without images. The problem is once the loop starts if I exclude products without images it will loop through the first 12 products and only display a subset of the 12 that have images. What I want it to do is keep looping until I have 12 products with images (if there are 12 products with images).
这在大多数情况下工作正常,但现在我正在努力显示类别中的产品。我想先显示所有有图像的产品,然后再显示没有图像的产品。问题是一旦循环开始,如果我排除没有图像的产品,它将循环遍历前 12 个产品,并且只显示 12 个有图像的子集。我想要它做的是继续循环,直到我有 12 个带有图像的产品(如果有 12 个带有图像的产品)。
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 have been unable to code would be to ignore some product while in the loop (so it would make another run if there is no image) or somehow code my query as part of the requirement of the loop i.e. pit it in the $args?
我无法编码的可能的逻辑解决方案是在循环中忽略某些产品(因此如果没有图像,它将再次运行)或以某种方式将我的查询编码为循环要求的一部分,即将其放入$args?
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Jeffrey Jenkinson
I have managed to find a workable solution to my problem. It was just impossible to list the products through separate loops without making a mess of pagination. So the logical step was to use the loop and essentially order the products based on whether or not the image exists. This creates a new problem because the Wordpress ordering cannot determine whether the image link points to a file or not.
我已经设法为我的问题找到了可行的解决方案。通过单独的循环列出产品而不会使分页混乱是不可能的。所以合乎逻辑的步骤是使用循环并基本上根据图像是否存在对产品进行排序。这会产生一个新问题,因为 Wordpress 排序无法确定图像链接是否指向文件。
You can however set a "Menu order" for a product in woocommerce. Then if you set "Default product sorting" to "Default sorting" under "Woocommerce -> Settings -> Catalog" it will use this menu order to order the products in a catalog view.
但是,您可以在 woocommerce 中为产品设置“菜单顺序”。然后,如果您在“Woocommerce -> 设置 -> 目录”下将“默认产品排序”设置为“默认排序”,它将使用此菜单顺序在目录视图中对产品进行排序。
Great! But I still have 17000 products and I need to specify a Menu order for each. There was no way I could do this using the native woocommerce tools it would have taken weeks. So I decided to write a little plugin to change the "Menu order" of each product based on whether the image exists or not.
伟大的!但是我仍然有 17000 个产品,我需要为每个产品指定一个菜单顺序。使用本机 woocommerce 工具,我无法做到这一点,这需要数周时间。所以我决定写一个小插件,根据图片是否存在来改变每个产品的“菜单顺序”。
Here is the function used to write to the post database:
这是用于写入 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>';
}
I since I have so many products my plugin processes them in smaller batches. I am managing a batch of around 2000 products at a time without failing. I did have to adjust my php memory limit in config.php
因为我有这么多产品,所以我的插件会以较小的批次处理它们。我一次管理一批大约 2000 种产品而没有失败。我确实必须在 config.php 中调整我的 php 内存限制
define('WP_MAX_MEMORY_LIMIT', '256M');
I still wonder if there might be an easier way of accomplishing this but for the time being this solution will suffice.
我仍然想知道是否有更简单的方法来实现这一点,但目前这个解决方案就足够了。

