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.