[英]Hide all products with a specific stock status from WooCommerce catalog
I'm using Flatsome template on Wordpress and Woocommerce site.我在 Wordpress 和 Woocommerce 站点上使用 Flatsome 模板。 I also create custom stock status (example
noproduzione
, used when a product is not created anymore from manufacturer).我还创建了自定义库存状态(例如
noproduzione
,当不再从制造商创建产品时使用)。 But I don't want to show this products ( noproduzione
stock status) on my site (only in admin pages).但我不想在我的网站上(仅在管理页面中)显示此产品(
noproduzione
库存状态)。
I'm using this code that I write here (I put it in my functions.php file), but it seems that on flatsome does not works.我正在使用我在这里编写的这段代码(我把它放在我的函数中。php 文件),但似乎在 flatsome 上不起作用。 How to apply it on this template?
如何在这个模板上应用它?
add_action( 'woocommerce_product_query', 'qc_action_product_query', 10, 2 );
function qc_action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional meta query
$q->set( 'meta_query', array( array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
To create custom code I used some of codes found on StackOverflow为了创建自定义代码,我使用了 StackOverflow 上的一些代码
// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['10days'] = __( 'Disponibile entro 10 giorni', 'woocommerce' );
$status['inarrivo'] = __( 'In arrivo', 'woocommerce' );
$status['noproduzione'] = __( 'Fuori produzione', 'woocommerce' );
return $status;
}
// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text( $availability, $product) {
switch( $product->get_stock_status() ) {
case '10days':
$availability = __( 'Disponibile entro 10 giorni', 'woocommerce' );
break;
case 'inarrivo':
$availability = __( 'In arrivo', 'woocommerce' );
break;
case 'noproduzione':
$availability = __( 'Fuori produzione', 'woocommerce' );
break;
}
return $availability;
}
I added code to show the text of availability, and also to hide the cart button if the product is noproduzione
stock status我添加了代码来显示可用性文本,如果产品是
noproduzione
库存状态,还隐藏购物车按钮
// Display the stock status on other page
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
function wcs_stock_text_shop_page() {
//returns an array with 2 items availability and class for CSS
global $product;
$availability = $product->get_stock_status();
//check if availability in the array = string 'noproduzione'
//if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
if ( $product->get_stock_status() === 'noproduzione') {
echo '<span style="color:#b20000;">Fuori produzione!</span>';
}
else if ( $product->get_stock_status() === 'onbackorder') {
echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
}
else if ( $product->get_stock_status() === '10days') {
echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
}
else if ( $product->get_stock_status() === 'inarrivo') {
echo '<span style="color:#e0c81d;">In arrivo</span>';
}
else if ( $product->get_stock_status() === 'outofstock') {
echo '<span style="color:#b20000;">Terminato!</span>';
}
else {
echo '<span style="color:#53af00;">Disponibile!</span>';
}
}
// Hide the cart button if stock status is 'noproduzione'
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'noproduzione' ) {
return false;
}
return $purchasable;
}
Last, I also added this code, is useful because order the RELATED PRODUCT by "instock" status, and for me is good because do not show the other custom stock status.最后,我还添加了这段代码,它很有用,因为通过“instock”状态订购相关产品,对我来说很好,因为不显示其他自定义库存状态。 But I want to apply this to all of my frontend site.
但我想将此应用于我所有的前端站点。
//show, in the related products, only instock products!
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {
foreach( $related_posts as $key => $related_post ) {
// Get product
$related_product = wc_get_product( $related_post );
// Is a WC product
if ( is_a( $related_product, 'WC_Product' ) ) {
// Stock status
$stock_status = $related_product->get_stock_status();
// NOT instock
if ( $stock_status != 'instock' ) {
unset( $related_posts[$key] );
}
}
}
return $related_posts;
}
So, the question is: how to hide this products (noproduzione stock status) on my site (and show only in admin pages)?所以,问题是:如何在我的网站上隐藏这个产品(noproduzione 库存状态)(并且只在管理页面中显示)? Noproduzione is different from OUTOFSTOCK!
Noproduzione 与 OUTOFSTOCK 不同!
You should better use dedicated woocommerce_product_query_meta_query
filter hook as follows, to hide, from your store, all products that have a specific stock status (works with custom stock status) :您应该更好地使用专用
woocommerce_product_query_meta_query
过滤器挂钩,如下所示,从您的商店中隐藏所有具有特定库存状态的产品(适用于自定义库存状态) :
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
if ( ! is_admin() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
);
}
return $meta_query;
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works on all versions since WooCommerce 3.
自 WooCommerce 3 以来的所有版本都经过测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.