Make a new shipping method as a part of plugin in Woocommerce












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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.







      php wordpress plugins woocommerce shipping-method






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 5 at 0:15









      LoicTheAztec

      94.9k1370109




      94.9k1370109










      asked Jan 2 at 16:48









      hussein ghasemihussein ghasemi

      157




      157
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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) and the-php-file-name.php by the real php file name)*




          Related: Create a new shipping method in woocommerce 3






          share|improve this answer


























          • should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

            – hussein ghasemi
            Jan 5 at 11:56











          • @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

            – LoicTheAztec
            Jan 5 at 12:00













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010130%2fmake-a-new-shipping-method-as-a-part-of-plugin-in-woocommerce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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) and the-php-file-name.php by the real php file name)*




          Related: Create a new shipping method in woocommerce 3






          share|improve this answer


























          • should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

            – hussein ghasemi
            Jan 5 at 11:56











          • @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

            – LoicTheAztec
            Jan 5 at 12:00


















          1














          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) and the-php-file-name.php by the real php file name)*




          Related: Create a new shipping method in woocommerce 3






          share|improve this answer


























          • should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

            – hussein ghasemi
            Jan 5 at 11:56











          • @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

            – LoicTheAztec
            Jan 5 at 12:00
















          1












          1








          1







          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) and the-php-file-name.php by the real php file name)*




          Related: Create a new shipping method in woocommerce 3






          share|improve this answer















          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) and the-php-file-name.php by the real php file name)*




          Related: Create a new shipping method in woocommerce 3







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 5 at 0:21

























          answered Jan 5 at 0:14









          LoicTheAztecLoicTheAztec

          94.9k1370109




          94.9k1370109













          • should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

            – hussein ghasemi
            Jan 5 at 11:56











          • @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

            – LoicTheAztec
            Jan 5 at 12:00





















          • should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

            – hussein ghasemi
            Jan 5 at 11:56











          • @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

            – LoicTheAztec
            Jan 5 at 12:00



















          should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

          – hussein ghasemi
          Jan 5 at 11:56





          should i place include_ one in construct function of main plugin php or before main class or in a function within a class? what about add_filter/actions ?

          – hussein ghasemi
          Jan 5 at 11:56













          @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

          – LoicTheAztec
          Jan 5 at 12:00







          @husseinghasemi yes it should be better to place it as an action hook in your main plugin init function (or in the main plugin constructor function)… You need to try, as plugins can be made in so many different ways.

          – LoicTheAztec
          Jan 5 at 12:00






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010130%2fmake-a-new-shipping-method-as-a-part-of-plugin-in-woocommerce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory