Reputation: 135
I am trying to create a plugin for woocommerce and I need to create a new shipping method as part of it. I found this site and follow it's instruction. I have just changed some names.
Here is my code:
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' );
}
Now on WooCommerce > Settings > Shipping I see my method name, but when I click it I see an empty form with "save changes" button. I have check the code several times, but I didn't find what is wrong.
second problem: As you can see, this code is for a complete plugin. I want to make it as a part of another plugin. What is the correct way for that?
update:
I found an answer for first problem: The class id must not include capital letters.
Upvotes: 1
Views: 375
Reputation: 253784
There is some errors and mistakes in your code… Also you should try to avoid capitals on function names, variable names and slug names in php (different than in Javascript or other script languages).
Try the following instead:
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;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. Tested and works.
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)*
Related: Create a new shipping method in woocommerce 3
Upvotes: 1