Adding Products Manually with Product ID

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 года назад
Hi everyone,
we are developing a plugin to import products into nopcommerce from  web service, using IProductService.InsertProduct(product)
Is it possible insert our product id?


foreach (Article articol in articols)
{
  //get product
  var product = _productService.GetProductBySku(articol.CODE);

  //check if product exist
  var isNew = (product == null);

  //create or update class product
  product ??= new Product();

  //default attributes
  product.ProductTypeId = (int)ProductType.SimpleProduct;
  product.VisibleIndividually = true;
  product.ManageInventoryMethodId = (int)ManageInventoryMethod.ManageStock;
  product.MarkAsNew = true;

  //add attributes
  product.Id = articol.ID; // IS IT POSSIBLE INSERT THIS ID?
  product.Sku = articol.CODE;
  product.Name = articol.CODE;
  product.ShortDescription = articol.DESCRIPTION : "";
  product.FullDescription = (traduzione != null) ? articol.DESCRIPTION : "";
  product.MetaKeywords = "";
  product.MetaDescription = "";
  product.MetaTitle = "";
  product.Price = 20;
  product.TaxCategoryId = _taxCategoryService.GetTaxCategoryById(1).Id;
  product.StockQuantity = 100;
  product.Length = 1;
  product.Height = 1;
  product.Width = 1;
  product.Weight = 1;
  product.Published = true;
  product.Deleted = false;

  //check if product is new
  if (isNew)
  {
    _productService.InsertProduct(product);
  }
  else
  {
    _productService.UpdateProduct(product);
  }
}
3 года назад
Why must you insert ID? Is SKU not good enough? :)
3 года назад
Hi Woon,
thank you for reply.
I would like to use the same ID as the management program, for compatibility. In a second moment also i'll export the orders to be return to  web service, and the program only accepts the id.
3 года назад
It may be possible, but would not be easy to do.  The Id field has the IDENTITY property.
It would be easier if you used some other field (e.g. SKU,  GTIN, or generic attribute), and then your web service would just send the correct value.
3 года назад
New York wrote:
It may be possible, but would not be easy to do.  The Id field has the IDENTITY property.
It would be easier if you used some other field (e.g. SKU,  GTIN, or generic attribute), and then your web service would just send the correct value.


Thank you for support. We are considering to insert "articol.ID" in generic attribute


//check if product is new
if (isNew)
{
  _productService.InsertProduct(product);

  //insert local id in generic
  _genericAttributeService.SaveAttribute<int>(product, "IdCodeLocal", articol.ID);
}
else
{
  _productService.UpdateProduct(product);
}


Is it possible get EntityId by Key and Value, like this?
var id = _genericAttributeService.GetAttributeByKeyValue("IdCodeLocal", articol.ID).EntityId;
3 года назад
Hi all,
finally i found the solution
https://www.nopcommerce.com/en/boards/topic/79840/get-items-from-generic-attribute-table

        

public virtual int GetEntityIdByKeyGroupKeyAndValue(string keyGroup, string key, string value)
        {
            var genericAttribute = _genericAttributeRepository.Table.Where(x => x.KeyGroup == keyGroup && x.Key == key && x.Value == value).FirstOrDefault();
            
            if (genericAttribute == null)
                return 0;

            return genericAttribute.EntityId;
        }
3 года назад
Just to confirm
This is not an answer to your original question but it is a way to reference your old ID
So on you Product Import you will be planning to write a new record to the Generic Attribute table so you can access the old ID value at a later time.
Or more specifically you can access the new product using the old Id via searching the Generic Attribute table
3 года назад
Yidna wrote:
Just to confirm
This is not an answer to your original question but it is a way to reference your old ID
So on you Product Import you will be planning to write a new record to the Generic Attribute table so you can access the old ID value at a later time.
Or more specifically you can access the new product using the old Id via searching the Generic Attribute table


Yes Yidna, you are right. Having noticed that use internal ID was not the correct procedure, we preferred to use Generic Attributes to synchronize products.
I have posted our solution to help others who will have the same problem
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.