在 Woocommerce 中创建一个新的运输方式作为插件的一部分
Make a new shipping method as a part of plugin in Woocommerce
我正在尝试为 woocommerce 创建一个插件,我需要创建一个新的送货方式作为其中的一部分。
我找到 this 网站并按照其说明进行操作。我刚刚更改了一些名字。
这是我的代码:
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 > 设置 > 送货上,我看到了我的方法名称,但是当我单击它时,我看到一个带有 "save changes" 按钮的空表单。我已经检查了几次代码,但我没有发现错误。
第二个问题:如您所见,这段代码是针对一个完整的插件。我想把它作为另一个插件的一部分。正确的做法是什么?
更新:
我找到了第一个问题的答案:class ID 不能包含大写字母。
您的代码中存在一些错误和错误......此外,您应该尽量避免在 php 中的函数名称、变量名称和 slug 名称中使用大写字母(不同于 Javascript 或其他脚本语言).
请尝试以下方法:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_action( 'woocommerce_shipping_init', 'exec_pishtaz_shipping_method' );
function exec_pishtaz_shipping_method(){
if ( ! class_exists('WC_Shipping_Method_Pishtaz_Post') ) {
class WC_Shipping_Method_Pishtaz_Post extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ){
$this->id = 'pishtaz';
$this->domain = 'pishtaz_post';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pishtaz post', $this->domain ) ;
$this->method_description = sprintf( __( 'sending goods with %s', $this->domain ), $this->method_title );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
//available country
//$this->availability = 'including';
//$this->countries = array('US');
//create from and settings
$this->init();
}
//init your setting
public function init(){
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option( 'enabled', $this->domain );
$this->title = $this->get_option( 'title', $this->domain );
$this->info = $this->get_option( 'info', $this->domain );
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options') );
}
public function init_form_fields(){
//our settings
$this->form_fields = array (
'title' => array(
'title' => __( 'Title' , $this->domain ),
'type' => 'text',
'description' => __( 'Title to be shown on the site', $this->domain ),
'desc_tip' => true,
'default' => __( 'pishtaz post', $this->domain )
),
'cost' => array(
'type' => 'text',
'title' => __( 'Cost', $this->domain ),
'description' => __( 'Enter a cost', $this->domain ),
'desc_tip' => true,
'default' => '',
),
);
}
public function calculate_shipping ( $packages = array() ){
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => '0',
'calc_tax' => 'per_item'
);
$this->add_rate( $rate );
}
}
}
}
add_filter( 'woocommerce_shipping_methods', 'add_pishtaz_shipping_method' );
function add_pishtaz_shipping_method( $methods ) {
$methods['pishtaz'] = 'WC_Shipping_Method_Pishtaz_Post';
return $methods;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。已测试并有效。
To make this code part of another plugin: Add this code to a separate php file, that you will include it using the following in your main plugin file:
include_once( 'subfolder/the-php-file-name.php' );
*(Where you will replace subfolder
by the correct subfolder name (if it exist) and the-php-file-name.php
by the real php file name)*
相关:
我正在尝试为 woocommerce 创建一个插件,我需要创建一个新的送货方式作为其中的一部分。 我找到 this 网站并按照其说明进行操作。我刚刚更改了一些名字。
这是我的代码:
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 > 设置 > 送货上,我看到了我的方法名称,但是当我单击它时,我看到一个带有 "save changes" 按钮的空表单。我已经检查了几次代码,但我没有发现错误。
第二个问题:如您所见,这段代码是针对一个完整的插件。我想把它作为另一个插件的一部分。正确的做法是什么?
更新:
我找到了第一个问题的答案:class ID 不能包含大写字母。
您的代码中存在一些错误和错误......此外,您应该尽量避免在 php 中的函数名称、变量名称和 slug 名称中使用大写字母(不同于 Javascript 或其他脚本语言).
请尝试以下方法:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_action( 'woocommerce_shipping_init', 'exec_pishtaz_shipping_method' );
function exec_pishtaz_shipping_method(){
if ( ! class_exists('WC_Shipping_Method_Pishtaz_Post') ) {
class WC_Shipping_Method_Pishtaz_Post extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ){
$this->id = 'pishtaz';
$this->domain = 'pishtaz_post';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pishtaz post', $this->domain ) ;
$this->method_description = sprintf( __( 'sending goods with %s', $this->domain ), $this->method_title );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
//available country
//$this->availability = 'including';
//$this->countries = array('US');
//create from and settings
$this->init();
}
//init your setting
public function init(){
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option( 'enabled', $this->domain );
$this->title = $this->get_option( 'title', $this->domain );
$this->info = $this->get_option( 'info', $this->domain );
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options') );
}
public function init_form_fields(){
//our settings
$this->form_fields = array (
'title' => array(
'title' => __( 'Title' , $this->domain ),
'type' => 'text',
'description' => __( 'Title to be shown on the site', $this->domain ),
'desc_tip' => true,
'default' => __( 'pishtaz post', $this->domain )
),
'cost' => array(
'type' => 'text',
'title' => __( 'Cost', $this->domain ),
'description' => __( 'Enter a cost', $this->domain ),
'desc_tip' => true,
'default' => '',
),
);
}
public function calculate_shipping ( $packages = array() ){
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => '0',
'calc_tax' => 'per_item'
);
$this->add_rate( $rate );
}
}
}
}
add_filter( 'woocommerce_shipping_methods', 'add_pishtaz_shipping_method' );
function add_pishtaz_shipping_method( $methods ) {
$methods['pishtaz'] = 'WC_Shipping_Method_Pishtaz_Post';
return $methods;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。已测试并有效。
To make this code part of another plugin: Add this code to a separate php file, that you will include it using the following in your main plugin file:
include_once( 'subfolder/the-php-file-name.php' );
*(Where you will replace
subfolder
by the correct subfolder name (if it exist) andthe-php-file-name.php
by the real php file name)*
相关: