在 Woocommerce 中隐藏 "out of stock" 个具有自定义元数据的产品
Hide "out of stock" products with custom meta data In Woocommerce
我目前正在 WooCommerce 网上商店工作,我添加了一个名为 external_stock
的自定义元字段,其中 WP All Import 导入我们供应商提供的所有产品的库存3小时。我们在实际商店中获得的产品数量正在输入正常库存字段。
我想要实现的是,正常库存和 external_stock
均为 0 的产品不会在网上商店中显示。
我已经编辑了一个插件,只要我们的库存为 0 但外部库存 > 0,产品页面就会显示 'Available within x days',当两个库存都为 0 时,它将显示 'Out Of Stock',但客户仍然可以订购 'Out Of Stock' 产品,这就是我想隐藏它们的原因。
Update for Woocommerce 3
自 Woocommerce 3 起,产品 库存状态 不再设置为产品元数据。
它现在 由 product_visibility
自定义分类 在 outofstock
术语 下处理。
因此您需要改用税务查询来隐藏缺货产品:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
You can use any WooCommerce conditional tag in an if statement to target for example specific product category or product tag archive pages.
对于包含特定元数据的产品,您将使用:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Define an additional meta query
$meta_query = array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
原回答:
您可以尝试在 woocommerce_product_query
操作挂钩中挂接此自定义函数:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function 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' => 'outofstock',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并且可以工作。
It will remove all "out of stock" products from shop and archives pages. But it will not hide "out of stock" variations in single product pages for variable products.
对于您的自定义 meta_key
external_stock
,您必须这样添加:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
$meta_query = array(
'relation' => 'AND', // can be also 'OR'
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
),
array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
这是未经测试的,需要您设置和测试
官方文档:WordPress Class Reference WP_Query - Custom Field Parameters
我目前正在 WooCommerce 网上商店工作,我添加了一个名为 external_stock
的自定义元字段,其中 WP All Import 导入我们供应商提供的所有产品的库存3小时。我们在实际商店中获得的产品数量正在输入正常库存字段。
我想要实现的是,正常库存和 external_stock
均为 0 的产品不会在网上商店中显示。
我已经编辑了一个插件,只要我们的库存为 0 但外部库存 > 0,产品页面就会显示 'Available within x days',当两个库存都为 0 时,它将显示 'Out Of Stock',但客户仍然可以订购 'Out Of Stock' 产品,这就是我想隐藏它们的原因。
Update for Woocommerce 3
自 Woocommerce 3 起,产品 库存状态 不再设置为产品元数据。
它现在 由 product_visibility
自定义分类 在 outofstock
术语 下处理。
因此您需要改用税务查询来隐藏缺货产品:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
You can use any WooCommerce conditional tag in an if statement to target for example specific product category or product tag archive pages.
对于包含特定元数据的产品,您将使用:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Define an additional meta query
$meta_query = array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
原回答:
您可以尝试在 woocommerce_product_query
操作挂钩中挂接此自定义函数:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function 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' => 'outofstock',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并且可以工作。
It will remove all "out of stock" products from shop and archives pages. But it will not hide "out of stock" variations in single product pages for variable products.
对于您的自定义 meta_key
external_stock
,您必须这样添加:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
$meta_query = array(
'relation' => 'AND', // can be also 'OR'
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
),
array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
这是未经测试的,需要您设置和测试
官方文档:WordPress Class Reference WP_Query - Custom Field Parameters