简体   繁体   English

在WooCommerce商店页面中隐藏无缩略图的产品

[英]Hiding products without thumbnail in WooCommerce shop page

I use importer which imports thousands of products to the shop. 我使用进口商将数千种产品进口到商店。 Although I must insert picture and description for the product before I want to sell the item. 虽然我必须在出售产品之前插入产品的图片和说明。

I would like to hide product from the store at all IF there is no thumbnail image assigned. 如果没有分配缩略图,我想将所有产品都隐藏在商店中。 This way new products appear to the shop only after I have set the thumbnail image. 这样,只有在设置缩略图后,新产品才会出现在商店中。

I tried this in header.php, but did not work: 我在header.php中尝试了此操作,但没有成功:

 <?php if($_product->getImage() && $_product->getImage() != 'no_selection'){
?>
 <style>
  /* Css to hide Featured image Div */
 </style>
<?php
}?>

This gave me error: 这给了我错误:

Fatal error: Call to a member function getImage() on a non-object 致命错误:在非对象上调用成员函数getImage()

Is there any quick way to hide WordPress WooCommerce products from the Shop page, if they do not have a thumbnail picture placed? 如果没有放置缩略图的图片,是否有任何快速方法可从“商店”页面隐藏WordPress WooCommerce产品?

Thanks 谢谢

You can use a custom function hooked in woocommerce_product_query action, hook that will change the shop query and will not display on shop page products without an image, this way: 您可以使用挂在woocommerce_product_query操作中的自定义函数,该钩子将更改商店查询,并且将在没有图像的情况下不显示在商店页面产品上,这种方式是:

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $query ) {

    $query->set( 'meta_query', array( array(
       'key' => '_thumbnail_id',
       'value' => '0',
       'compare' => '>'
    )));

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This code is tested and works on WooCommerce 2.6.x and 3.0+ 此代码已经过测试,可在WooCommerce 2.6.x和3.0+上运行

Woo-commerce usually inserts a placeholder picture on products which don't have a thumbnail picture. Woo-commerce通常会在没有缩略图的产品上插入占位符图片。 What you can do is target products which don't have a thumbnail using Jquery easily. 您可以做的是使用Jquery轻松创建没有缩略图的目标产品。

on my website it puts an imame named "placeholder.png" which will be in site_url()."/wp-content/plugins/woocommerce/assets/images/placeholder.png", site url which is your website's url. 在我的网站上,它会放置一个名为“ placeholder.png”的名称,该名称将位于site_url()中。” / wp-content / plugins / woocommerce / assets / images / placeholder.png”,即网站的网址。

enqueue a javascript to your theme or create a child theme and add it in the functions.php file. 将javascript放入主题中或创建子主题,然后将其添加到functions.php文件中。

here goes the code. 代码在这里。

hideproduct.js hideproduct.js

jQuery(document).ready(function(){

    jQuery(".products a img.woocommerce-
            placeholder").parents(".product").css("display","none");

})

Well, you could save yourself a lot of struggle and import your products initially with the post_status set to draft , which will result in not showing them on the front page. 好吧,您可以省去很多post_status ,并在将post_status设置为draft最初导入产品,这将导致它们不会显示在首页上。 Then, every time you add an image to your product, which I assume you do via the WP backend, you "publish" your product, since you have to save it anyway. 然后,每次您将图像添加到产品中时(我假设您是通过WP后端完成的),您都将“发布”产品,因为无论如何都必须保存它。

If you have a separate import-script for images, you can set the post_status to public from there. 如果您有单独的图像导入脚本,则可以从此处将post_status设置为public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM