我正在尝试为 woocommerce 创建一个插件,我需要创建一个新的运输方式作为其中的一部分。我找到了这个网站并按照它的说明进行操作。我刚刚更改了一些名称。
这是我的代码:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function hGhPishtazShippingMethod(){
if (!class_exists('hGhPishtazShippingMethodClass')){
class hGhPishtazShippingMethodClass extends WC_Shipping_Method {
function __construct(){
$this->id = 'hGhPishtazShiping';
$this->method_title = __( 'pishtaz post' ) ;
$this->method_description = __( 'sending goods with pishtaz post' );
// Available country
//$this->availability = 'including';
//$this->countries = array('US');
// Create from and settings
$this->init();
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
$this->title = isset ($this->settings['title']) ? $this->settings['title'] : __( 'pishtaz post' );
}
// Init your setting
function init(){
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
function init_form_fields(){
// Our settings
$this->form_fields = array (
'enabled' => array(
'title' => __( 'Enable' , 'woocommerce' ),
'type' => 'checkbox',
'description' =>__( 'enable pishtaz post' , 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title' , 'woocommerce' ),
'type' => 'text',
'description' => __( 'Title to be shown on the site', 'woocommerce' ),
'default' => __( 'pishtaz post', 'woocommerce' )
)
);
}
function calculate_shipping ($package){
// Our calculation
}
}
}
}
add_action( 'woocommerce_shipping_init', 'hGhPishtazShippingMethod' );
function add_hGhPishtazShippingMethod( $methods ) {
$methods[] = 'hGhPishtazShippingMethodClass';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_hGhPishtazShippingMethod' );
}
现在在 WooCommerce > Settings > Shipping 我看到我的方法名称,但是当我点击它时,我看到一个带有“保存更改”按钮的空表单。我已经检查了几次代码,但我没有发现有什么问题。
第二个问题:正如你所看到的,这段代码是一个完整的插件。我想把它作为另一个插件的一部分。正确的方法是什么?
更新:
我找到了第一个问题的答案:类 id 不能包含大写字母。