Last active
January 26, 2022 16:34
-
-
Save rafhaelhp/7b080f20b260cb98c5e4aac1d477bd74 to your computer and use it in GitHub Desktop.
Correção atualizada 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; | |
$dimensoes = array(); | |
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; | |
$AmountTotal += $ItemAmount; | |
$dimensoes[] = [ | |
"altura" => $pdt_dimension_heigth, | |
"largura" => $pdt_dimension_width, | |
"comprimento" => $pdt_dimension_depth, | |
"peso" => $pdt_dimension_weight, | |
"itens" => $ItemAmount | |
]; | |
endif; | |
endforeach; | |
$menoresDimensoes = array(); | |
$altura = 0; | |
$largura = 0; | |
$comprimento = 0; | |
$volume_total = 0; | |
$pesoTotal = 0; | |
foreach ($dimensoes as $key => $item): | |
$menor = [$item['altura'], $item['largura'], $item['comprimento']]; | |
sort($menor); | |
$menoresDimensoes[] = intval($menor[0] * $item['itens']); | |
if (intval($item['largura'] * $item['itens']) > $largura): | |
$largura = intval($item['largura'] * $item['itens']); | |
endif; | |
if (intval($item['comprimento'] * $item['itens']) > $comprimento): | |
$comprimento = intval($item['comprimento'] * $item['itens']); | |
endif; | |
$pesoTotal += $item['peso'] * $item['itens']; | |
endforeach; | |
$altura = array_sum($menoresDimensoes); | |
$volume_total = $altura + $largura + $comprimento; | |
$CartTotalShip = number_format($CartTotal, '2', ',', ''); | |
$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 : ""); | |
$data['nCdEmpresa'] = ""; | |
$data['sCepOrigem'] = str_replace('-', '', SITE_ADDR_ZIP); | |
$data['sCepDestino'] = str_replace('-', '', $POST['zipcode']); | |
$data['nVlPeso'] = floatval($pesoTotal / 1000); | |
$data['nCdFormato'] = ECOMMERCE_SHIPMENT_FORMAT; | |
$data['nVlComprimento'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 16 : ($comprimento >= 16 ? $comprimento : 16)); | |
$data['nVlAltura'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 2 : ($altura >= 2 ? $altura : 2)); | |
$data['nVlLargura'] = (ECOMMERCE_SHIPMENT_BY_WEIGHT ? 11 : ($largura >= 11 ? $largura : 11)); | |
$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): | |
if (ECOMMERCE_SHIPMENT_LOCAL_PRICE): | |
$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) && strstr(ECOMMERCE_SHIPMENT_LOCAL, $City->localidade)): | |
$jSON['cart_shipment'] .= "<li><label><input type='radio' name='shipment' value='" . ECOMMERCE_SHIPMENT_LOCAL_PRICE . "' id='10004'><span>Taxa de entrega: R$ " . number_format(ECOMMERCE_SHIPMENT_LOCAL_PRICE, 2, ',', '.') . "</span></label></li>"; | |
endif; | |
endif; | |
$cidade_retirar = array_map("trim", explode(",", ECOMMERCE_SHIPMENT_LOCAL)); | |
$City = json_decode(file_get_contents("https://viacep.com.br/ws/" . str_replace('-', '', $POST['zipcode']) . "/json/")); | |
if (ECOMMERCE_SHIPMENT_LOCAL_IN_PLACE && !empty($City->localidade) && in_array(trim($City->localidade), $cidade_retirar)): | |
$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> As dimensões totais do seu carrinho são maiores que as permitidas pelos correios. Por favor, tente remover alguns itens e calcule novamente o frete. Caso o erro persista você pode dividir o seu carrinho executando dois pedidos separados ou entrar em contato com a gente para tentarmos enviar por outro meio.</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; |
Vlw :)
ObrIgado
Opa, blz? estou tendo o seguinte erro utilizando o padrão do wc:
Erro na Linha: #162 :: Undefined variable: pdt_dimension_height
/home/caramell/public_html/_cdn/widgets/ecommerce/cart.ajax.php
{"H":0,"cart_shipment":" Taxa de entrega: R$ 15,00</label> Retirar na Loja: R$ 0,00</label>"}
Alguma correção do seguinte erro Linha: #162 :: Undefined variable: pdt_dimension_height
/home/caramell/public_html/_cdn/widgets/ecommerce/cart.ajax.php
{"H":0,"cart_shipment":" Taxa de entrega: R$ 15,00 Retirar na Loja: R$ 0,00"}?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Boa!