Page 1 of 1

Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 13, 2015 6:23 am
by mbartens
I have a site in which users can purchase subscriptions. The subscriptions will give specific member prices and some products will be member only.

I created a User Group and gave it Authorized User permissions.
Next, I created a Subscription product and assigned it to the previously created subscription group.
Then I create a product and select Group Restrictions: Enabled. Only _Default appears in this box.
I go back to the subscription product that I created and the group has now disappeared from the drop down.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 13, 2015 11:26 am
by Katie
May,

I'm having some trouble following the steps to reproduce.

Question - You created a group and gave it Authorized User permission, then you say this group did not appear on the product editing page where you can enable group restrictions. Now, here is where I have to wonder if the group you created might have additional permissions set? When I tested this, the new group I created appears for the product. So, let's work through this issue first and then we can address any other problems.

Thanks
Katie

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Mon Mar 16, 2015 3:49 am
by mbartens
The user group I created only had Authorized permissions.

I went back today and tried the process over but didn't even get as far.
Here's what I did:



1. Create user group and give Authorized User Permissions

2. Create Product
Name: Test Membership
Price: 30.00
Shippable: no

3. Click Save and Edit

4.Add subscription plan:
Subscription Name: Test Membership
Subscriber Group: Group I created in step 1 is not there

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Mon Mar 16, 2015 11:30 am
by Katie
If you remove the 'Authorized User' permissions from the group, it should work.

I need to ask the dev team why this group has been restricted from the listing. There may be a reason, but I'm not sure as of yet.

If you use this group exclusively for subscriptions/member access to a product, then it shouldn't be a problem to remove the permission.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 20, 2015 1:34 am
by mbartens
If I remove it from the Authorized User role, it is in the drop down under subscriptions.
However, if I go into a product and enable Group Restrictions it is not in the list box.
This is an important feature for this site, how can this be fixed? Thank you.

ETA: The code that brings up that list box actually says: LoadNonSubscriptionGroups

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 20, 2015 4:53 am
by nadeem
This 'LoadNonSubscriptionGroups' (This is in the backend code) method should be modified (or a new method can be added) so that it should 'LoadNonAdminGroups' for the store irrespective of subscription/non-subscriptions groups, something like this:

Code: Select all

public IList<Group> LoadNonAdminGroups()
{
    IList<Group> groups = NHibernateHelper.CreateCriteria<Group>()
    .Add(Restrictions.Eq("Store.Id", AbleContext.Current.StoreId))
    .AddOrder(Order.Asc("Name"))
    .List<Group>();

   // ALL NON ADMIN GROUPS
   return groups.FindAll(grp => !grp.IsInRole(Role.AllAdminRoles)) as IList<Group>;
}
One more update is required to make it work properly that is:

Locate the BindSubscriptionGroup() method on Admin/Products/EditSubscription.aspx.cs and replace with the below code:

Code: Select all

private void BindSubscriptionGroup()
 {
     SubscriptionGroup.Items.Clear();
     SubscriptionGroup.Items.Add(new ListItem(string.Empty));
     IList<CommerceBuilder.Users.Group> groupCol = GroupDataSource.LoadAll("Name");

     CommerceBuilder.Users.Group group;
     for (int i = groupCol.Count - 1; i >= 0; i--)
     {
         group = groupCol[i];
         if (group.Roles.Count > 0 && group.IsInRole(Role.AllAdminRoles) || Group.DefaultUserGroupName.Equals(group.Name))
         {
              groupCol.RemoveAt(i);
         }
     }
            SubscriptionGroup.DataSource = groupCol;
            SubscriptionGroup.DataBind();
}

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 20, 2015 9:10 am
by mbartens
Thank you. Another issue I'm realizing though... I want everyone to see this product - but only members can purchase it. I figured on the front end I could just hide the add to cart button if the user doesn't have a subscription. Enabling group restrictions on the product and associated with the subscriber groups makes the products not show for anon and no subscribers.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Fri Mar 20, 2015 9:53 am
by Katie
I want everyone to see this product - but only members can purchase it
I'm sorry May, but this will require a customization. The product membership feature limits who can see (and purchase) the product. The subscription group feature simply adds the purchaser to the applicable group and removes them when the subscription expires.

I would guess that the customization would be relatively simple though. You are just going to want to remove the code that decides to show products based on membership groups, and make it specific to the Add to Cart buttons.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Mon Mar 23, 2015 5:27 am
by mbartens
What I did was add a bit column to ac_products called isVisibleToAll. So far it works ok for the SQL queries:'

Code: Select all

                if (user.UserGroups.Count > 0)
                {
                    string groups = string.Join(",", (from ug in AbleContext.Current.User.UserGroups select ug.GroupId).ToList<int>().ToArray());

                    selectQuery.Append(" AND ( P.EnableGroups = 0 OR (EXISTS(SELECT PG.GroupId FROM ac_ProductGroups AS PG WHERE PG.ProductId = P.ProductId AND PG.GroupId IN (" + groups + ")) OR P.isVisibleToAll=1)) ");
                }
                else
                {
                    selectQuery.Append(" AND (P.EnableGroups = 0 OR P.isVisibleToAll=1) ");
                }
I saw it wasn't working for the featured products though so I'm looking at ProductRepository, GetFeaturedProducts.
I'm a bit lost with this syntax... how do I add "OR" ?

Code: Select all

                if (user.UserGroups.Count > 0)
                {
                    Disjunction disjunction = new Disjunction();
                    int[] groupIds = (from ug in user.UserGroups select ug.GroupId).ToList<int>().ToArray();
                    DetachedCriteria secondCriteria = DetachedCriteria.For<ProductGroup>("PG")
                        .Add(Restrictions.EqProperty("PG.Product.Id", "P.Id"))
                        .Add(Restrictions.In("PG.Group.Id", groupIds))
                        .SetProjection(Projections.Property("PG.Group.Id"));

                    disjunction.Add(Restrictions.Eq("P.EnableGroups", false));
                    disjunction.Add(Subqueries.Exists(secondCriteria));
                    criteria.Add(disjunction);
                }
                else
                {
                    criteria.Add(Restrictions.Eq("P.EnableGroups", false));
                }

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Mon Mar 23, 2015 10:15 pm
by nadeem
Disjunction takes the criterion and ORs them together. Use the disjunction to add an OR and then add it to criteria something like this for example:

Code: Select all

disjunction.Add(Restrictions.Eq("P.EnableGroups", false));
disjunction.Add(Restrictions.Eq("P.isVisibleToAll", true));
criteria.Add(disjunction);
Here, the above code will ORs the EnableGroups and isVisibleToAll and then added to the main criteria query i.e.

Code: Select all

P.EnableGroups = false OR P.isVisibleToAll = true
Hope this helps.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Mon Mar 23, 2015 10:51 pm
by nadeem
BTW, make customizations/updates keeping upgrades in mind. I suggest not to push yourself to a situation where the future upgrades get harder for you.

Re: Issue with Product Group Restrictions and Subscriptions

Posted: Wed Mar 25, 2015 3:39 am
by mbartens
Thank you so much for your help. I'm trying not to make too many changes but unfortunately they are necessary.