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

问题描述 投票:0回答:2

我需要针对特定情况的帮助。在 WooCommerce 中,如果为简单产品或变体启用了“管理库存”,则会在产品页面 => 中显示通知,例如 [此示例][1]

但是,如果没有启用“管理库存”,那么就没有通知,我觉得很遗憾,因为即使我不管理库存数量,我仍然想告诉我的客户,它正好有库存。

我找到了下面的代码。对于简单的产品,它的工作没有任何问题。但是,对于可变产品,甚至在选择变体“之前”也会显示此消息。这当然不行,只有在选择变体后才应显示此代码。 有人可以帮我解决这个问题吗?对于可变产品,此消息仅应在选择特定变体后

显示。

我制作了一个视频捕获来更具说明性: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.sstatic.net/aFnN1.png

请尝试以下操作,这应该允许仅显示可变产品的变体(以及简单产品)的自定义库存可用性:
php wordpress woocommerce hook-woocommerce stock
2个回答
2
投票
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 文件中。已测试并有效。

要排除某些产品类别,请使用以下内容(与您的评论相关):


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 文件中。已测试并有效。

相关主题:

如果 WooCommerce 中未启用“管理库存”,则显示自定义库存消息

我意识到这个帖子已经有 4 年历史了,但我的 Woocommerce 主题遇到了完全相同的问题。该代码也对我有用,但我注意到即使产品状态设置为缺货,它仍然显示“有库存”。有人可以帮我排除缺货产品的“有货”消息吗?既适用于简单产品又适用于可变产品?非常感谢! -安德鲁

0
投票

© www.soinside.com 2019 - 2024. All rights reserved.