CURD using nopCommerce plugin in Admin Area

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 years ago
Hello community i am new to the copCommerce world
can some one guide me is there any article or tutorial where we can learn the CRUD for the custom tables in database, and then perform the CRUD operation on those tables at admin area.

what i have done up till now

Created Plugin
Added Pages in Admin Area
2 years ago
Did you check these documentation?
If you need more help, please let me know.
https://docs.nopcommerce.com/en/developer/plugins/plugin-with-data-access.html
To add menu:
https://docs.nopcommerce.com/en/developer/plugins/menu-item.html
2 years ago
I think the docs are a bit weak in explaining that to do data operations you use services.  It's best to look at existing code  - e.g.
\Presentation\Nop.Web\Areas\Admin\Controllers\ProductController.cs

await _productService.InsertProductAsync(product);

var originalProduct = await _productService.GetProductByIdAsync(copyModel.Id);
var productBySku = await _productService.GetProductBySkuAsync(sku);

await _productService.UpdateProductAsync(product);

await _productService.DeleteProductAsync(product);
2 years ago
Team let me know where I am going wrong
this is what I am trying to achieve

This project is to achieve the calculation done for the courier boxes required for the order placed on nopCommerce

Example: The store owner will define the no of boxes in the admin area and the boxes product category, that which box belongs to which category of product.

On each order placement this plugin will calculate the no of boxes required for that particular order as per the width, length and depth of the box and the same variables define for the products.

for this purpose this plugin development has been break into the following.
1. Define Plugin
2. Create custom tables in the database for the following task
* Boxes (boxes will be define in this table)
* OrderBoxes (this table save the no of boxes qty for each order in rows)
3. Create Admin Area Menu For The Following Pages
* Add Box (Insert Box)
* Manage Boxes (List Box / Delete / Edit)
* Boxes Order List (this will have the quantity of boxes required for each order)

Here is my repository kindly check
https://github.com/UmairAhmedKhanPK/Nop.Plugin.Packaging.Boxes
2 years ago
I don't know that there's anyone here that is going to review / debug your entire code.  It's best to post your very specific problems.
2 years ago
my problem is I am unable to create custom tables in database through my plugin code
i am using latest nopCommerce version 4.40
here is what i have done

Domains Folder created classs PackagingBoxesRecords.cs

public partial class PackagingBoxesRecords : BaseEntity
    {
        //primary key
        public int BoxID { get; set; }
        //category id -- not a foregin key
        public int CategoryID { get; set; }
        public string BoxName { get; set; }
        public string BoxDetails { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public int Length { get; set; }
        public DateTime AddedDateTime { get; set; }
    }


In Data folder created class PackagingBoxesRecordBuilder.cs

public class PackagingBoxesRecordBuilder : NopEntityBuilder<PackagingBoxesRecords>
    {
        public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table
                //map the primary key (not necessary if it is Id field)
                .WithColumn(nameof(PackagingBoxesRecords.BoxID)).AsInt32().PrimaryKey()
                .WithColumn(nameof(PackagingBoxesRecords.CategoryID))
                .AsInt32()
                .Nullable()
                .WithColumn(nameof(PackagingBoxesRecords.BoxName))
                .AsString(1000)
                .WithColumn(nameof(PackagingBoxesRecords.BoxDetails))
                .AsString(1000)
                .Nullable()
                .WithColumn(nameof(PackagingBoxesRecords.Height))
                .AsInt32()
                .WithColumn(nameof(PackagingBoxesRecords.Width))
                .AsInt32()
                .WithColumn(nameof(PackagingBoxesRecords.Length))
                .AsInt32()
                .WithColumn(nameof(PackagingBoxesRecords.AddedDateTime))
                .AsDateTime();
        }
    }


In Data folder anther class SchemaMigration.cs

[SkipMigrationOnUpdate]
    [NopMigration("2021/11/26 12:20:55:1687541", "Packaging.Boxes base schema")]
    public class SchemaMigration : AutoReversingMigration
    {
        protected IMigrationManager _migrationManager;

        public SchemaMigration(IMigrationManager migrationManager)
        {
            _migrationManager = migrationManager;
        }

        public override void Up()
        {
            _migrationManager.BuildTable<PackagingBoxesRecords>(Create);
        }
    }


In Services Folder created interface IPackagingBoxesServices.cs

    public interface IPackagingBoxesServices
    {
        public interface IProductViewTrackerService
        {
            /// <summary>
            /// Logs the specified record.
            /// </summary>
            /// <param name="record">The record.</param>
            void Log(PackagingBoxesRecords record);
        }
    }

In Services Folder created interface PackagingBoxesServices.cs

    public class PackagingBoxesServices : IPackagingBoxesServices
    {
        private readonly IRepository<PackagingBoxesRecords> _packagingBoxesRecordsRepository;
        public PackagingBoxesServices(IRepository<PackagingBoxesRecords> packagingBoxesRecordsRepository)
        {
            _packagingBoxesRecordsRepository = packagingBoxesRecordsRepository;
        }

        /// <summary>
        /// Logs the specified record.
        /// </summary>
        /// <param name="record">The record.</param>
        public virtual void Log(PackagingBoxesRecords record)
        {
            if (record == null)
                throw new ArgumentNullException(nameof(record));
            _packagingBoxesRecordsRepository.Insert(record);
        }
    }


In Infrastructure folder created Dependency Injection class DependencyRegistrar.cs

public class DependencyRegistrar : IDependencyRegistrar
    {
        /// <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="appSettings">App settings</param>
        public virtual void Register(IServiceCollection services, ITypeFinder typeFinder, AppSettings appSettings)
        {
            services.AddScoped<IPackagingBoxesServices, PackagingBoxesServices>();
        }

        /// <summary>
        /// Order of this dependency registrar implementation
        /// </summary>
        public int Order => 1;
    }



Here is my code please identify where i am going wrong
2 years ago
It looks like you have all the bits
Does the Plugin install ?
Is there any error in the log ?

You could try setting some breakpoints in Visual Studio debugger to check if the Up method is actioned
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.