Introduction
In this tutorial, we will learn about updating the price for a product on Amazon using the Feeds API in C#. Amazon allows us to upload inventory and order data using the Feeds API. The Feeds API provided by Amazon is very easy to use as we have to upload the XML file in the Feed and proper FeedType for the action we have to do.
Prerequisites
- Knowledge of C#
- You must have Amazon credentials like SellerId, Access Key, etc.
We should have DLL files should be installed using the Nuget package manager.
Now let’s get started…
First, create a view model for the repricer.
- public class AmazonRepricerViewModel
- {
- public string Sku { get; set; }
- public decimal Price { get; set; }
- public bool isLive { get; set; }
- }
- public void RepriceParticularAmazonProducts(AmazonRepricerViewModel vm)
- {
- StreamWriter InputFile, OutputFile, InventoryFile;
- var guid = Guid.NewGuid();
- var guid1 = Guid.NewGuid();
- var guid2 = Guid.NewGuid();
- //Input files
- var uploadRootFolderInput = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Input Files";
- Directory.CreateDirectory(uploadRootFolderInput);
- var directoryFullPathInput = uploadRootFolderInput;
- if (directoryFullPathInput == null)
- {
- CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");
- return;
- }
- string path = Path.Combine(directoryFullPathInput, "PriceUpdateAmazonItem_" + guid + ".txt");
- //Output files
- var uploadRootFolderOutput = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Output Files";
- Directory.CreateDirectory(uploadRootFolderOutput);
- var directoryFullPathOutput = uploadRootFolderOutput;
- if (directoryFullPathOutput == null)
- {
- CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");
- return;
- }
- string path1 = Path.Combine(directoryFullPathOutput, "PriceUpdateAmazonItem_" + guid1 + ".txt");
- //feed files
- var uploadRootFolderFeed = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Feed Files";
- Directory.CreateDirectory(uploadRootFolderFeed);
- var directoryFullPathFeed = uploadRootFolderFeed;
- if (directoryFullPathFeed == null)
- {
- CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");
- return;
- }
- string path2 = Path.Combine(directoryFullPathFeed, "PriceUpdateAmazonItem_" + guid2 + ".txt");
- InputFile = File.CreateText(path);
- OutputFile = File.CreateText(path1);
- InventoryFile = File.CreateText(path2);
- try
- {
- StringBuilder str1 = new StringBuilder();
- str1.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\t");
- str1.AppendLine();
- str1.Append("<AmazonItemRequest xmlns=\"urn: Amazon:apis: eBLBaseComponents\">\t");
- str1.AppendLine();
- str1.Append("<AmazonUser>" + "Do Not Use" + "</AmazonUser>\t");
- str1.AppendLine();
- str1.Append("<SKU>" + vm.Sku + "</SKU>\t");
- str1.AppendLine();
- str1.Append("<Price>" + vm.Price + "</Price>\t");
- str1.AppendLine();
- str1.Append("<Date>" + DateTime.Now.ToString() + "</Date>\t");
- str1.AppendLine();
- str1.Append("</AmazonItemRequest>\t");
- str1.AppendLine();
- InputFile.Write(str1.ToString());
- InputFile.Close();
- StringBuilder str = new StringBuilder();
- str.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\t");
- str.AppendLine();
- str.Append("<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amzn-envelope.xsd\">\t");
- str.AppendLine();
- str.Append("<Header>\t");
- str.AppendLine();
- str.Append("<DocumentVersion>1.01</DocumentVersion>\t");
- str.AppendLine();
- str.Append("<MerchantIdentifier>" + _accountId + "</MerchantIdentifier>\t");
- str.AppendLine();
- str.Append("</Header>\t");
- str.AppendLine();
- str.Append("<MessageType>Price</MessageType>\t");
- str.AppendLine();
- str.Append("<Message>\t");
- str.AppendLine();
- str.Append("<MessageID>1</MessageID>\t");
- str.AppendLine();
- str.Append("<OperationType>Update</OperationType>\t");
- str.AppendLine();
- str.Append("<Price>\t");
- str.AppendLine();
- str.Append("<SKU>" + vm.Sku + "</SKU>\t");
- str.AppendLine();
- str.Append("<StandardPrice currency=\"USD\">" + vm.Price + "</StandardPrice>\t");
- str.AppendLine();
- str.Append("</Price>\t");
- str.AppendLine();
- str.Append("</Message>\t");
- str.AppendLine();
- str.Append("</AmazonEnvelope>\t");
- str.AppendLine();
- InventoryFile.Write(str.ToString());
- InventoryFile.Close();
- var response = AmazonPriceUpdate(path2);
- OutputFile.Write("Feed Submitted Successfully, Your submission id is " + response);
- OutputFile.Close();
- }
- catch (WebException ex)
- {
- string exMessage = ex.Message;
- if (ex.Response != null)
- {
- using (var responseReader = new StreamReader(ex.Response.GetResponseStream()))
- {
- exMessage = responseReader.ReadToEnd();
- }
- }
- OutputFile.Write(exMessage.ToString());
- OutputFile.Close();
- }
- }

Mimi OMalleyPosted Sep 17, 2019, 1:10 AM
Do you have a snippet i can use to set a quantity discount?