Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
I'm trying to find the best way to force an affiliate value on only the shopper-side pages. If no affiliate is detected, redirect to a landing page.
I thought global.asax Begin_Request() was a good place to work but it's not going as planned.
Any suggestions?
|
|
|
|
Rank: Advanced Member
Groups: Admin, Administrators, Developers, Registered, HelpDesk, Authorized User Joined: 7/31/2019(UTC) Posts: 77
Was thanked: 8 time(s) in 8 post(s)
|
Global.asax Begin_Request() seems a good place to handle any affiliate related processing. Please explain what issues you are facing with it? You can check for an affiliate indicator in query string as under: Code:// look for an affiliate indicator in the querystring
Store store = AbleContext.Current.Store;
Affiliate affiliate = null;
int affiliateId = AlwaysConvert.ToInt(context.Request.QueryString[store.Settings.AffiliateParameterName]);
if (affiliateId > 0)
affiliate = AbleContext.Container.Resolve<IAffiliateRepository>().Load(affiliateId);
Please keep in mind that the affiliate related processing can only be done once user instance is initialized. So, you should place the following check around any such code: Code:
if (AbleContext.Current.User != null)
{
// any affiliate related processing.
}
|
|
|
|
|
Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Once I detect that I need to redirect to a no-affiliate page, I ran into a too-many-redirects problem. Since I was using BeginRequest(), it fires both for the initial request and the redirected page. So it just kept redirectly. I tried using a session variable to toggle a flag I could test for at the beginning of PageRequest(), but I don't think session variables are available consistently in BeginRequest() I did find an alternate solution in StackOverflow that seems to be working so far. The User context seems populated too at this point, so that concern is handled I think. First time I've ever seen this method used in Global.asax lol Code:
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
// don't redirect for admin users
if (!AbleContext.Current.User.IsAdmin)
{
// don't redirect admin pages
string origPath = Request.Path.ToLowerInvariant();
if (!origPath.Contains("/admin/"))
{
// only redirect if no affiliate assigned
if (AbleContext.Current.User.Affiliate == null)
{
bool redirected = false;
var beingredirected = HttpContext.Current.Session["beingredirected"];
if (beingredirected != null)
{
redirected = Boolean.Parse(beingredirected.ToString());
}
if (!redirected)
{
if (Request.HttpMethod == "GET")
{
HttpContext.Current.Session["beingredirected"] = "true";
Response.Redirect("~/No-Affiliate");
}
else if (Request.HttpMethod == "POST")
{
}
}
}
}
}
HttpContext.Current.Session["beingredirected"] = "false";
}
}
|
|
|
|
Rank: Advanced Member
Groups: Admin, Administrators, Developers, Registered, HelpDesk, Authorized User Joined: 7/31/2019(UTC) Posts: 77
Was thanked: 8 time(s) in 8 post(s)
|
Hi Joe, Instead using a session variable to toggle a flag, it seems that you should just add a new check to exclude the "/No-Affiliate" page from the redirect processing. something like: Code:if (origPath.Contains("/no-affiliate")) return;
Edited by user Wednesday, October 7, 2020 8:48:12 AM(UTC)
| Reason: Not specified |
|
1 user thanked Naveed Ashraf for this useful post.
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The AbleCommerce Forums uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close