| 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 SuperFretePAC extends \WC_Shipping_Method { |
| 12 |
|
| 13 |
public $free_shipping; |
| 14 |
public $extra_days; |
| 15 |
public $extra_cost; |
| 16 |
public $extra_cost_type; |
| 17 |
public function __construct( $instance_id = 'superfrete_pac') { |
| 18 |
$this->id = 'superfrete_pac'; |
| 19 |
$this->instance_id = absint( $instance_id ); |
| 20 |
$this->method_title = __( 'PAC SuperFrete' ); |
| 21 |
$this->method_description = __( 'Envia utilizando PAC' ); |
| 22 |
$this->supports = array( |
| 23 |
'shipping-zones', |
| 24 |
'instance-settings', |
| 25 |
); |
| 26 |
$this->instance_form_fields = array( |
| 27 |
'enabled' => array( |
| 28 |
'title' => __( 'Ativar/Desativar' ), |
| 29 |
'type' => 'checkbox', |
| 30 |
'label' => __('Ativar SuperFrete nas áreas de entrega', 'superfrete'), |
| 31 |
'default' => 'yes', |
| 32 |
), |
| 33 |
'title' => array( |
| 34 |
'title' => __( 'Method Title' ), |
| 35 |
'type' => 'text', |
| 36 |
'description' => __('Este título aparecerá no checkout', 'superfrete'), |
| 37 |
'default' => __('Entrega via PAC SuperFrete', 'superfrete'), |
| 38 |
'desc_tip' => true |
| 39 |
), |
| 40 |
'free_shipping' => array( |
| 41 |
'title' => __('Frete Grátis', 'superfrete'), |
| 42 |
'type' => 'checkbox', |
| 43 |
'label' => __('Ativar frete grátis para este método', 'superfrete'), |
| 44 |
|
| 45 |
), |
| 46 |
'extra_days' => array( |
| 47 |
'title' => __('Dias extras no prazo', 'superfrete'), |
| 48 |
'type' => 'number', |
| 49 |
'description' => __('Dias adicionais ao prazo estimado pela SuperFrete', 'superfrete'), |
| 50 |
'default' => 0, |
| 51 |
'desc_tip' => true |
| 52 |
), |
| 53 |
'extra_cost' => array( |
| 54 |
'title' => __('Valor adicional no frete', 'superfrete'), |
| 55 |
'type' => 'price', |
| 56 |
'description' => __('Valor extra a ser somado ao custo do frete', 'superfrete'), |
| 57 |
'default' => '0', |
| 58 |
'desc_tip' => true |
| 59 |
), |
| 60 |
'extra_cost_type' => array( |
| 61 |
'title' => __('Tipo de valor adicional', 'superfrete'), |
| 62 |
'type' => 'select', |
| 63 |
'description' => __('Escolha se o valor adicional será fixo (R$) ou percentual (%) sobre o frete original.', 'superfrete'), |
| 64 |
'default' => 'fixed', |
| 65 |
'options' => array( |
| 66 |
'fixed' => __('Valor fixo (R$)', 'superfrete'), |
| 67 |
'percent' => __('Porcentagem (%)', 'superfrete'), |
| 68 |
), |
| 69 |
), |
| 70 |
); |
| 71 |
$this->enabled = $this->get_option('enabled'); |
| 72 |
$this->free_shipping = $this->get_option('free_shipping'); |
| 73 |
$this->title = $this->get_option('title'); |
| 74 |
$this->extra_days = $this->get_option('extra_days', 0); |
| 75 |
$this->extra_cost = $this->get_option('extra_cost', 0); |
| 76 |
$this->extra_cost_type = $this->get_option('extra_cost_type', 'fixed'); |
| 77 |
|
| 78 |
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 79 |
} |
| 80 |
protected function get_service_id() { |
| 81 |
return 1; // ID do PAC na API |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Calcula o frete e retorna as opções disponíveis. |
| 87 |
*/ |
| 88 |
public function calculate_shipping($package = []) { |
| 89 |
|
| 90 |
if (!$this->enabled || empty($package['destination']['postcode'])) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$cep_destino = $package['destination']['postcode']; |
| 95 |
$peso_total = 0; |
| 96 |
$produtos = []; |
| 97 |
$insurance_value = 0; |
| 98 |
foreach ($package['contents'] as $item) { |
| 99 |
$product = $item['data']; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
$insurance_value += $product->get_price() * $item['quantity']; |
| 105 |
$weight_unit = get_option('woocommerce_weight_unit'); |
| 106 |
$dimension_unit = get_option('woocommerce_dimension_unit'); |
| 107 |
|
| 108 |
$produtos[] = [ |
| 109 |
'quantity' => $item['quantity'], |
| 110 |
'weight' => ($weight_unit === 'g') ? floatval($product->get_weight()) / 1000 : floatval($product->get_weight()), |
| 111 |
'height' => ($dimension_unit === 'm') ? floatval($product->get_height()) * 100 : floatval($product->get_height()), |
| 112 |
'width' => ($dimension_unit === 'm') ? floatval($product->get_width()) * 100 : floatval($product->get_width()), |
| 113 |
'length' => ($dimension_unit === 'm') ? floatval($product->get_length()) * 100 : floatval($product->get_length()), |
| 114 |
|
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
$cep_origem = get_option('woocommerce_store_postcode'); |
| 119 |
|
| 120 |
if (!$cep_origem) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
$payload = [ |
| 125 |
'from' => ['postal_code' => $cep_origem], |
| 126 |
'to' => ['postal_code' => $cep_destino], |
| 127 |
'services' => "1", |
| 128 |
'options'=> [ |
| 129 |
"own_hand"=>false, |
| 130 |
"receipt"=>false, |
| 131 |
"insurance_value"=> $insurance_value, |
| 132 |
"use_insurance_value"=>false |
| 133 |
], |
| 134 |
|
| 135 |
'products' => $produtos, |
| 136 |
]; |
| 137 |
|
| 138 |
$request = new Request(); |
| 139 |
|
| 140 |
|
| 141 |
$response = $request->call_superfrete_api('/api/v0/calculator', 'POST', $payload); |
| 142 |
|
| 143 |
if (!empty($response)) { |
| 144 |
foreach ($response as $frete) { |
| 145 |
$prazo_total = $frete['delivery_time'] + $this->extra_days; |
| 146 |
$text_dias = ($prazo_total <= 1) ? "dia útil" : "dias úteis"; |
| 147 |
|
| 148 |
if (!$frete['has_error'] && !isset($frete["error"])) { |
| 149 |
$frete_custo = 0; |
| 150 |
|
| 151 |
if ($this->free_shipping !== 'yes') { |
| 152 |
$frete_base = floatval($frete['price']); |
| 153 |
if ($this->extra_cost_type === 'percent') { |
| 154 |
$frete_custo = $frete_base + ($frete_base * ($this->extra_cost / 100)); |
| 155 |
} else { |
| 156 |
$frete_custo = $frete_base + $this->extra_cost; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
$frete_desc = ($this->free_shipping === 'yes') ? "- Frete Grátis" : ""; |
| 161 |
|
| 162 |
$rate = [ |
| 163 |
'id' => $this->id . '_' . $frete['id'], |
| 164 |
'label' => $frete['name'] . ' - Promocional - (' . $prazo_total . ' ' . $text_dias . ') ' . $frete_desc, |
| 165 |
'cost' => $frete_custo |
| 166 |
]; |
| 167 |
$this->add_rate($rate); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public function process_admin_options() { |
| 174 |
parent::process_admin_options(); // Chama a função do WooCommerce para salvar |
| 175 |
} |
| 176 |
} |
| 177 |
|