Skip to content

Commit

Permalink
Merge pull request #188 from melhorenvio/feature/add-compatibility-pl…
Browse files Browse the repository at this point in the history
…ugins-composite
  • Loading branch information
JoaoPedro0000 authored May 6, 2024
2 parents f4496b8 + defc35f commit a093fed
Show file tree
Hide file tree
Showing 29 changed files with 683 additions and 632 deletions.
11 changes: 10 additions & 1 deletion Controllers/ShowCalculatorProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MelhorEnvio\Controllers;

use MelhorEnvio\Helpers\EscapeAllowedTags;
use MelhorEnvio\Services\Products\ProductsService;

/**
* Controller to manage the product's paged calculator
Expand Down Expand Up @@ -100,6 +101,14 @@ public function addCalculateShipping() {
* @return string
*/
private function getTemplateCalculator() {
return "<div id='woocommerce-correios-calculo-de-frete-na-pagina-do-produto' class='containerCalculator'> <?php wp_nonce_field('solicita_calculo_frete', 'solicita_calculo_frete'); ?> <input type='hidden' id='calculo_frete_endpoint_url' value='".admin_url( 'admin-ajax.php' )."'> <input type='hidden' id='calculo_frete_produto_altura' value='".$this->height."'> <input type='hidden' id='calculo_frete_produto_largura' value='".$this->width."'> <input type='hidden' id='calculo_frete_produto_comprimento' value='".$this->length."'> <input type='hidden' id='calculo_frete_produto_peso' value='".$this->weight."'> <input type='hidden' id='calculo_frete_produto_preco' value='".$this->price."'> <input type='hidden' id='id_produto' value='".$this->id."'> <div class='calculatorRow'> <div class='row'> <div class='col-75'> <p>Simulação de frete</p> <input type='text' maxlength='9' class='iptCep calculatorRow' id='inputCep' placeholder='Informe seu cep' onkeyup='usePostalCodeMask(event);'> </div> </div> <div id='calcular-frete-loader'> <img src='".MELHORENVIO_ASSETS."/images/loader.gif' /> </div> <div class=resultado-frete tableResult> <table> <thead> </thead> <tbody> </tbody> </table> <small id='destiny-shipping-mehor-envio'></small></br> <small class='observation-shipping-free'></small> </div> </div> </div>";
return "<div id='woocommerce-correios-calculo-de-frete-na-pagina-do-produto' class='containerCalculator'> <?php wp_nonce_field('solicita_calculo_frete', 'solicita_calculo_frete'); ?> <input type='hidden' id='calculo_frete_endpoint_url' value='".admin_url( 'admin-ajax.php' )."'> <input type='hidden' id='calculo_frete_produto_altura' value='".$this->height."'> <input type='hidden' id='calculo_frete_produto_largura' value='".$this->width."'> <input type='hidden' id='calculo_frete_produto_comprimento' value='".$this->length."'> <input type='hidden' id='calculo_frete_produto_peso' value='".$this->weight."'> <input type='hidden' id='calculo_frete_produto_preco' value='".$this->price."'> <input type='hidden' id='id_produto' value='".$this->id."'> <div class='calculatorRow'> <div class='row'> <div class='col-75'> <p>Simulação de frete</p>". $this->verifyCanQuoteByProductType() . "</div></div><div id='calcular-frete-loader'> <img src='".MELHORENVIO_ASSETS."/images/loader.gif' /> </div> <div class=resultado-frete tableResult> <table> <thead> </thead> <tbody> </tbody> </table> <small id='destiny-shipping-mehor-envio'></small></br> <small class='observation-shipping-free'></small> </div> </div> </div>";
}

private function verifyCanQuoteByProductType(){
if( ProductsService::hasProductComposition($this->product) ){
return "<span>A cotação do frete desse produto deve ser feita no carrinho!</span>";
}

return "<input type='text' maxlength='9' class='iptCep calculatorRow' id='inputCep' placeholder='Informe seu cep' onkeyup='usePostalCodeMask(event);'>";
}
}
33 changes: 33 additions & 0 deletions Factory/ProductServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MelhorEnvio\Factory;

use Exception;

class ProductServiceFactory
{
/**
* @throws Exception
*/
public static function fromProduct($product)
{
switch ($product->get_type()) {
case 'woosb':
return new \MelhorEnvio\Services\Products\BundleService();
case 'composite':
return new \MelhorEnvio\Services\Products\CompositeService();
default:
return new \MelhorEnvio\Services\Products\ProductsService();
}
}

/**
* @throws Exception
*/
public static function fromId($productId)
{
$product = wc_get_product($productId);

return self::fromProduct($product);
}
}
27 changes: 27 additions & 0 deletions Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MelhorEnvio\Models;

class Product
{
public $id;
public $name;
public $quantity;
public $weight;
public $height;
public $width;
public $length;
public $unitary_value;
public $insurance_value;
public $type;
public $is_virtual;
public $components = [];
public $parentId;
public $pricing;
public $shipping_fee;

public function setValues($value){
$this->unitary_value = (float) $value;
$this->insurance_value = (float) $value;
}
}
2 changes: 1 addition & 1 deletion Models/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class Version {

const VERSION = '2.15.0';
const VERSION = '2.15.1';
}
8 changes: 7 additions & 1 deletion Services/AgenciesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AgenciesService {

protected $company = null;

protected $service = null;

public function __construct( $data ) {

if ( ! empty( $data['state'] ) ) {
Expand Down Expand Up @@ -87,8 +89,12 @@ private function normalize( $data ) {

$companyId = null;

if (!isset($agency->companies)){
continue;
}

foreach ($agency->companies as $company) {

$companyId = $company->id;

if ( empty( $companyId ) ) {
Expand Down
13 changes: 6 additions & 7 deletions Services/CalculateShippingMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace MelhorEnvio\Services;

use Exception;
use MelhorEnvio\Helpers\MoneyHelper;
use MelhorEnvio\Helpers\TimeHelper;
use MelhorEnvio\Models\ShippingService;
use MelhorEnvio\Helpers\PostalCodeHelper;
use MelhorEnvio\Services\WooCommerceBundleProductsService;

class CalculateShippingMethodService {

Expand Down Expand Up @@ -38,19 +38,18 @@ class CalculateShippingMethodService {
* @param int $timeExtra
* @param int $percent
* @return bool
* @throws Exception
*/
public function calculateShipping( $package = array(), $code, $id, $company, $title, $taxExtra, $timeExtra, $percent ) {
public function calculateShipping( $package, $code, $id, $company, $title, $taxExtra, $timeExtra, $percent ) {
$to = PostalCodeHelper::postalcode( $package['destination']['postcode'] );
if ( strlen( $to ) != PostalCodeHelper::SIZE_POSTAL_CODE ) {
return false;
}

$products = ( isset( $package['contents'] ) )
? $package['contents']
: ( new CartWooCommerceService() )->getProducts();
$products = ( new CartWooCommerceService() )->getProducts();

if ( WooCommerceBundleProductsService::isWooCommerceProductBundle( $products ) ) {
$products = ( new WooCommerceBundleProductsService() )->manageProductsBundle( $products );
if ( empty( $products ) ) {
$products = $package['contents'];
}

$result = ( new QuotationService() )->calculateQuotationByProducts(
Expand Down
6 changes: 2 additions & 4 deletions Services/CartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

use MelhorEnvio\Models\Order;
use MelhorEnvio\Models\Option;
use MelhorEnvio\Models\Payload;
use MelhorEnvio\Models\Session;
use MelhorEnvio\Models\ShippingCompany;
use MelhorEnvio\Models\ShippingService;
use MelhorEnvio\Helpers\SessionHelper;
use MelhorEnvio\Helpers\PostalCodeHelper;
use MelhorEnvio\Helpers\CpfHelper;
use MelhorEnvio\Helpers\ProductVirtualHelper;
use MelhorEnvio\Services\AgenciesSelectedService;
use MelhorEnvio\Services\Products\ProductsService;

class CartService {

Expand Down Expand Up @@ -90,7 +88,7 @@ public function createPayloadToCart( $orderId, $products, $dataBuyer, $shippingM

$dataFrom = ( new SellerService() )->getData();

$quotation = ( new QuotationService() )->calculateQuotationByPostId( $orderId );
$quotation = ( new QuotationService() )->calculateQuotationByPostId( $orderId , $products);

$orderInvoiceService = new OrderInvoicesService();

Expand Down
50 changes: 18 additions & 32 deletions Services/CartWooCommerceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,35 @@

namespace MelhorEnvio\Services;

use Exception;
use MelhorEnvio\Factory\ProductServiceFactory;
use MelhorEnvio\Helpers\DimensionsHelper;
use MelhorEnvio\Services\Products\ProductsService;

class CartWooCommerceService {

/**
* Function to get alls products on cart woocommerce
*
* @return Array
* @return array
* @throws Exception
*/
public function getProducts() {
public function getProducts(): array
{
global $woocommerce;

$items = $woocommerce->cart->get_cart();

$products = array();

foreach ( $items as $itemProduct ) {
$productId = ( $itemProduct['variation_id'] != 0 )
? $itemProduct['variation_id']
: $itemProduct['product_id'];

$productInfo = wc_get_product( $productId );

if ( empty( $productInfo ) ) {
continue;
}
$data = $productInfo->get_data();

$products[] = array(
'id' => $itemProduct['product_id'],
'variation_id' => $itemProduct['variation_id'],
'name' => $data['name'],
'price' => $productInfo->get_price(),
'insurance_value' => $productInfo->get_price(),
'height' => DimensionsHelper::convertUnitDimensionToCentimeter( $productInfo->get_height() ),
'width' => DimensionsHelper::convertUnitDimensionToCentimeter( $productInfo->get_width() ),
'length' => DimensionsHelper::convertUnitDimensionToCentimeter( $productInfo->get_length() ),
'weight' => DimensionsHelper::convertWeightUnit( $productInfo->get_weight() ),
'quantity' => ( isset( $itemProduct['quantity'] ) )
? intval( $itemProduct['quantity'] )
: 1,
);
}
return $products;
$products = array_map(function($item) use ($items) {
$productId = ( $item['variation_id'] != 0 )
? $item['variation_id']
: $item['product_id'];

$productService = ProductServiceFactory::fromId($productId);

return $productService->getDataByProductCart($item, $items);
}, $items);

return ProductsService::removeComponentProducts($products);
}
}
Loading

0 comments on commit a093fed

Please sign in to comment.