首页 > 解决方案 > Woocommerce中可变产品的所有变体的总库存

问题描述

我尝试使用Get the total stock from a variable product In Woocommerce,试图使这段代码工作:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_variable_product_stock_quantity', 20 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
} 

有了这个:

add_action( 'woocommerce_single_product_summary', 'display_variable_product_stock_quantity', 15 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
}

或者是否可以在数量的默认元素中获得总库存?例如。股票将在下面突出显示:

在此处输入图像描述

标签: phpwoocommerce

解决方案


只需使用以下方法,以这种方式更改钩子(用于存档页面作为商店)

add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 20 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
} 

代码位于您的活动子主题(或活动主题)的 function.php 文件中。它应该按要求工作。


推荐阅读