从 WooCommerce 目录中隐藏具有特定库存状态的所有产品 [英] Hide all products with a specific stock status from WooCommerce catalog
问题描述
我在 Wordpress 和 Woocommerce 网站上使用 Flatsome 模板.我还创建了自定义库存状态(例如 noproduzione
,当制造商不再创建产品时使用).但我不想在我的网站上(仅在管理页面中)显示此产品(noproduzione
库存状态).
I'm using Flatsome template on Wordpress and Woocommerce site. I also create custom stock status (example noproduzione
, used when a product is not created anymore from manufacturer). But I don't want to show this products (noproduzione
stock status) on my site (only in admin pages).
我正在使用我在这里写的这段代码(我把它放在我的functions.php文件中),但似乎在flatsome上不起作用.如何应用到这个模板上?
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. 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 );
}
为了创建自定义代码,我使用了 StackOverflow 上的一些代码
To create custom code I used some of codes found on 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;
}
我添加了代码来显示可用性文本,如果产品是 noproduzione
库存状态,还隐藏购物车按钮
I added code to show the text of availability, and also to hide the cart button if the product is noproduzione
stock status
// 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;
}
最后,我还添加了此代码,这很有用,因为可以通过instock"订购相关产品;状态,对我来说很好,因为不显示其他自定义库存状态.但我想将其应用到我所有的前端网站.
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. 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;
}
那么,问题是:如何在我的网站上隐藏此产品(noproduzione 库存状态)(并且仅显示在管理页面中)?Noproduzione 与 OUTOFSTOCK 不同!
So, the question is: how to hide this products (noproduzione stock status) on my site (and show only in admin pages)? Noproduzione is different from OUTOFSTOCK!
推荐答案
您应该更好地使用专用的 woocommerce_product_query_meta_query
过滤器钩子,如下所示,从您的商店中隐藏所有具有特定库存状态的产品(适用于自定义库存状态):
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):
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;
}
代码位于活动子主题(或活动主题)的functions.php 文件中.自 WooCommerce 3 以来的所有版本都经过测试并适用.
Code goes in functions.php file of the active child theme (or active theme). Tested and works on all versions since WooCommerce 3.
这篇关于从 WooCommerce 目录中隐藏具有特定库存状态的所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!