【问题标题】:Several issues when using woocommerce_get_availability hook使用 woocommerce_get_availability 挂钩时的几个问题
【发布时间】:2021-11-06 01:11:07
【问题描述】:

我试图在单个产品页面上为缺货的产品显示“缺货消息”。

我的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');

我收到以下错误:

注意:股票调用不正确。不应直接访问产品属性。回溯:require('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->apply_filters, 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_doing_it_wrong 请参阅 WordPress 中的调试以获取更多信息,位于 /homepages/17/d825330075/htdocs/mytheme/wp-includes/functions.php 第 5313 行

任何想法如何解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    需要使用方法“get_stock_quantity”

    
    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

    【讨论】:

    • 抱歉,woocommerce_get_availability 是过滤器挂钩,而不是操作挂钩。
    • @chappers 你能改变接受的答案吗?因为来自 7uc1f3r 的答案是最好的。技术水平很高。
    【解决方案2】:

    您的代码包含的不是 1 个而是多个错误

    • woocommerce_get_availability 不是一个动作,而是一个过滤器钩子
    • number_format() 在这里根本不适用
    • $product-&gt;stock 需要替换为 $product-&gt;get_stock_quantity()
    • managing_stock() 必须为真,否则无法设置/存储产品库存数量
    • echo 不适用于过滤器钩子,应使用 return 代替
    • 输出应分配给$availability
    • $product 作为参数传递给函数,所以不需要使用全局变量

    所以你得到:

    // Change stock text
    function filter_woocommerce_get_availability( $availability, $product ) {       
        // Managing stock is activated
        if ( $product->managing_stock() ) {
            // Stock quantity
            $stock_quantity = $product->get_stock_quantity();
            
            // Stock quantity is less than or equal to 0
            if ( $stock_quantity <= 0 ) {
                $availability['availability'] = __( 'Sorry currently out of stock', 'woocommerce' );
            }
        }
    
        return $availability;
    }
    add_filter( 'woocommerce_get_availability', 'filter_woocommerce_get_availability', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2011-09-12
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多