-
-
Save filipegiacomo/204d0d91fa394cda542242ae60051d23 to your computer and use it in GitHub Desktop.
Correção para calculo de frete no carrinho WC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CART SHIPMENT CALCULATE TO HIT CEP | |
case 'cart_shipment': | |
$CartTotal = 0; | |
$HeightTotal = 0; | |
$WidthTotal = 0; | |
$DepthTotal = 0; | |
$WeightTotal = 0; | |
$AmountTotal = 0; | |
foreach ($_SESSION['wc_order'] as $ItemId => $ItemAmount): | |
$Read->ExeRead(DB_PDT, "WHERE pdt_id = (SELECT pdt_id FROM " . DB_PDT_STOCK . " WHERE stock_id = :id)", "id={$ItemId}"); | |
if (!$Read->getResult()): | |
unset($_SESSION['wc_order'][$ItemId]); | |
else: | |
extract($Read->getResult()[0]); | |
$CartTotal += ($pdt_offer_price && $pdt_offer_start <= date('Y-m-d H:i:s') && $pdt_offer_end >= date('Y-m-d H:i:s') ? $pdt_offer_price : $pdt_price) * $ItemAmount; | |
$HeightTotal += $pdt_dimension_height * $ItemAmount; | |
$WidthTotal += $pdt_dimension_width * $ItemAmount; | |
$DepthTotal += $pdt_dimension_depth * $ItemAmount; | |
$WeightTotal += $pdt_dimension_weight * $ItemAmount; | |
$AmountTotal += $ItemAmount; | |
endif; | |
endforeach; | |
$jSON['H'] = $HeightTotal; | |
$CartTotalShip = number_format($CartTotal, '2', ',', ''); | |
$WeightTotalShip = floatval($WeightTotal / 1000); | |
$HeightTotalShip = ($HeightTotal >= 2 ? $HeightTotal : 2); | |
$WidthTotalShip = ($WidthTotal / $AmountTotal >= 11 ? $WidthTotal / $AmountTotal : 11); | |
$DepthTotalShip = ($DepthTotal / $AmountTotal >= 16 ? $DepthTotal / $AmountTotal : 16); | |
$url = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx'; | |
$jSON['cart_shipment'] = null; | |
$serviceCodes = explode(",", ECOMMERCE_SHIPMENT_SERVICE); | |
foreach ($serviceCodes as $key => $code) : | |
$data = array(); | |
$data['sDsSenha'] = (!empty(ECOMMERCE_SHIPMENT_CDSENHA) ? ECOMMERCE_SHIPMENT_CDSENHA : 0); | |
$data['sCepOrigem'] = str_replace('-', '', SITE_ADDR_ZIP); | |
$data['sCepDestino'] = str_replace('-', '', $POST['zipcode']); | |
$data['nVlPeso'] = $WeightTotalShip; | |
$data['nCdFormato'] = ECOMMERCE_SHIPMENT_FORMAT; | |
$data['nVlComprimento'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 16 : $DepthTotalShip); | |
$data['nVlAltura'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 2 : $HeightTotalShip); | |
$data['nVlLargura'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 11 : $WidthTotalShip); | |
$data['nVlDiametro'] = '0'; | |
$data['sCdMaoPropria'] = (ECOMMERCE_SHIPMENT_OWN_HAND == 1 || ECOMMERCE_SHIPMENT_OWN_HAND == 's' ? 's' : 'n'); | |
$data['nVlValorDeclarado'] = (ECOMMERCE_SHIPMENT_DECLARE ? $CartTotalShip : '0'); | |
$data['sCdAvisoRecebimento'] = (ECOMMERCE_SHIPMENT_ALERT ? 's' : 'n'); | |
$data['StrRetorno'] = 'xml'; | |
$data['nCdServico'] = $code; | |
$query = http_build_query($data); | |
$curl = curl_init($url . '?' . $query); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($curl); | |
$resultXml = simplexml_load_string($result); | |
foreach ($resultXml->cServico as $row): | |
if (!empty($row->Valor) && $row->Valor != '0,00'): | |
$jSON['cart_shipment'] .= "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='" . str_replace(',', '.', $row->Valor) . "' type='radio' id='{$row->Codigo}'/> " . getShipmentTag(intval($row->Codigo)) . ": 01 a " . str_pad($row->PrazoEntrega + ECOMMERCE_SHIPMENT_DELAY, 2, 0, 0) . " dias úteis - R$ {$row->Valor}</label>"; | |
endif; | |
endforeach; | |
if (!empty($row->Erro) && $row->Erro == '-3'): | |
$jSON['trigger'] = AjaxErro("<b>OPPSSS:</b> O CEP digitado não foi encontrado na base dos correios. Confira isso :)", E_USER_WARNING); | |
$jSON['reset'] = true; | |
$ErroZip = true; | |
unset($_SESSION['wc_shipment'], $_SESSION['wc_shipment_zip']); | |
break; | |
endif; | |
endforeach; | |
$CompanyPrice = $CartTotal * (ECOMMERCE_SHIPMENT_COMPANY_VAL / 100); | |
if (ECOMMERCE_SHIPMENT_COMPANY && $CompanyPrice >= ECOMMERCE_SHIPMENT_COMPANY_PRICE && empty($ErroZip)): | |
$jSON['cart_shipment'] .= "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='{$CompanyPrice}' type='radio' id='10001'/> Envio Padrão: 01 a " . str_pad(ECOMMERCE_SHIPMENT_DELAY + ECOMMERCE_SHIPMENT_COMPANY_DAYS, 2, 0, 0) . " dias úteis - R$ " . number_format($CompanyPrice, '2', ',', '.') . "</label>"; | |
endif; | |
$CartPrice = (empty($_SESSION['wc_cupom']) ? $CartTotal : $CartTotal * ((100 - $_SESSION['wc_cupom']) / 100)); | |
if (ECOMMERCE_SHIPMENT_FREE && $CartPrice > ECOMMERCE_SHIPMENT_FREE && empty($ErroZip)): | |
$jSON['cart_shipment'] .= "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='0' type='radio' id='10002'/> Envio Gratuito: 01 a " . str_pad(ECOMMERCE_SHIPMENT_DELAY + ECOMMERCE_SHIPMENT_FREE_DAYS, 2, 0, 0) . " dias úteis - R$ 0,00</label>"; | |
endif; | |
if (ECOMMERCE_SHIPMENT_FIXED): | |
$jSON['cart_shipment'] .= "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='" . ECOMMERCE_SHIPMENT_FIXED_PRICE . "' type='radio' id='10003'/> Frete Fixo: 01 a " . str_pad(ECOMMERCE_SHIPMENT_DELAY + ECOMMERCE_SHIPMENT_FIXED_DAYS, 2, 0, 0) . " dias úteis - R$ " . number_format(ECOMMERCE_SHIPMENT_FIXED_PRICE, 2, ',', '.') . "</label>"; | |
endif; | |
if (ECOMMERCE_SHIPMENT_LOCAL): | |
$ECOMMERCE_SHIPMENT_LOCAL = explode(",", ECOMMERCE_SHIPMENT_LOCAL); | |
$City = json_decode(file_get_contents("https://viacep.com.br/ws/" . str_replace('-', '', $POST['zipcode']) . "/json/")); | |
if (!empty($City) && !empty($City->localidade) && in_array($City->localidade, array_map('trim', $ECOMMERCE_SHIPMENT_LOCAL))): | |
$jSON['cart_shipment'] = "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='" . ECOMMERCE_SHIPMENT_LOCAL_PRICE . "' type='radio' id='10004'/> Taxa de entrega: R$ " . number_format(ECOMMERCE_SHIPMENT_LOCAL_PRICE, 2, ',', '.') . "</label>"; | |
endif; | |
if (ECOMMERCE_SHIPMENT_LOCAL_IN_PLACE): | |
$jSON['cart_shipment'] .= "<label class='shiptag'><input required class='wc_shipment' name='shipment' value='0' type='radio' id='10005'/> Retirar na Loja: R$ 0,00</label>"; | |
endif; | |
endif; | |
if (empty($jSON['cart_shipment']) && empty($ErroZip)): | |
$jSON['trigger'] = AjaxErro("<b>OPPSSS:</b> Não existem opções de entrega para o pedido autal. Você pode remover ou adicionar alguns produtos para tentar novamente!<p>Ou caso queira, entre em contato para que possamos te ajudar!</p><p>Fone: " . SITE_ADDR_PHONE_A . "<br>E-mail: " . SITE_ADDR_EMAIL . "</p>", E_USER_WARNING); | |
elseif (empty($ErroZip)): | |
$_SESSION['wc_shipment_zip'] = $POST['zipcode']; | |
endif; | |
break; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment