使用WooCommerce_Get_Availability挂钩时的几个问题
我正在尝试为缺货的产品在单个产品页面上显示"缺货"消息。
我的functions.php
function stock_catalog() {
global $product;
if (number_format($product->stock, 0, '', '') <= 0) {
echo '<p class="mb-2"> ' . __('** Sorry currently out of stock **', 'text-
domain') . '</p>';
}
}
add_action('woocommerce_get_availability', 'stock_catalog');
我收到以下错误:
注意:错误地调用了股票。不应直接访问产品属性。回溯:需要(‘wp-BLOG-Header.php’),require_once(‘wp-includes/template-loader.php’),include(‘/plugins/woocommerce/templates/single-product.php’),wc_Get_TEMPLATE_PART,LOAD_TEMPLATE,require(‘/themes/mytheme/woocommerce/content-single-product.php’),do_action(‘woocommerce_single_product_summary’),WP_HOOK->;do_action,WP_Hook->;应用过滤器,WooCommerce_Template_Single_Add_to_Cart,Do_action(‘WooCommerce_Simple_Add_to_Cart’),WP_Hook-&>;do_action,WP_Hook-&>;Apply_Filters,WooCommerce_Simple_Add_to_Cart,WC_Get_Template,include(‘/plugins/woocommerce/templates/single-product/add-to-cart/simple.php’),WC_Get_stock_html,WC_Product->;Get_Availability,apply_filters(‘woocommerce_get_availability’),WP_Hook->;Apply_Filters,STOCK_CATALOG,WC_Abstract_Legacy_Product->;__Get,WC_Do_It_Error请参阅/homepages/17/d825330075/htdocs/mytheme/wp-includes/functions.php第5313行中的在WordPress中调试以了解更多信息
有什么解决此问题的方法吗?
解决方案
需要使用方法";GET_STOCK_QUANTITY&QOOT;
function stock_catalog($availability, $product) {
if ( $product->managing_stock() && $product->get_stock_quantity() <= 0) {
$availability['availability'] = __('** Sorry currently out of stock **', 'text-
domain');
}
return $availability;
}
add_filter('woocommerce_get_availability', 'stock_catalog', 10, 2);
更多详细信息https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_get_stock_quantity
相关文章