Miva Merchant API: How to Test if a Product is in a Category

Miva Merchant Modules and Development
Have you ever wanted to find out if a product is in a particular category? Here's how you can do that using the ever so nifty Miva Merchant API.

Miva Merchant API: How to Test if a Product is in a Category

The Miva Merchant API is a list of functions that you can use in store pages.


Have you ever wanted to find out if a product is in a particular category? Here's how you can do that using the ever so nifty Miva Merchant API.

by Scot Ranney • May 03, 2019

Miva Merchant, Miva Merchant Storemorph, Miva Scripting 101


The Miva Merchant API has a set of functions that start with CategoryXProduct that deal with handling what categories products are assigned to.

If you want to quickly find out if a product is assigned to a category, use the

<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(cat_id, product_id, categoryxproduct var)" />

function.

The cat_id is the category id, product_id is the product id, and categoryxproduct is the structure the data will be loaded into if the product exists in the category.

For example, if you've loaded some product data into l.settings:product and some category data into l.settings:category, you could check to see if the product is in the category this way:

<mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(l.settings:category:id, l.settings:product:id, l.settings:_catproduct)" />

Once you call this, you can check l.settings:_catproduct to see if the product was found in the category.

<mvt:if expr="l.settings:_catproduct:cat_id">
    do something here because the product was found in the category
<mvt:else>
    do something here because the product was not found in the category
</mvt:if>

If the product is found in the category, the l.settings:_catproduct structure will contain:

l._catproduct:cat_id
l._catproduct:product_id
l._catproduct:disp_order

What if you don't know the product or category id?

If you know the product code and the category code then use these category and product lookup via code functions. Put them above the CategoryXProduct_Load(...) function so the product and category data is available.

<mvt:assign name="l.product_code" value="'my_product_code'" />
<mvt:do file="g.Module_Library_DB" name="l.success" value="Product_Load_Code(l.product_code, l.settings:product)" />

<mvt:assign name="l.category_code" value="'my_category_code'" /> <mvt:do file="g.Module_Library_DB" name="l.success" value="Category_Load_Code(l.category_code, l.settings:category)" />

If you used a product code that exists in your store the product data will be in l.settings:product and the product id would be l.settings:product:id

Same thing goes for the category lookup.

Related Posts and Information


overall rating:
my rating: log in to rate

miva miva merchant mivascript stormorph tutorial

The blog posts on Scot's Scripts are made using by Scot's Blogger, a full featured Wordpress replacement Miva Merchant blogging module.