Posted on 30/01/2020 11:45:52
Thanks Nicolai and sorry , i forgot to send the feeprovider
Here is the code of the provider:
[AddInActive(true)]
[AddInLabel("Gastos de envio por codigo postal")]
[AddInName("Netaservice.FeeProviderCodigoPostal")]
public class FeeProviderCodigoPostal : Dynamicweb.Ecommerce.Orders.FeeProvider
{
public override Dynamicweb.Ecommerce.Prices.PriceRaw FindFee(Dynamicweb.Ecommerce.Orders.Order order)
{
Dynamicweb.Ecommerce.Prices.PriceRaw returnFee = null;
double gastosEnvio = 0.0;
Discount discount = new Discount();
//comprobar si los gastos de envio son gratis
foreach (var ol in order.OrderLines)
{
if (!string.IsNullOrEmpty(ol.DiscountId))
{
using (var myDr = Database.CreateDataReader("SELECT * FROM EcomDiscount WHERE DiscountId="+ ol.DiscountId + ";"))
{
while (myDr.Read())
{
//si los gastos de envio son gratis poner los gastos de envio a 0
if(Convert.ToBoolean(myDr["DiscountAddFreeShipping"]) && Convert.ToBoolean(myDr["DiscountActive"]))
return new Dynamicweb.Ecommerce.Prices.PriceRaw(0.0, Dynamicweb.Ecommerce.Common.Application.DefaultCurrency);
}
}
}
}
//comprobar si se ha introducido un codigo postal
string codigoPostal = order.OrderFieldValues.GetOrderFieldValue("CodigoPostalEnvio").Value as string;
if(!string.IsNullOrEmpty(codigoPostal))
{
//buscar el codigo postal en la tabla
//añadir la linea de subida a domicilio
bool existeCodigoPostal = false;
bool isBaleares = false;
//comprobar si el codigo postal existe en la tabla
using (var myDr = Database.CreateDataReader("SELECT * FROM ItemType_Codigos_postales WHERE Codigo_postal='" + codigoPostal + "';"))
{
//leer valor
while (myDr.Read())
{
existeCodigoPostal = true;
//comprobar si es baleares
if (!string.IsNullOrEmpty(Convert.ToString(myDr["Provincia"]))
&& Convert.ToString(myDr["Provincia"]).Equals("Baleares"))
isBaleares = true;
break;
}
}
//si existe el codigo postal buscamos los gastos de envio de los articulos
foreach(var orderLine in order.OrderLines)
{
foreach (var g in orderLine.Product.Groups)
{
bool hasGasto = false;
using (var myDir = Database.CreateDataReader("SELECT TOP(1) * FROM ItemType_GastosEnvioGrupo WHERE GroupId='" + g.Id + "';"))
{
while (myDir.Read())
{
if(isBaleares)
{
gastosEnvio += (Convert.ToDouble(myDir["Gasto_baleares"]) * orderLine.Quantity);
}
else if (existeCodigoPostal)
{
gastosEnvio += (Convert.ToDouble(myDir["Gasto_con_cp"]) * orderLine.Quantity);
}
else
{
gastosEnvio += (Convert.ToDouble(myDir["Gasto_sin_cp"]) * orderLine.Quantity);
}
hasGasto = true;
break;
}
}
//si ya se han insertado los gastos de envio se comprueba el siguiente articulo
if (hasGasto)
break;
}
}
}
OrderService orderService = new OrderService();
orderService.RemoveOrderCache(order.Id);
//aplicar gastos de envio
returnFee = new Dynamicweb.Ecommerce.Prices.PriceRaw(gastosEnvio, Dynamicweb.Ecommerce.Common.Application.DefaultCurrency);
return returnFee;
}
}
Also i modify the form to put a default country code and payment and still without work. Here is the modified form template
<form name="ordersubmit" id="OrderSubmit" method="post" action="/Default.aspx?ID=2439" autocomplete="off">
<div class="cart-summary__info u-pull--right u-border-top dw-mod">
<div id="codigo_postal" style="margin-top: 15px;">
@if (string.IsNullOrEmpty(GetString("CodigoPostalEnvio.Clean")))
{
<div class="cart-summary__info dw-mod">
<p style="font-size:14px;color:#f33;">
<strong>
Indica tu código postal para calcular tus gastos de envío y poder continuar con el proceso de compra:
</strong>
</p>
</div>
}
<div class="cart-summary__info dw-mod">
<i class="fas fa-map-pin"></i>
Código postal
</div>
<div class="cart-summary__info u-pull--right dw-mod">
<input type="text" name="CodigoPostalEnvio" maxlength="5" value='@GetString("CodigoPostalEnvio.Clean")' pattern='((0[1-9]|5[0-2])|[1-4][0-9])[0-9]{3}'>
<!-- @GetString("CodigoPostalEnvio") -->
<button type="submit" class="btn btn--primary dw-mod" name="CartV2.GotoStep0" id="CartV2.GotoStep0">Calcular</button>
</div>
<!-- Campos auxiliares -->
<div style="display: none;">
<select class="u-full-width" name="EcomOrderCustomerCountry" id="EcomOrderCustomerCountry" onchange="Cart.SubmitCart()">
<option value="ES" selected="">Spain</option>
</select>
</div>
<div style="display: none;">
<input id="EcomCartPaymethodId_PAY11" name="EcomCartPaymethodId" type="radio" checked="true" value="PAY11" data-expand="savedCards_Contrareembolso" class="form__control dw-mod">
</div>
</div>
</div>
</form>
Debuging i found that the feed is only called when on click on the minicart button... not in the purchase process
Jose,
Regards.