Skip to content

Commit

Permalink
Altura, Largura e Comprimento
Browse files Browse the repository at this point in the history
1. [](#fix)
   * #5

2. [](#add)
   * Mapeamento dos atributos Altura, Largura e Comprimento de forma dinâmica pelo Admin.
  • Loading branch information
eloom committed Oct 16, 2023
1 parent 2058657 commit 1f2e563
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 31 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 2.0.2
## 16/10/2023

1. [](#fix)
* https://github.com/eloom/module-correios-frete/issues/5

2. [](#add)
* Mapeamento dos atributos Altura, Largura e Comprimento de forma dinâmica pelo Admin.

# 2.0.1
## 21/09/2023

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
*
* Frete com Correios para Magento
*
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
*/
declare(strict_types=1);

namespace Eloom\CorreiosFrete\Block\Adminhtml\Config\Source;

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;

class Attributes implements \Magento\Framework\Option\ArrayInterface {

private $attributeFactory;

public function __construct(Attribute $attributeFactory) {
$this->attributeFactory = $attributeFactory;
}

public function toOptionArray() {
$attributes = $this->attributeFactory->getCollection();

$options = [];
$options[] = ['value' => '', 'label' => 'Selecione'];

foreach ($attributes as $attribute) {
$front = $attribute->getFrontendLabel();

if (!empty($front)) {
$options[] = ['value' => $attribute->getAttributecode(), 'label' => $attribute->getAttributecode()];
} else {
$options[] = ['value' => $attribute->getAttributecode(), 'label' => $attribute->getAttributecode()];
}
}

sort($options);

return $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
2 changes: 1 addition & 1 deletion src/module-correios-frete/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
83 changes: 61 additions & 22 deletions src/module-correios-frete/Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand All @@ -19,7 +19,7 @@
use Eloom\SdkCorreios\Errors;
use Eloom\SdkCorreios\Endpoints\Rastro;
use Eloom\SdkCorreios\Exceptions\UnauthorizedException;

use Eloom\SdkCorreios\Exceptions\CorreiosException;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
Expand All @@ -40,9 +40,9 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface {
private $fromZip = null;
private $toZip = null;
private $hasFreeMethod = false;
private $nVlComprimento = 0;
private $nVlAltura = 0;
private $nVlLargura = 0;
private $totalLength = 0;
private $totalHeight = 0;
private $totalWidth = 0;

/**
* Rate result data
Expand Down Expand Up @@ -145,39 +145,67 @@ private function check(RateRequest $request) {

return false;
}
$weightAttr = $this->getConfigData('weight');

$widthAttr = $this->getConfigData('width');
$heightAttr = $this->getConfigData('height');
$lengthAttr = $this->getConfigData('length');

$defaultHeight = $this->getConfigData('default_height');
$defaultWidth = $this->getConfigData('default_width');
$defaultLength = $this->getConfigData('default_length');

$price = 0;
$weight = 0;
if ($request->getPackageValue() > 0 && $request->getPackageWeight() > 0) {
$price = $request->getPackageValue();
$weight = $request->getPackageWeight();
} else if ($request->getAllItems()) {

$width = 0;
$height = 0;
$length = 0;

if ($request->getAllItems()) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
foreach ($request->getAllItems() as $item) {
if ($item->getProduct()->isVirtual()) {
continue;
}

if ($item->getHasChildren() && $item->isShipSeparately()) {
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
$product = $objectManager->create(Product::class)->load($child->getProductId());

$price += (float)(!is_null($product->getData('special_price')) ? $product->getData('special_price') : $product->getData('price'));
$weight += (float)$product->getData('weight');
if (!$child->getProduct()->isVirtual()) {
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($child->getProductId());

$price += ($item->getPrice() - $item->getDiscountAmount());
$parentIds = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($product->getId());
if (!$parentIds) {
$parentIds = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->getParentIdsByChild($product->getId());

if ($parentIds) {
$parentProd = $objectManager->create('Magento\Catalog\Model\Product')->load($parentIds[0]);

$weight += (float)$parentProd->getData($weightAttr);
$width += ($parentProd->getData($widthAttr) > 0 ? $parentProd->getData($widthAttr) : $defaultWidth);
$height += ($parentProd->getData($heightAttr) > 0 ? $parentProd->getData($heightAttr) : $defaultHeight);
$length += ($parentProd->getData($lengthAttr) > 0 ? $parentProd->getData($lengthAttr) : $defaultLength);
}
}
}
}
} else {
$product = $objectManager->create(Product::class)->load($item->getProductId());
if ($product->getTypeId() == 'simple') {
$price += (float)(!is_null($product->getData('special_price')) ? $product->getData('special_price') : $product->getData('price'));
$weight += (float)$product->getData('weight');

$weight += (float)$product->getData($weightAttr);
$width += ($product->getData($widthAttr) > 0 ? $product->getData($widthAttr) : $defaultWidth);
$height += ($product->getData($heightAttr) > 0 ? $product->getData($heightAttr) : $defaultHeight);
$length += ($product->getData($lengthAttr) > 0 ? $product->getData($lengthAttr) : $defaultLength);
}
}
}
}
$this->nVlAltura = $this->getConfigData('default_height');
$this->nVlLargura = $this->getConfigData('default_width');
$this->nVlComprimento = $this->getConfigData('default_length');
$this->totalHeight = $height;
$this->totalWidth = $width;
$this->totalLength = $length;

$this->hasFreeMethod = $request->getFreeShipping();
$this->_freeMethod = $this->getConfigData('servico_gratuito');
Expand Down Expand Up @@ -376,7 +404,6 @@ private function calcPrecoPrazo() {
$prazoClient->withProduct($codigoServicos);
}
$prazos = $prazoClient->withCepOrigem($this->fromZip)->withCepDestino($this->toZip)->nacional();


/**
* Preço
Expand All @@ -391,7 +418,7 @@ private function calcPrecoPrazo() {
$precoClient->withProduct($codigoServicos);
}
$precoClient->withCepOrigem($this->fromZip)->withCepDestino($this->toZip);
$precoClient->withDiametro(0)->withAltura($this->nVlAltura)->withLargura($this->nVlLargura)->withComprimento($this->nVlComprimento);
$precoClient->withDiametro(0)->withAltura($this->totalHeight)->withLargura($this->totalWidth)->withComprimento($this->totalLength);

$nVlPeso = 0;
if ($this->volumeWeight > $this->getConfigData('volume_weight_min') && $this->volumeWeight > $this->packageWeight) {
Expand All @@ -406,7 +433,19 @@ private function calcPrecoPrazo() {
}
$precoClient->withTpObjeto($this->getConfigData('cd_formato'));

$precos = $precoClient->nacional();
try {
$precos = $precoClient->nacional();
} catch (CorreiosException $exception) {
$this->logger->critical($exception->getMessage());

$rate = $this->_rateErrorFactory->create();
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
$rate->setErrorMessage($exception->getMessage());
$this->getRateResult()->append($rate);

return $this->getRateResult();
}

/**
* Merge
Expand Down
2 changes: 1 addition & 1 deletion src/module-correios-frete/Setup/InstallData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
2 changes: 1 addition & 1 deletion src/module-correios-frete/Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @category elOOm
* @package Modulo Frete com Correios
* @copyright Copyright (c) 2023 elOOm (https://eloom.tech)
* @version 2.0.1
* @version 2.0.2
* @license https://opensource.org/licenses/OSL-3.0
* @license https://opensource.org/licenses/AFL-3.0
*
Expand Down
17 changes: 17 additions & 0 deletions src/module-correios-frete/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@
<validate>required-entry validate-digits</validate>
</field>

<field id="fields" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Mapeamento de atributos</label>
<frontend_model>Magento\Config\Block\System\Config\Form\Field\Heading</frontend_model>
</field>
<field id="width" translate="label" type="select" sortOrder="71" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Largura</label>
<source_model>Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes</source_model>
</field>
<field id="height" translate="label" type="select" sortOrder="72" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Altura</label>
<source_model>Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes</source_model>
</field>
<field id="length" translate="label" type="select" sortOrder="73" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Comprimento</label>
<source_model>Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes</source_model>
</field>

<field id="showmethod" translate="label" type="select" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show Method if Not Applicable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
6 changes: 5 additions & 1 deletion src/module-correios-frete/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
<vl_valor_declarado>0</vl_valor_declarado>
<cd_mao_propria>0</cd_mao_propria>
<cd_aviso_recebimento>0</cd_aviso_recebimento>
<default_width>11</default_width>
<default_height>2</default_height>
<default_length>16</default_length>
<default_width>11</default_width>
<prazo_entrega>1</prazo_entrega>
<taxa_extra>0</taxa_extra>
<taxa_extra_valor>0</taxa_extra_valor>
<prazo_extra>0</prazo_extra>
<mensagem_prazo_entrega><![CDATA[%s - Até %d dia(s) úteis]]></mensagem_prazo_entrega>
<width>volume_largura</width>
<height>volume_altura</height>
<length>volume_comprimento</length>
<weight>weight</weight>
<volume_weight_min>5</volume_weight_min>
<sort_order>15</sort_order>
</eloom_correios>
Expand Down
2 changes: 1 addition & 1 deletion src/module-correios-frete/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Eloom_CorreiosFrete" setup_version="2.0.1">
<module name="Eloom_CorreiosFrete" setup_version="2.0.2">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Sales"/>
Expand Down

0 comments on commit 1f2e563

Please sign in to comment.