Skip to content

Instantly share code, notes, and snippets.

@bodia-uz
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save bodia-uz/5d5b6d3b7bc710bc83ad to your computer and use it in GitHub Desktop.

Select an option

Save bodia-uz/5d5b6d3b7bc710bc83ad to your computer and use it in GitHub Desktop.
// type your offers.xml path
var xmlUrlOrPath = @"D:\Work\parfumstudio\Exchange\offers_2014-10-10T02-08-30.xml";
var doc = XDocument.Load(xmlUrlOrPath);
// parse xml
var offers = doc.Descendants("Предложение")
.Where(o => o.Element("Штрихкод") != null)
//.Skip(400)
//.Take(15)
.Select(o => new {
SKU = o.Element("Штрихкод").Value,
Name = o.Element("Наименование").Value,
Price = decimal.Parse(o.Descendants("ЦенаЗаЕдиницу").First().Value.Trim(), System.Globalization.CultureInfo.InvariantCulture),
Stock = int.Parse(o.Element("Количество").Value)
});
//offers.Dump();
var offersSKU = offers.Select(o=>o.SKU).Distinct();
var part = 2000;
var cycles = (int)Math.Ceiling(offersSKU.Count()/(double)part);
for(var i = 0; i < cycles; i++ ){
// get variants that corespondend parsed from xml offers
var partOffers = offersSKU.Skip(i * part).Take(part);
var variants = Variants.Where(v => partOffers.Contains(v.SKU.Trim())).ToList();
var variantChanges = variants
.GroupBy(v=>offers.FirstOrDefault(o=>o.SKU==v.SKU.Trim()))
.Where(vg=>vg.Key != null)
.SelectMany(vg => vg.Select(v=>new{
Product = v.Product,
Variant = v,
vg.Key.SKU,
//ProductName = v.Product.Name,
VariantName = v.Name,
SitePrice = v.Price,
DbPrice = vg.Key.Price,
SiteAvailability = v.Availability,
DbAvailability = vg.Key.Stock > 0,
SiteStock = v.Stock,
DbStock = vg.Key.Stock,
//Url = "http://parfumstudio.ua/shop/" + v.Product.Url + ".html",
EditUrl = "http://parfumstudio.ua/admin/product/edit/" + v.Product.Id,
}))
// uncomment if want to show onlu changed variants
.Where(v=> v.DbPrice != v.SitePrice || v.DbAvailability != v.SiteAvailability)
.OrderBy(v=>v.Variant.ProductId)
;
// output variantChanges
variantChanges.Dump();
foreach(var change in variantChanges.Where(v=> v.DbPrice != v.SitePrice || v.DbAvailability != v.SiteAvailability)){
change.Product.Visible = change.DbAvailability;
change.Variant.Price = change.DbPrice;
change.Variant.Availability = change.DbAvailability;
change.Variant.Stock = change.DbStock;
}
// Uncomment to submit changes
SubmitChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment