Getting categoryid within _ProductVariantAddToCart.cshtml

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 年 前
I need to be able to check if a product is in a certain category(s) and show or not show the buy button depending on whether it is or not.

I appear to have access to the ProductVariantId from within the Model on the page _ProductVariantAddToCart.cshtml but am not sure how to end up with a check along the lines of:

if(CategoryId == 23 || CategoryId == 27 )
{
    Then hide the buy button.
}


Anyone know of the best way to do this? Or, even better, if Nop allows this through the admin area? It has got to be ONLY for certain products within certain categories.

Thanks.
10 年 前
eoinm wrote:
I need to be able to check if a product is in a certain category(s) and show or not show the buy button depending on whether it is or not.

I appear to have access to the ProductVariantId from within the Model on the page _ProductVariantAddToCart.cshtml but am not sure how to end up with a check along the lines of:

if(CategoryId == 23 || CategoryId == 27 )
{
    Then hide the buy button.
}


Anyone know of the best way to do this? Or, even better, if Nop allows this through the admin area? It has got to be ONLY for certain products within certain categories.

Thanks.


So you have the same products in different categories? :)
10 年 前
Well I have products within, for example, Category B which is itself a subcategory of Category A. So, I don't know if all I need to do is check for Category A and that will cover all products within subcategories of Category A. I doubt this is the case though so I'm anticipating that I'll need to check if they're in either category.
10 年 前
eoinm wrote:
Well I have products within, for example, Category B which is itself a subcategory of Category A. So, I don't know if all I need to do is check for Category A and that will cover all products within subcategories of Category A. I doubt this is the case though so I'm anticipating that I'll need to check if they're in either category.


If I am not wrong, a product in sub-category B is not automatically a product of category A if you don't specify it to be so. :)
10 年 前
To give a little more detail:


What I actually really need is the ability to hide the price and buy button if the product is being displayed outside of a certain country. I'll need to do this based on an IP check, so if you're from Country X you can see the full catalogue of products but you can't buy or see the price of anything within certain categories.

I don't have access to an IP database yet so I need an interim solution which involves checking if a product is in any of the special categories and then hiding the price and buy button accordingly, for everyone.

I don't think Nop allows for anything like this so I'll need to code for it, which is what I'm trying to do.

Or, if anyone can think of a way I can do what I really need to do then please suggest it as I'll eventually have to go down that route.
10 年 前
Just updating my own question here as I might have a solution that could be quite easy. If I do a check like this:

static public string GetCountry()
{
    return new WebClient().DownloadString("http://api.hostip.info/country.php");
}

which will return the country code of the country the user is in, if it's not country X then hide the button.

This might work.
10 年 前
That country look up works but is only half the issue as I still need to know what category is displaying.

So original question still stands, how can I get the category of the current product being displayed when I'm on a page like _ProductVariantAddToCart.cshtml ?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.