【问题标题】:Uncaught Error: Call to a member function get_shipping_packages()未捕获的错误:调用成员函数 get_shipping_packages()
【发布时间】:2021-05-07 11:27:13
【问题描述】:

我正在尝试获取用户的当前送货区域,但每次尝试获取时都会出错

// The package. 

// Get cart shipping packages
$shipping_packages = $woocommerce->cart->get_shipping_packages();

// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = $woocommerce->wc_get_shipping_zone( reset( $shipping_packages ) );

$zone_id   = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name

// Testing output
echo '<p>Zone id: ' . $zone_id . ' | Zone name: ' . $zone_name . '</p>';

错误信息

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce woocommerce-bookings


    【解决方案1】:

    有两个问题:

    • $woocommerce 全局变量未定义(可以替换为WC()),
    • wc_get_shipping_zone() 不是 Woocommerce 方法,而是一个函数。

    改用以下方法(插件见下文):

    // Get cart shipping packages
    $shipping_packages = WC()->cart->get_shipping_packages();
    
    // Get the WC_Shipping_Zones instance object for the first package
    $shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
    
    $zone_id       = $shipping_zone->get_id(); // Get the zone ID
    $zone_name     = $shipping_zone->get_zone_name(); // Get the zone name
    
    // Testing output
    echo '<p>Zone id: ' . $zone_id . ' | Zone name: ' . $zone_name . '</p>';
    

    它应该可以工作


    插件,试试

    global $woocommerce;
    
    // Get cart shipping packages
    $shipping_packages = $woocommerce->cart->get_shipping_packages();
    
    // Get the WC_Shipping_Zones instance object for the first package
    $shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
    
    $zone_id       = $shipping_zone->get_id(); // Get the zone ID
    $zone_name     = $shipping_zone->get_zone_name(); // Get the zone name
    

    【讨论】:

    • 由于文件 wp-content/plugins/risk-free-cash-on-delivery-cod-woocommerce/risk_free_advanced_cod.php 的第 134 行出现错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。未捕获的错误:调用 wp-content/plugins/risk-free-cash-on-delivery-cod-woocommerce/risk_free_advanced_cod.php:134 中未定义的函数 WC() 堆栈跟踪:#0 wp-settings.php(388): include_once() ...
    • @Md.SafiqulIslam 我已经更新了我的答案……这似乎是一个过时插件的问题……
    • 他们俩都在抛出错误。我还尝试降级 woo-commerce 插件。我应该检查什么来解决问题
    • @Md.SafiqulIslam 问题似乎来自risk-free-cash-on-delivery-cod-woocommerce 无法访问 Woocommerce 全局变量的插件......它应该在 woocommerce 之后加载,检查之前是否加载了 woocommerce。不要降级 WooCommerce。
    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多