简体   繁体   English

对于没有托管库存的 WooCommerce 变体显示“有货”通知

[英]Display “In Stock” notice for WooCommerce variations with no Managed Stock

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]在 WooCommerce 中,如果为简单的产品或变体启用了“管理库存”,则会在产品页面中显示通知 => 例如 [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.对于可变产品,仅在选择特定变体后才应显示此消息。

I've made a video capture to be a bit more illustrative: https://sgevcen.tinytake.com/tt/NDQzNTU2OF8xNDAyNTU2NA我制作了一个视频捕捉更能说明问题: 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;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 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;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。


Related thread: Display custom stock message if "Manage Stock" is not enabled in WooCommerce相关主题: 如果 WooCommerce 中未启用“管理库存”,则显示自定义库存消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 woocommerce-显示库存变化 - woocommerce - display variations in stock 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 显示 现货 WooCommerce 单品简短描述 - Display In stock available variations in WooCommerce single product short description 基于 Woocommerce 产品变体库存数据复选框选项的自定义显示 - Custom display based on Woocommerce product variations stock data checkbox option 在 wp-admin 列中显示每个 woocommerce 变体库存数量 - Display each woocommerce variations stock quantity in wp-admin column 如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮 - Display a Sold out greyed button if all variations are out of stock in WooCommerce 检查 WooCommerce 产品(简单或变体)是否有库存并将标签显示为短代码 - Check if WooCommerce product (simple or variations) are in stock and display label as shortcode woocommerce 产品的低库存通知 - Low Stock notice for woocommerce product WooCommerce 产品变化:自动启用管理库存和设置库存数量 - WooCommerce product variations: Auto enable Manage Stock and set stock quantity 如果 WooCommerce 中的一个变体缺货,则将所有变体设置为缺货 - Set all variations out of stock if one variation is out of stock in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM