Help Integrating Customer Shipping Module

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 年 前
Hi all,

we are trying to integrate a custom module to mopcommerce for our tnt shipping.

It is a basic change to the shippingbyweight, so that instead of doing the calc;

SHIPPING = WEIGHT * PRICE PER KG BOUND

we just have a set

SHIPPING = PRICE AT THIS KG BOUND

So in thenopcommerce project i have simply tried to copy the shippingbywieght projec tin the shipping folder and called it tnt with the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Shipping;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.Common;

namespace NopSolutions.NopCommerce.Shipping.Methods.TNTCM
{
    /// <summary>
    /// TNT Shipping by weight computation method
    /// </summary>
    public class TNTComputationMethod : IShippingRateComputationMethod
    {
        #region Utilities

        private decimal GetRate(decimal subTotal, decimal Weight, int ShippingMethodID)
        {
            decimal shippingTotal = decimal.Zero;

            ShippingByWeight shippingByWeight = null;
            var shippingByWeightCollection = ShippingByWeightManager.GetAllByShippingMethodID(ShippingMethodID);
            foreach (var shippingByWeight2 in shippingByWeightCollection)
            {
                if ((Weight >= shippingByWeight2.From) && (Weight <= shippingByWeight2.To))
                {
                    shippingByWeight = shippingByWeight2;
                    break;
                }
            }
            if (shippingByWeight == null)
                return decimal.Zero;
            if (shippingByWeight.UsePercentage && shippingByWeight.ShippingChargePercentage <= decimal.Zero)
                return decimal.Zero;
            if (!shippingByWeight.UsePercentage && shippingByWeight.ShippingChargeAmount <= decimal.Zero)
                return decimal.Zero;
            if (shippingByWeight.UsePercentage)
                shippingTotal = Math.Round((decimal)((((float)subTotal) * ((float)shippingByWeight.ShippingChargePercentage)) / 100f), 2);
            else
            {
                shippingTotal = shippingByWeight.ShippingChargeAmount;
            }
            if (shippingTotal < decimal.Zero)
                shippingTotal = decimal.Zero;
            return shippingTotal;
        }
        #endregion

        #region Methods
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage ShipmentPackage, ref string Error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (ShipmentPackage == null)
                throw new ArgumentNullException("ShipmentPackage");
            if (ShipmentPackage.Items == null)
                throw new NopException("No shipment items");
            if (ShipmentPackage.ShippingAddress == null)
            {
                Error = "Shipping address is not set";
                return shippingOptions;
            }
            if (ShipmentPackage.ShippingAddress.Country == null)
            {
                Error = "Shipping country is not set";
                return shippingOptions;
            }

            decimal subTotal = decimal.Zero;
            foreach (var shoppingCartItem in ShipmentPackage.Items)
            {
                if (shoppingCartItem.IsFreeShipping)
                    continue;
                subTotal += PriceHelper.GetSubTotal(shoppingCartItem, ShipmentPackage.Customer, true);
            }

            decimal weight = ShippingManager.GetShoppingCartTotalWeigth(ShipmentPackage.Items);

            var shippingMethods = ShippingMethodManager.GetAllShippingMethods(ShipmentPackage.ShippingAddress.CountryID);
            foreach (var shippingMethod in shippingMethods)
            {
                var shippingOption = new ShippingOption();
                shippingOption.Name = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate = GetRate(subTotal, weight, shippingMethod.ShippingMethodID);
                shippingOptions.Add(shippingOption);
            }

            return shippingOptions;
        }

        /// <summary>
        /// Gets fixed shipping rate (if shipping rate computation method allows it and the rate can be calculated before checkout).
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <returns>Fixed shipping rate; or null if shipping rate could not be calculated before checkout</returns>
        public decimal? GetFixedRate(ShipmentPackage ShipmentPackage)
        {
            return null;
        }
        #endregion
    }
}

I have compiled this project and put the Nop.Shipping.TNT.dll and Nop.Shipping.TNT Program Debug Database up to the bin folder on our server. (from the Nop.Shipping.TNT\bin\Debug folder).

Next I have changed then created a new folder under Administration/Shipping called TNTConfigure, in this copied and modified slighly the ConfigureShipping.ascx and the ascx.cs to the following

<%@ Control Language="C#" AutoEventWireup="true" Inherits="NopSolutions.NopCommerce.Web.Administration.Shipping.TNT.ConfigureShipping"
    CodeBehind="ConfigureShipping.ascx.cs" %>
<%@ Register TagPrefix="nopCommerce" TagName="DecimalTextBox" Src="../../Modules/DecimalTextBox.ascx" %>
<asp:UpdatePanel ID="upShip" runat="server">
    <ContentTemplate>
        <asp:Panel runat="server" ID="pnlError" EnableViewState="false" Visible="false" class="messageBox messageBoxError">
            <asp:Literal runat="server" ID="lErrorTitle" EnableViewState="false" />
        </asp:Panel>
        <table class="adminContent">
            <tr>
                <td colspan="2" width="100%">
                    <asp:GridView ID="gvTNT" runat="server" AutoGenerateColumns="false"
                        DataKeyNames="TNTID" OnRowDeleting="gvTNT_RowDeleting"
                        OnRowDataBound="gvTNT_RowDataBound" OnRowCommand="gvTNT_RowCommand"
                        Width="100%">
                        <Columns>
                            <asp:TemplateField HeaderText="Shipping method" ItemStyle-Width="13%">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlShippingMethod" CssClass="adminInput" runat="server">
                                    </asp:DropDownList>
                                    <asp:HiddenField ID="hfShippingByWeightID" runat="server" Value='<%# Eval("ShippingByWeightID") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="From" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="12%"
                                ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <nopCommerce:DecimalTextBox runat="server" CssClass="adminInput" Width="50px" Value='<%# Eval("From") %>'
                                        ID="txtFrom" RequiredErrorMessage="From is required" MinimumValue="0" MaximumValue="999999"
                                        ValidationGroup="UpdateShippingByWeight" RangeErrorMessage="The value must be from 0 to 999999">
                                    </nopCommerce:DecimalTextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="To" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%"
                                ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <nopCommerce:DecimalTextBox runat="server" CssClass="adminInput" Width="50px" Value='<%# Eval("To") %>'
                                        ID="txtTo" RequiredErrorMessage="To is required" MinimumValue="0" MaximumValue="999999"
                                        ValidationGroup="UpdateShippingByWeight" RangeErrorMessage="The value must be from 0 to 999999">
                                    </nopCommerce:DecimalTextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Charge amount" HeaderStyle-HorizontalAlign="Center"
                                ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <nopCommerce:DecimalTextBox runat="server" CssClass="adminInput" Width="50px" Value='<%# Eval("ShippingChargeAmount") %>'
                                        ID="txtShippingChargeAmount" RequiredErrorMessage="Charge amount is required"
                                        MinimumValue="0" MaximumValue="999999" ValidationGroup="UpdateShippingByWeight"
                                        RangeErrorMessage="The value must be from 0 to 999999"></nopCommerce:DecimalTextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Update" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%"
                                ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <asp:Button ID="btnUpdate" runat="server" CssClass="adminButton" Text="Update" ValidationGroup="UpdateShippingByWeight"
                                        CommandName="UpdateShippingByWeight" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%"
                                ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <asp:Button ID="btnDelete" runat="server" CssClass="adminButton" Text="Delete" CausesValidation="false"
                                        CommandName="Delete" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td colspan="2" width="100%">
                    <hr />
                </td>
            </tr>
            <tr>
                <td colspan="2" width="100%">
                    <br />
                    Adding new values:
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    Select shipping method:
                </td>
                <td class="adminData">
                    <asp:DropDownList ID="ddlShippingMethod" runat="server" CssClass="adminInput">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    Order weight from [<%=MeasureManager.BaseWeightIn.Name%>]:
                </td>
                <td class="adminData">
                    <nopCommerce:DecimalTextBox runat="server" ID="txtFrom" Value="0" RequiredErrorMessage="From is required"
                        MinimumValue="0" MaximumValue="999999" ValidationGroup="AddShippingByWeight"
                        RangeErrorMessage="The value must be from 0 to 999999" CssClass="adminInput">
                    </nopCommerce:DecimalTextBox>
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    Order weight to [<%=MeasureManager.BaseWeightIn.Name%>]:
                </td>
                <td class="adminData">
                    <nopCommerce:DecimalTextBox runat="server" ID="txtTo" Value="0" RequiredErrorMessage="To is required"
                        MinimumValue="0" MaximumValue="999999" ValidationGroup="AddShippingByWeight"
                        RangeErrorMessage="The value must be from 0 to 999999" CssClass="adminInput">
                    </nopCommerce:DecimalTextBox>
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    Charge amount []
                :</td>
                <td class="adminData">
                    <nopCommerce:DecimalTextBox runat="server" ID="txtShippingChargeAmount" Value="0"
                        RequiredErrorMessage="Charge amount is required" MinimumValue="0" MaximumValue="999999"
                        ValidationGroup="AddShippingByWeight" RangeErrorMessage="The value must be from 0 to 999999"
                        CssClass="adminInput"></nopCommerce:DecimalTextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="left">
                    <asp:Button runat="server" ID="btnAdd" Text="Add new" CssClass="adminButton" ValidationGroup="AddShippingByWeight"
                        OnClick="btnAdd_Click"></asp:Button>
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="up1" runat="server" AssociatedUpdatePanelID="upShip">
    <ProgressTemplate>
        <div class="progress">
            <asp:Image ID="imgUpdateProgress" runat="server" ImageUrl="~/images/UpdateProgress.gif" AlternateText="update" />
            <%=GetLocaleResourceString("Admin.Common.Wait...")%>
        </div>
    </ProgressTemplate>
</asp:UpdateProgress>

*********************************************ASCX.CS**********************************************

//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  https://www.nopcommerce.com/License.aspx.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
//
/
14 年 前
Sorry just realised the end of this got cut off s to continue

*******************************ASCX.CS***********************************************

//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  https://www.nopcommerce.com/License.aspx.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
//
// Contributor(s): _______.
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Measures;
using NopSolutions.NopCommerce.BusinessLogic.Shipping;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Administration.Modules;
using NopSolutions.NopCommerce.Web.Templates.Shipping;

namespace NopSolutions.NopCommerce.Web.Administration.Shipping.TNT
{
    public partial class ConfigureShipping : BaseNopAdministrationUserControl, IConfigureShippingRateComputationMethodModule
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvTNT.Columns[1].HeaderText = string.Format("From [{0}]", MeasureManager.BaseWeightIn.Name);
                gvTNT.Columns[2].HeaderText = string.Format("To [{0}]", MeasureManager.BaseWeightIn.Name);
                gvTNT.Columns[5].HeaderText = "Charge amount";
                if (ShippingByWeightManager.CalculatePerWeightUnit)
                {
                    gvTNT.Columns[5].HeaderText += string.Format(" per {0}", MeasureManager.BaseWeightIn.Name);
                }

                FillDropDowns();
                BindData();
            }
        }

        private void FillDropDowns()
        {
            ddlShippingMethod.Items.Clear();
            ShippingMethodCollection shippingMethodCollection = ShippingMethodManager.GetAllShippingMethods();
            foreach (ShippingMethod shippingMethod in shippingMethodCollection)
            {
                ListItem item = new ListItem(shippingMethod.Name, shippingMethod.ShippingMethodID.ToString());
                ddlShippingMethod.Items.Add(item);
            }
        }

        private void BindData()
        {
            ShippingByWeightCollection shippingByWeightCollection = ShippingByWeightManager.GetAll();
            gvTNT.DataSource = shippingByWeightCollection;
            gvTNT.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                int shippingMethodID = int.Parse(this.ddlShippingMethod.SelectedItem.Value);
                
                ShippingByWeight shippingByWeight = ShippingByWeightManager.InsertShippingByWeight(shippingMethodID,
                    txtFrom.Value, txtTo.Value, false,
                    0, txtShippingChargeAmount.Value);

                BindData();
            }
            catch (Exception exc)
            {
                processAjaxError(exc);
            }
        }

        protected void gvTNT_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateShippingByWeight")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvTNT.Rows[index];

                HiddenField hfShippingByWeightID = row.FindControl("hfShippingByWeightID") as HiddenField;
                DropDownList ddlShippingMethod = row.FindControl("ddlShippingMethod") as DropDownList;
                DecimalTextBox txtFrom = row.FindControl("txtFrom") as DecimalTextBox;
                DecimalTextBox txtTo = row.FindControl("txtTo") as DecimalTextBox;
                CheckBox cbUsePercentage = row.FindControl("cbUsePercentage") as CheckBox;
                DecimalTextBox txtShippingChargePercentage = row.FindControl("txtShippingChargePercentage") as DecimalTextBox;
                DecimalTextBox txtShippingChargeAmount = row.FindControl("txtShippingChargeAmount") as DecimalTextBox;

                int shippingByWeightID = int.Parse(hfShippingByWeightID.Value);
                int shippingMethodID = int.Parse(ddlShippingMethod.SelectedItem.Value);
                ShippingByWeight shippingByWeight = ShippingByWeightManager.GetByID(shippingByWeightID);

                if (shippingByWeight != null)
                    ShippingByWeightManager.UpdateShippingByWeight(shippingByWeight.ShippingByWeightID,
                      shippingMethodID, txtFrom.Value, txtTo.Value, cbUsePercentage.Checked,
                      txtShippingChargePercentage.Value, txtShippingChargeAmount.Value);

                BindData();
            }
        }

        protected void gvTNT_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ShippingByWeight shippingByWeight = (ShippingByWeight)e.Row.DataItem;

                Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
                if (btnUpdate != null)
                    btnUpdate.CommandArgument = e.Row.RowIndex.ToString();

                DropDownList ddlShippingMethod = e.Row.FindControl("ddlShippingMethod") as DropDownList;
                ddlShippingMethod.Items.Clear();
                ShippingMethodCollection shippingMethodCollection = ShippingMethodManager.GetAllShippingMethods();
                foreach (ShippingMethod shippingMethod in shippingMethodCollection)
                {
                    ListItem item = new ListItem(shippingMethod.Name, shippingMethod.ShippingMethodID.ToString());
                    ddlShippingMethod.Items.Add(item);
                    if (shippingByWeight.ShippingMethodID == shippingMethod.ShippingMethodID)
                        item.Selected = true;
                }
            }
        }

        protected void gvTNT_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int shippingByWeightID = (int)gvTNT.DataKeys[e.RowIndex]["ShippingByWeightID"];
            ShippingByWeight shippingByWeight = ShippingByWeightManager.GetByID(shippingByWeightID);
            if (shippingByWeight != null)
            {
                ShippingByWeightManager.DeleteShippingByWeight(shippingByWeight.ShippingByWeightID);
                BindData();
            }
        }

        protected void processAjaxError(Exception exc)
        {
            ProcessException(exc, false);
            pnlError.Visible = true;
            lErrorTitle.Text = exc.Message;
        }

        public void Save()
        {

        }
    }
}
14 年 前
and ASCX.CS.DESIGNER just in case

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4200
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace NopSolutions.NopCommerce.Web.Administration.Shipping.TNT {
    
    
    public partial class ConfigureShipping {
        
        /// <summary>
        /// upShip control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.UpdatePanel upShip;
        
        /// <summary>
        /// pnlError control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Panel pnlError;
        
        /// <summary>
        /// lErrorTitle control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Literal lErrorTitle;
        
        /// <summary>
        /// gvTNT control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.GridView gvTNT;
        
        /// <summary>
        /// ddlShippingMethod control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.DropDownList ddlShippingMethod;
        
        /// <summary>
        /// txtFrom control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::NopSolutions.NopCommerce.Web.Administration.Modules.DecimalTextBox txtFrom;
        
        /// <summary>
        /// txtTo control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::NopSolutions.NopCommerce.Web.Administration.Modules.DecimalTextBox txtTo;
        
        /// <summary>
        /// txtShippingChargeAmount control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::NopSolutions.NopCommerce.Web.Administration.Modules.DecimalTextBox txtShippingChargeAmount;
        
        /// <summary>
        /// btnAdd control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button btnAdd;
        
        /// <summary>
        /// up1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.UpdateProgress up1;
    }
}
14 年 前
Now when i go to the admin section add the template using the following

configuration path template = Shipping\TNTConfigure\ConfigureShipping.ascx
class name = NopSolutions.NopCommerce.Shipping.Methods.TNTCM.TNTComputationMethod, Nop.Shipping.TNT

and when i go to the configuration tab, nothing appears?

can anyone tell me what i am doing wrong?
13 年 前
Hi, I am also going to try and implement a TNT shipping rates module. Did you have any success with yours?
13 年 前
flynny1st wrote:
Now when i go to the admin section add the template using the following

configuration path template = Shipping\TNTConfigure\ConfigureShipping.ascx
class name = NopSolutions.NopCommerce.Shipping.Methods.TNTCM.TNTComputationMethod, Nop.Shipping.TNT

and when i go to the configuration tab, nothing appears?

can anyone tell me what i am doing wrong?


Hi,

Did anyone ever resolve this issue?

We have created a custom Shipping module. The module was working fine, then after a server crash and restore the module calculates the shipping correctly at checkout but when we go to the configuration tab in admin - nothing appears??

Thanks in advance
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.