Ucommerce 2.0.0, how i can get bundles through API using c#
Hello, dear uCommerce team.
I was investigating your Marketing libraries, but i can't find a way, how i can get discount bundles using API.
I wont to get all bundles which are added in umbraco.
So basicly i need to get list of Compaigns, i had found some MarketingService class, but i can't create instance of that class, i can't find apropriate parameters to pass them in constructor.
Hope you will help me, and if its possible, please provide some example.
Looking into this it seems that we forgot to add a mapping to that particular field. That's the reason for the error you're seeing. What you can do instead is use the CampaignItem property to load the targets like so:
foreach(Target target in Target.Find(x => x.CampaignItem == item).ToList())
Thank you for your reply. Now it works without error. But can you explain for me how i can get products by the target or targetId? Or even, maybe i am going not in the right way. Basicly i wont to get all products which are in Bundle Act. I had spent few days investigating entity v2 without luck. I hope you will help me.
It's a pretty complicated domain to make sense of without docs (we are working on them), but here's how you get the targets for a campaignitem:
var targetAggregator = ObjectFactory.Instance.Resolve(); var targets = targetAggregator.GetApplyTargets(campaignItem); foreach (var genericTarget in targets.Where(x => x is ProductTarget) { var productTarget = genericTarget as ProductTarget; // product info available in target from properties Sku and VariantSku }
Targets are actually inherited to specific instances so you'll have to downcast to the specific type you want, in this case the ProductTarget.
I couldn't make your code working, first i decided i am using old version of uCommerce - 2.0.0, i had installed version 2.0.1, but still without luck, so i made some changes, which works for me, i can get product name and id now:
IQueryable<Campaign> campaigns = Campaign.All(); foreach (Campaign camp in campaigns) { foreach (CampaignItem item in camp.CampaignItems) { if (item.Name.ToLower() == "bundle discount") {
IList<Target> targets = Target.Find(x => x.CampaignItem == item); foreach (var genericTarget in targets.Where(x => x is ProductTarget)) { var productTarget = genericTarget as ProductTarget; litInfo2.Text += "Product name:" + productTarget.Sku + " Product Id:" + Product.SingleOrDefault(p => p.Name == productTarget.Sku).Id.ToString() + "<br/>";
// product info available in target from properties Sku and VariantSku } } }
Thanks for you very much :-) You really putted me on right way ;-) Best regards.
But how can i get award value? I mean some discount price, i can't see some properties related to that in Award class. Maybe i should use some another logic?
Ucommerce 2.0.0, how i can get bundles through API using c#
Hello, dear uCommerce team.
I was investigating your Marketing libraries, but i can't find a way, how i can get discount bundles using API.
I wont to get all bundles which are added in umbraco.
So basicly i need to get list of Compaigns, i had found some MarketingService class, but i can't create instance of that class, i can't find apropriate parameters to pass them in constructor.
Hope you will help me, and if its possible, please provide some example.
Thanks and best regards.
I made mistake, i can get all Compaigns, and Compaign items, but i can't find a way how to get products which are in compaign item.
this is where i had come:
IQueryable<Campaign> campaigns = Campaign.All();
foreach (Campaign camp in campaigns)
{
foreach (CampaignItem item in camp.CampaignItems)
{
if (item.Name.ToLower() == "bundle discount")
{
foreach(Target target in Target.Find(x => x.CampaignItemId == item.Id).ToList()) //here i am getting error....
{
litInfo2.Text += "<br/>" + target.TargetId;
}
}
}
}
This is error message:
could not resolve property: CampaignItemId of: UCommerce.EntitiesV2.Target [.Where[UCommerce.EntitiesV2.Target](.Select[UCommerce.EntitiesV2.Target,UCommerce.EntitiesV2.Target](NHibernate.Linq.NhQueryable`1[UCommerce.EntitiesV2.Target], Quote((entity, ) => (entity)), ), Quote((x, ) => (Equal(x.CampaignItemId, p1))), )]
Hi Igor,
Looking into this it seems that we forgot to add a mapping to that particular field. That's the reason for the error you're seeing. What you can do instead is use the CampaignItem property to load the targets like so:
foreach(Target target in Target.Find(x => x.CampaignItem == item).ToList())
Thank you for your reply. Now it works without error. But can you explain for me how i can get products by the target or targetId? Or even, maybe i am going not in the right way. Basicly i wont to get all products which are in Bundle Act. I had spent few days investigating entity v2 without luck. I hope you will help me.
Hi Igor,
It's a pretty complicated domain to make sense of without docs (we are working on them), but here's how you get the targets for a campaignitem:
Targets are actually inherited to specific instances so you'll have to downcast to the specific type you want, in this case the ProductTarget.
Hope it makes sense.
I couldn't make your code working, first i decided i am using old version of uCommerce - 2.0.0, i had installed version 2.0.1, but still without luck, so i made some changes, which works for me, i can get product name and id now:
IQueryable<Campaign> campaigns = Campaign.All();foreach (Campaign camp in campaigns)
{
foreach (CampaignItem item in camp.CampaignItems)
{
if (item.Name.ToLower() == "bundle discount")
{
IList<Target> targets = Target.Find(x => x.CampaignItem == item);
foreach (var genericTarget in targets.Where(x => x is ProductTarget))
{
var productTarget = genericTarget as ProductTarget;
litInfo2.Text += "Product name:" + productTarget.Sku + " Product Id:"
+ Product.SingleOrDefault(p => p.Name == productTarget.Sku).Id.ToString() + "<br/>";
// product info available in target from properties Sku and VariantSku
}
}
}
Thanks for you very much :-) You really putted me on right way ;-) Best regards.
Cool. BTW I noticed an opportunity to optimize the perf of your code a bit.
You can bunch up these two lines into a single call:
foreach (CampaignItem item in camp.CampaignItems)
{
if (item.Name.ToLower() == "bundle discount")
Instead try this:
foreach (CampaignItem item in camp.CampaignItems.Where(x => x.Name.ToLower() == "bundle discount"))
Thanks. Nice optimization. But i had 1 more clearing question.
How can i get Award value of bundle?
As i understood i should downcast target to award? But, still, i can't find some property in Award class, where award value palced.
Can you help me with that?
Awards are different from targets. So you want to grab those in the same way you're loading up targets.
Like Targets there are multiple concrete implementations of the award depending on how it's configured.
But how can i get award value? I mean some discount price, i can't see some properties related to that in Award class. Maybe i should use some another logic?
Hi Igor,
You have to downcast the Award to a specific one. I've attached a screenshot of the concrete types.
Thanks, it helped me to figure out. Much appreciate.
is working on a reply...