| 1 |
<?php |
| 2 |
|
| 3 |
namespace SuperFrete_API\Shipping; |
| 4 |
|
| 5 |
use SuperFrete_API\Http\Request; |
| 6 |
use SuperFrete_API\Helpers\Logger; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) |
| 9 |
exit; // Segurança |
| 10 |
|
| 11 |
class SuperFreteShipping extends \WC_Shipping_Method { |
| 12 |
|
| 13 |
public function __construct($instance_id = 0) { |
| 14 |
$this->id = 'superfrete'; |
| 15 |
$this->instance_id = absint($instance_id); |
| 16 |
$this->method_title = __('SuperFrete', 'superfrete'); |
| 17 |
$this->method_description = __('Método de envio via SuperFrete API.', 'superfrete'); |
| 18 |
$this->supports = ['shipping-zones', 'instance-settings']; |
| 19 |
|
| 20 |
$this->init(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Inicializa as configurações do método de envio. |
| 25 |
*/ |
| 26 |
public function init() { |
| 27 |
// Carrega as configurações do método de envio |
| 28 |
$this->init_form_fields(); |
| 29 |
$this->init_settings(); |
| 30 |
|
| 31 |
$this->enabled = $this->get_option('enabled'); |
| 32 |
$this->title = $this->get_option('title'); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Define os campos do método de envio no painel de administração. |
| 37 |
*/ |
| 38 |
public function init_form_fields() { |
| 39 |
$this->form_fields = [ |
| 40 |
'enabled' => [ |
| 41 |
'title' => __('Ativar/Desativar', 'superfrete'), |
| 42 |
'type' => 'checkbox', |
| 43 |
'label' => __('Ativar SuperFrete nas áreas de entrega', 'superfrete'), |
| 44 |
'default' => 'yes', |
| 45 |
], |
| 46 |
'title' => [ |
| 47 |
'title' => __('Título', 'superfrete'), |
| 48 |
'type' => 'text', |
| 49 |
'description' => __('Este título aparecerá no checkout', 'superfrete'), |
| 50 |
'default' => __('Entrega via SuperFrete', 'superfrete'), |
| 51 |
'desc_tip' => true, |
| 52 |
], |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Calcula o frete e retorna as opções disponíveis. |
| 58 |
*/ |
| 59 |
public function calculate_shipping($package = []) { |
| 60 |
if (!$this->enabled || empty($package['destination']['postcode'])) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$cep_destino = $package['destination']['postcode']; |
| 65 |
$peso_total = 0; |
| 66 |
$dimensoes = ['height' => 2, 'width' => 11, 'length' => 16, 'weight' => 0.3]; |
| 67 |
|
| 68 |
foreach ($package['contents'] as $item) { |
| 69 |
$product = $item['data']; |
| 70 |
$peso_total += $product->get_weight() * $item['quantity']; |
| 71 |
$dimensoes['height'] = max($dimensoes['height'], $product->get_height()); |
| 72 |
$dimensoes['width'] = max($dimensoes['width'], $product->get_width()); |
| 73 |
$dimensoes['length'] += $product->get_length(); |
| 74 |
} |
| 75 |
|
| 76 |
$cep_origem = get_option('woocommerce_store_postcode'); |
| 77 |
|
| 78 |
if (!$cep_origem) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$payload = [ |
| 83 |
'from' => ['postal_code' => $cep_origem], |
| 84 |
'to' => ['postal_code' => $cep_destino], |
| 85 |
'services' => "1,2,17", |
| 86 |
'package' => $dimensoes, |
| 87 |
]; |
| 88 |
$request = new Request(); |
| 89 |
$response = $request->call_superfrete_api('/api/v0/calculator', 'POST', $payload); |
| 90 |
|
| 91 |
if (!empty($response)) { |
| 92 |
foreach ($response as $frete) { |
| 93 |
if (!$frete['has_error']) { |
| 94 |
$rate = [ |
| 95 |
'id' => $this->id . '_' . $frete['id'], |
| 96 |
'label' => $frete['name'] . ' - (' . $frete['delivery_time'] . ' dias úteis)', |
| 97 |
'cost' => floatval($frete['price']), |
| 98 |
]; |
| 99 |
$this->add_rate($rate); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |