Hiding products without thumbnail in WooCommerce shop page

不问归期 提交于 2021-02-19 08:13:07

问题


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:

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

Is there any quick way to hide WordPress WooCommerce products from the Shop page, if they do not have a thumbnail picture placed?

Thanks


回答1:


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:

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.

This code is tested and works on WooCommerce 2.6.x and 3.0+




回答2:


Woo-commerce usually inserts a placeholder picture on products which don't have a thumbnail picture. What you can do is target products which don't have a thumbnail using Jquery easily.

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.

enqueue a javascript to your theme or create a child theme and add it in the functions.php file.

here goes the code.

hideproduct.js

jQuery(document).ready(function(){

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

})



回答3:


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. 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.

If you have a separate import-script for images, you can set the post_status to public from there.



来源:https://stackoverflow.com/questions/44884498/hiding-products-without-thumbnail-in-woocommerce-shop-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!