[英]Hide “out of stock” products with custom meta data In Woocommerce
我目前正在 WooCommerce 网上商店工作,我添加了一个名为external_stock
的自定义元字段,其中 WP All Import 每 3 小时导入一次我们所有产品的供应商提供的库存。 我们实际商店中的产品数量正在输入正常库存字段中。
我想要实现的是,正常库存和external_stock
均为 0 的产品不会显示在网上商店中。
我已经编辑了一个插件,当我们的库存为 0 但外部库存 > 0 时,产品页面显示“在 x 天内可用”,当两个库存都为 0 时,它将显示“缺货”,但客户仍然可以订购“缺货”产品,这就是我想隐藏它们的原因。
Woocommerce 3 的更新
从 Woocommerce 3 开始,产品库存状态不再设置为产品元数据。
它现在由product_visibility
自定义分类在outofstock
term 下outofstock
。
因此,您需要使用 Tax 查询来隐藏缺货产品:
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 文件中。 测试和工作。
您可以在 if 语句中使用任何WooCommerce 条件标签来定位特定产品类别或产品标签存档页面。
对于包含特定元数据的产品,您将使用:
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 文件或任何插件文件中。
代码经过测试并有效。
它将从商店和档案页面中删除所有“缺货”产品。 但它不会隐藏可变产品的单个产品页面中的“缺货”变化。
对于您的自定义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 );
}
这是未经测试的,需要您进行设置和测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.