/
custom-shipping.php
139 lines (109 loc) · 3.54 KB
/
custom-shipping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @wordpress-plugin
* Plugin Name: Custom shipping
* Plugin URI:
* Description:
* Version: 1.0
* Author:
* Author URI:
* Text Domain: custom-shipping
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
function woocommerce_custom_shipping_init(){
if ( !class_exists( 'WC_Shipping_Method' ) ) {
return;
}
if ( ! class_exists( 'WC_Custom_Shipping_Method' ) ) {
class WC_Custom_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'custom';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Custom shipping', 'custom-shipping' );
$this->method_description = __( 'Custom shipping for WooCommerce', 'custom-shipping' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->init_form_fields();
$this->enabled = "yes";
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->availability = $this->get_option( 'availability' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Init form fields
*
* @access public
*/
public function init_form_fields() {
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'custom-shipping' ),
'type' => 'checkbox',
'label' => __( 'Enable custom shippng', 'custom-shipping' ),
'default' => 'yes',
),
'title' => array(
'title' => __( 'Title', 'custom-shipping' ),
'type' => 'text',
'description' => __( 'Method title', 'custom-shipping' ),
'default' => __( 'Custom shipping', 'custom-shipping' ),
),
'description' => array(
'title' => __( 'Description', 'custom-shipping' ),
'type' => 'text',
'description' => __( 'Shipping description', 'custom-shipping' ),
'default' => __( 'Custom shipping for WooCommerce', 'custom-shipping' ),
),
'cost' => array(
'title' => __( 'Price', 'custom-shipping' ),
'type' => 'text'
),
);
}
/**
* Calculate_shipping function.
*
* @access public
*/
public function calculate_shipping( $package = array() ) {
$cost = $this->get_option( 'cost' );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'calc_tax' => 'per_order',
'cost' => $cost
);
$this->add_rate( $rate );
}
/**
* Is available shipping
*
* @access public
*/
public function is_available( $package ) {
if ( 'no' == $this->enabled ) {
return false;
}
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package );
}
}
}
}
add_action('plugins_loaded', 'woocommerce_custom_shipping_init');
function add_woo_custom_shipping_method( $methods ) {
$methods['custom'] = 'WC_Custom_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_woo_custom_shipping_method' );