digitalhydra
9/13/2016 - 4:28 PM

Example instance based shipping method

Example instance based shipping method

<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Sample instance based method.
 */
class WC_Shipping_Test_Method extends WC_Shipping_Method {

	/**
	 * Constructor. The instance ID is passed to this.
	 */
	public function __construct( $instance_id = 0 ) {
		$this->id                    = 'test_method';
		$this->instance_id 			     = absint( $instance_id );
		$this->method_title          = __( 'Test Method' );
		$this->method_description    = __( 'Some shipping method.' );
		$this->supports              = array(
			'shipping-zones',
			'instance-settings',
		);
	    	$this->instance_form_fields = array(
        		'enabled' => array(
        			'title' 		=> __( 'Enable/Disable' ),
        			'type' 			=> 'checkbox',
        			'label' 		=> __( 'Enable this shipping method' ),
        			'default' 		=> 'yes',
        		),
        		'title' => array(
        			'title' 		=> __( 'Method Title' ),
        			'type' 			=> 'text',
        			'description' 	=> __( 'This controls the title which the user sees during checkout.' ),
        			'default'		=> __( 'Test Method' ),
        			'desc_tip'		=> true
        		)
		);
		$this->enabled		    = $this->get_option( 'enabled' );
		$this->title                = $this->get_option( 'title' );

		add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
	}

	/**
	 * calculate_shipping function.
	 * @param array $package (default: array())
	 */
	public function calculate_shipping( $package = array() ) {
		$this->add_rate( array(
			'id'    => $this->id . $this->instance_id,
			'label' => $this->title,
			'cost'  => 100,
		) );
	}
}