问题描述
我需要特殊情况的帮助.在WooCommerce中,如果管理库存"为简单的产品或变体启用,则在产品页面中显示通知=>例如[此示例] [1]
I need help for a particular situation. In WooCommerce, if "Manage Stock" is enabled for a simple product or variation, then a notification is being displayed in the product page => such as [this example][1]
但是,如果管理库存"未启用,则没有通知,我感到可惜,因为即使我不管理库存数量,我仍想通知客户该库存正好在库存中.
However, if "Manage Stock" is not enabled, then there is no notification which I find it a pity because I still want to inform my customers that it's precisely in stock even if I don't manage the stock quantities.
我找到了以下代码.对于简单的产品,它可以正常工作.但是,对于可变产品,甚至在选择之前,都会显示此消息.这当然不行,只有选择了变体之后,才应显示此代码.
I've found the below code. For simple products, it works without any problem. However, for variable product, this message is being displayed even before that a variation is selected. This is of course not okay, this code should be displayed only after that a variation is selected.
有人可以帮我解决这个问题吗?对于可变产品,仅在选择特定版本后的 中显示此消息.
Can someone help me to fix this? For variable products, this message should only be displayed after that a particular variation is selected.
我进行了一段视频拍摄,使其更具说明性: https://sgevcen.tinytake .com/tt/NDQzNTU2OF8xNDAyNTU2NA
I've made a video capture to be a bit more illustrative : https://sgevcen.tinytake.com/tt/NDQzNTU2OF8xNDAyNTU2NA
function mycustom_shop_display_stock() {
global $product;
if ( !$product->get_manage_stock() && $product->is_in_stock() ) {
echo '<p class="stock in-stock">In Stock</p>';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'mycustom_shop_display_stock', 11 );
[1]: https://i.stack.imgur.com/aFnN1.png
推荐答案
请尝试以下操作,该操作应仅显示可变产品(以及简单产品)的自定义库存可用性:
Try the following instead that should allow to display your custom stock availability only for the variations of a variable product (and also on simple products):
add_filter( 'woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 );
function filter_wc_get_stock_html( $html, $product ) {
if ( ! $product->is_type('variable') && ! $product->get_manage_stock() && $product->is_in_stock() ) {
$html = '<p class="stock in-stock">' . __( "In Stock", "woocommerce" ) . '</p>';
}
return $html;
}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
要排除某些产品类别,请使用以下内容(与您的评论有关):
To exclude some product categories use the following (related to your comment):
add_filter( 'woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 );
function filter_wc_get_stock_html( $html, $product ) {
// Here define the product categories to be excluded (can be term Ids, slugs or names)
$terms_excl = array('hoodies', 'albums');
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if ( ! $product->is_type('variable') && ! $product->get_manage_stock() && $product->is_in_stock()
&& ! has_term( $terms_excl, 'product_cat', $product_id ) ) {
$html = '<p class="stock in-stock">' . __( "In Stock", "woocommerce" ) . '</p>';
}
return $html;
}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
相关主题:显示自定义如果管理库存",则显示库存消息没有在WooCommerce中启用
这篇关于显示“有货"没有托管库存的WooCommerce变化的公告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!