forked from Frete-Click/WooCommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreteclick.php
227 lines (213 loc) · 7.28 KB
/
freteclick.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/*
Plugin Name: FreteClick
Plugin URI: https://freteclick.com.br/
Description: Cálculo do frete com o serviço da web Frete Click
Version: 1.0
Author: Guilherme Cristino
Author URI: http://twitter.com/guilhermeCDP7
License: Todos os Direitos Reservados
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$pluginDir = plugin_dir_path(__FILE__);
require_once("includes/variables.php");
require_once("includes/functions.php");
if (is_admin()){
fc_add_scripts();
}
function fc_shipping_methods() {
/*Adicionar os métidos de entrega*/
if ( ! class_exists( 'Fc_shipping_methods' ) ) {
class Fc_shipping_methods extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct($instance_id = 0) {
global $pluginId, $pluginName, $pluginDescription, $pluginCountries, $pluginSupports;
$this->instance_id = absint( $instance_id );
$this->id = $pluginId;
$this->title = $pluginName;
$this->method_description = $pluginDescription;
$this->method_title = $pluginName;
$this->availability = 'including';
$this->countries = $pluginCountries;
$this->supports = $pluginSupports;
$this->init();
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
$this->enabled = isset($this->settings['FC_IS_ACTIVE']) ? $this->settings['FC_IS_ACTIVE'] : 'yes';
$this->fc_check_settings($this->settings);
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
function init_form_fields() {
$this->instance_form_fields = array(
'FC_IS_ACTIVE' => array(
'title' => __( 'Status do Frete Click' ),
'type' => 'checkbox',
'description' => __( 'Para ativar os métodos de entrega do frete click você deve checar essa opção.' ),
'label' => 'Ativar o Frete Click como Método de Entrega?'
),
'FC_CEP_ORIGIN' => array(
'title' => __( 'CEP de Origem' ),
'type' => 'text',
'description' => __( '' ),
'class' => 'fc-input-cep cep-origin'
),
'FC_STREET_ORIGIN' => array(
'title' => __( 'Rua' ),
'type' => 'text',
'description' => __( '' ),
'class' => 'street-origin'
),
'FC_NUMBER_ORIGIN' => array(
'title' => __( 'Número' ),
'type' => 'text',
'description' => __( '' )
),
'FC_COMPLEMENT_ORIGIN' => array(
'title' => __( 'Complemento' ),
'type' => 'text',
'description' => __( '' )
),
'FC_DISTRICT_ORIGIN' => array(
'title' => __( 'Bairro' ),
'type' => 'text',
'description' => __( '' ),
'class' => 'district-origin'
),
'FC_CITY_ORIGIN' => array(
'title' => __( 'Cidade de Origem' ),
'type' => 'text',
'description' => __( '' ),
'class' => 'city-origin'
),
'FC_STATE_ORIGIN' => array(
'title' => __( 'Estado de Origem' ),
'type' => 'select',
'description' => __( '' ),
'options' => array(
'AC' => 'Acre',
'AL' => 'Alagoas',
'AP' => 'Amapá',
'AM' => 'Amazonas',
'BA' => 'Bahia',
'CE' => 'Ceará',
'DF' => 'Distrito Federal',
'ES' => 'Espírito Santo',
'GO' => 'Goiás',
'MA' => 'Maranhão',
'MT' => 'Mato Grosso',
'MS' => 'Mato Grosso do Sul',
'MG' => 'Minas Gerais',
'PA' => 'Pará',
'PB' => 'Paraíba',
'PR' => 'Paraná',
'PE' => 'Pernambuco',
'PI' => 'Piauí',
'RJ' => 'Rio de Janeiro',
'RN' => 'Rio Grande do Norte',
'RS' => 'Rio Grande do Sul',
'RO' => 'Rondônia',
'RR' => 'Roraima',
'SC' => 'Santa Catarina',
'SP' => 'São Paulo',
'SE' => 'Sergipe',
'TO' => 'Tocantins'
),
'class' => 'state-origin'
),
'FC_CONTRY_ORIGIN' => array(
'title' => __( 'Paìs de Origem' ),
'type' => 'text',
'description' => __( '' ),
'default' => 'Brasil',
'class' => 'country-origin'
)
);
}
public function is_available( $package ){
return true;
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping($package = array()){
$array_resp = fc_calculate_shipping($package);
if ($array_resp->response->data != false){
foreach ($array_resp->response->data->quote as $key => $quote){
$quote = (array) $quote;
$carrier_data = array(
'id' => $quote['quote-id'],
'label' => $quote['carrier-alias'],
'cost' => $quote['total'],
'calc_tax' => 'per_item',
'meta_data' => array(
'Código de Rastreamento' => $quote['order-id'],
'Nome da Transportadora' => $quote['carrier-name'],
'Cotação' => $quote['quote-id']
)
);
$this->add_rate( $carrier_data );
}
}
else{
error_log(json_encode($array_data));
error_log(json_encode($array_resp));
}
}
function fc_check_settings($set){
if ($set['FC_IS_ACTIVE'] != 'yes' && isset($set['FC_IS_ACTIVE'])){
add_action( 'admin_notices', 'fc_is_disabled' );
}
else if (strlen(get_option('FC_API_KEY')) <= 0){
add_action( 'admin_notices', 'fc_missing_apikey' );
}
else if (strlen($set['FC_CEP_ORIGIN']) <= 0 || strlen($set['FC_CITY_ORIGIN']) <= 0 || strlen($set['FC_STREET_ORIGIN']) <= 0 || strlen($set['FC_NUMBER_ORIGIN']) <= 0 || strlen($set['FC_STATE_ORIGIN']) <= 0 || strlen($set['FC_CONTRY_ORIGIN']) <= 0 || strlen($set['FC_DISTRICT_ORIGIN']) <= 0){
add_action( 'admin_notices', 'fc_missing_address' );
}
}
}
}
}
add_action( 'woocommerce_shipping_init', 'fc_shipping_methods' );
function add_fc_shipping_methods( $methods ) {
$methods['freteclick'] = 'Fc_shipping_methods';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_fc_shipping_methods' );
/*Hooks para status dos pedidos*/
add_action('woocommerce_order_status_changed', 'fc_pedido_alterado', 10, 3);
/* Hooks para página de configurações globais */
add_action('admin_init', 'fc_options_register_fields');
add_action('admin_menu', 'fc_options_page');
/* Hook para busca frete no carrinho */
add_action( 'woocommerce_product_meta_start', 'fc_display_product_layout', 10, 0 );
/* registrando rota rest para buscar cotações */
add_action("rest_api_init", function () {
register_rest_route("freteclick", "/get_shipping", array(
'methods' => 'POST',
'callback' => 'rest_get_shipping'
));
});
}
else {
add_action( 'admin_notices', 'fc_wc_missing_notice' );
}
?>