hide-0-stock-no-image.php
· 718 B · PHP
Исходник
add_action('woocommerce_product_query', 'hide_products_without_image_and_zero_stock');
function hide_products_without_image_and_zero_stock($q) {
$meta_query = $q->get('meta_query');
// Condition to check for products with an image
$meta_query[] = array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS' // Ensures the product has an associated image
);
// Condition to check for products with stock quantity higher than 0
$meta_query[] = array(
'key' => '_stock',
'value' => 0,
'compare' => '>' // Ensures the product stock is greater than 0
);
// Set the modified meta query back on the query object
$q->set('meta_query', $meta_query);
}
| 1 | add_action('woocommerce_product_query', 'hide_products_without_image_and_zero_stock'); |
| 2 | |
| 3 | function hide_products_without_image_and_zero_stock($q) { |
| 4 | $meta_query = $q->get('meta_query'); |
| 5 | |
| 6 | // Condition to check for products with an image |
| 7 | $meta_query[] = array( |
| 8 | 'key' => '_thumbnail_id', |
| 9 | 'compare' => 'EXISTS' // Ensures the product has an associated image |
| 10 | ); |
| 11 | |
| 12 | // Condition to check for products with stock quantity higher than 0 |
| 13 | $meta_query[] = array( |
| 14 | 'key' => '_stock', |
| 15 | 'value' => 0, |
| 16 | 'compare' => '>' // Ensures the product stock is greater than 0 |
| 17 | ); |
| 18 | |
| 19 | // Set the modified meta query back on the query object |
| 20 | $q->set('meta_query', $meta_query); |
| 21 | } |
| 22 |