【问题标题】:Get all shipping classes in a shipping method init - Woocommerce在运输方法初始化中获取所有运输类别 - Woocommerce
【发布时间】:2018-04-17 04:46:35
【问题描述】:

我按照 Woocommerce Shipping Method API 创建了一种自定义运输方式。在我的运输方法类的init 方法中,我试图使用WC()->shipping->get_shipping_classes() 获取所有运输类。

此调用因 PHP 致命错误而失败:
致命错误:未捕获的错误:在 null 上调用成员函数 get_shipping_classes()...
这表明WC()->shippingnull,它基本上是WC_Shipping 类的一个实例。

我正在做类似于 Woocommerce 核心的统一费率运输方法。如here 所示,类似的代码在 Woocommerce 中有效。
这是我的送货方式类:

class WCS_City_Shipping_Method extends WC_Shipping_Flat_Rate {

    /**
     * Cities applicable on
     * 
     * @var array
     */
    public $cities = array();

    /**
     * Constructor.
     *
     * @since 1.0.0
     */
    public function __construct( $instance_id = 0 ) {

        $this->id                 = 'city_shipping';
        $this->instance_id        = absint( $instance_id );
        $this->method_title       = __( 'Flat Rate City Shipping', 'woocommerce-city-shipping' );
        $this->method_description = __( 'Applies only when shipping city matches one of provided.', 'woocommerce-city-shipping' );
        $this->supports           = array( 'shipping-zones', 'instance-settings', );

        $this->init();

        // Save settings
        add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
    }


    /**
     * Init.
     *
     * Initialize user set variables.
     *
     * @since 1.0.0
     */
    public function init() {
        $this->instance_form_fields = include( 'settings-city-shipping.php' );
        $this->title                = $this->get_option( 'title' );
        $this->tax_status           = $this->get_option( 'tax_status' );
        $this->cities               = $this->get_option( 'cities' );
        $this->cost                 = $this->get_option( 'cost' );
        $this->type                 = $this->get_option( 'type', 'class' );
    }

    /**
     * ... Rest of code
     * 
     */
}

这里是settings-city-shipping.php,它包含在init 方法中。

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$shipping_classes = WC()->shipping->get_shipping_classes(); // Fatal error here 

使用过滤器添加运输方式为:

// Add shipping method
add_filter( 'woocommerce_shipping_methods', array( $this, 'add_shipping_method_class' ) );
public function add_shipping_method_class( $methods ) {

    if ( class_exists( 'WCS_City_Shipping_Method' ) ) {
        $methods['city_shipping'] = 'WCS_City_Shipping_Method';
    }
    return $methods;

}

请帮助找出导致致命错误的原因以及如何获取所有运输类别。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    这是一个愚蠢的错误。我已经在这个自定义运输方法的插件类的初始化代码中初始化了WCS_City_Shipping_Method 类。代码在Woocommerce 准备好之前运行,因此导致致命错误。

    我错过了来自 Woocommerce Shipping API documentation 的这一重要行:

    为了确保你需要扩展的类存在,你应该把你的 加载所有插件后调用的函数中的类。

    无论如何,我通过不初始化类并将其包装到在plugins_loaded 动作挂钩上运行的方法来解决问题。以下是更改:

    public function plugins_loaded_action() {
        // Load shipping method class
        add_action( 'woocommerce_shipping_init', array( $this, 'wcs_shipping_method' ) );
    }
    
    // Placed in plugin class init method
    add_action( 'plugins_loaded', array( $this, 'plugins_loaded_action' ) );
    
    public function wcs_shipping_method() {
        require_once plugin_dir_path( __FILE__ ) . 'includes/class-wcs-method.php';
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2018-11-28
      • 2014-07-05
      相关资源
      最近更新 更多