How to add custom shipping method in woocommerce.

1.upload this file in plugins and activate it from dashboard you can change name of shipping method according to you. 

1.upload this file in plugins and activate it from dashboard you can change name of shipping method according to you.
<?php

/*
Plugin Name:Call for quata
Description: Call for quata shipping method plugin
Author:Arunendra Pratap Rai
*/
/**
* Check if WooCommerce is active
*/

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

function your_shipping_method_init() {

if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {

class WC_Your_Shipping_Method extends WC_Shipping_Method {

/**

* Constructor for your shipping class

*

* @access public

* @return void

*/

public function __construct() {

$this->id = 'call-for-quata'; // Id for your shipping method. Should be uunique.

$this->method_title = __( 'CALL FOR QUOTE' ); // Title shown in admin

$this->method_description = __( 'Description of CALL FOR QUOTE' ); // Description shown in admin

$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled

$this->title = "CALL FOR QUOTE"; // This can be added as an setting but for this example its forced.

 //$this->init_form_fields();

$this->init();

}

/**

* Init your settings

*

* @access public

* @return void

*/   

function init() {
// Load the settings API
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
 $this->cost  = $this->get_option( 'cost' );
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
 * Initialise Gateway Settings Form Fields
 */
 function init_form_fields() {
     $this->form_fields = array(
'enabled' => array(
        'title'         => __( 'Enable/Disable', 'woocommerce' ),
          'type'          => 'checkbox',
       'label'         => __( 'Enable CALL FOR QUOTE', 'woocommerce' ),
          'default'       => 'yes'
                        ),
     'title' => array(
          'title' => __( 'Title', 'woocommerce' ),
          'type' => 'text',
          'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
          'default' => __( 'CALL FOR QUOTE', 'woocommerce' )
          ),
     'description' => array(
          'title' => __( 'Description', 'woocommerce' ),
          'type' => 'textarea',
          'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ),
          'default' => __("CALL FOR QUOTE Description", 'woocommerce')
           ),
'cost' => array(
                    'title'         => __( 'Default Cost', 'woocommerce' ),
'type'          => 'number',
'custom_attributes' => array(
'step'  => 'any',
'min'   => '0'
),
'description'   => __( 'Cost excluding tax. Enter an amount, e.g. 2.50.', 'woocommerce' ),
            'default'       => '',
          'desc_tip'      => true,
           'placeholder'   => '0.00'
                      )
     );
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 'Call For Detail Coast',
'calc_tax' => 'per_item'
);

// Register the rate
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );
function add_your_shipping_method( $methods ) {
$methods[] = 'WC_Your_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}
?>

No comments:

Post a Comment