[英]Display “In Stock” notice for WooCommerce variations with no Managed Stock
我需要針對特定情況的幫助。 在 WooCommerce 中,如果為簡單的產品或變體啟用了“管理庫存”,則會在產品頁面中顯示通知 => 例如 [this example][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.stack.imgur.com/aFnN1.png
請嘗試以下方法,它應該允許僅針對可變產品的變體(以及簡單產品)顯示您的自定義庫存可用性:
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 文件。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.