Announcement

Collapse
No announcement yet.

Get assigned categories

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Get assigned categories

    Hey all.

    So I'm trying my hand at using MVT again ... and I'm struggling, again.

    The end goal -- On the Basket `BASK` Page,
    1. get the item code for each item in the basket
    2. get the assigned category of each item
    3. if it matches a specific category apply a coupon code to the basket.
    From the token list I see that there is a `l.settings:basket:items[1]:code`, which should get me the item code, spiffy.

    Then I hunt through the documentation and find this -- https://docs.miva.com/code-samples/assigned-categories

    And it generally seems to be in the ball park of what I'm looking for, but when I try to put it into my BASK page, modifying it to look for the local var that I mentioned earlier, I get no output and the item code disappears.

    Here's what the Basket Contents section looks like thus far (I'm sure it's a not pretty but it will be cleaned up, as soon as it actually functions)
    :: This code block has been removed as it is waaaaay too long and unwieldy. I have also started to completely redo the mvt code I did find for getting the code. ::

    The follow up posts will have the updated code, below.

    2019-04-15-0900 -- This was the solution to on specific issue --

    Originally posted by cpp-dev View Post
    When talking this feature out with one of my co-workers, he pointed out that I was over thinking this. I was engineering a solution which already probably existed in the platform, I just need to find the correct settings.

    The solution as it turns out was, a special price group. While it is a little tedious to select the products individually, it's not the end of the world, and it will be simple for anyone in the office to manage.

    I'm sure there will be a need for something like this in the future, but it will likely have a better trigger for it's application rather than this odd reverse lookup that I was contriving.
    2019-05-03-1202 -- Resurrecting this thread because the solution to this problem will still be useful to our store.
    Last edited by cpp-dev; 05-03-19, 11:03 AM. Reason: Resurrecting the thread because the issue still needs attention

    #2
    Hi,

    That's a complex situation you have there. Have you tried creating a block of code that does what you want before fitting it into the page architecture? That's what I do when I'm trying to get something like this to work. Once I'm seeing the data/output that I expect, then I fit it into the page.

    I also mention that because that's a lot of code to go through and most of the developers here are super busy, so by narrowing down the problem by removing extra page architecture you'll probably get more help (or even figure it out yourself.)

    First thing I'd check is this line:
    <mvt:do name="l.result" file="g.Module_Library_DB" value="CategoryList_Load_Offset_Product_Assigned(l .settings:basket:items[1]:product:code,0,'','',0,l.null, g.assigned_categories )" /> Is it returning anything?

    Is l.result null or a number?

    Does g.assigned_categories contain anything? Find out how many elements this way:

    Code:
    <mvt:eval expr="miva_array_elements(g.assigned_categories)" />
    Also, please, use l.settings:assigned_categories or l.settings:temp:assigned_categories. Ssometimes I'll use in a dummy structure like "temp" to make sure I have a "clean" space to put my data because it's dangerous to be using global variables unless absolutely necessary.

    Global vars: It's safer to use l.settings:some_var and display it as &mvt:some_var; rather than g.some_var and displaying it as &mvt:global:some_var; because if you don't set one of those and someone knows you are using a global, they can set it to something creative on the URL (or in a form post to that page) and hack your site, especially since you are not encoding the global variables (as in, &mvte:global:some_var;)

    Scot
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers

    Comment


      #3
      Scot - ScotsScripts.com That's fair. I knew it was a rather bulky code block. I'll work on trimming down. Probably do a bump post when I do.

      Comment


        #4
        Couple of basic things. When you see a variable like:

        l.settings: basket:items[1]:code

        You are looking at an 'array' variable. Array variables are variables that contain a LIST of value enumerated by the [x] number. Rarely, does one use the 'direct' reference of l.settings:basket:items[x]:whatever. Instead, with a 'foreach' iterations you can get the ONE of the listed items each time via 'l.settings:basket:items:whatever'.

        Also, don't use special encoding unless you know you really need it. I.e.,
        miva_csv_encode shouldn't be necessary as you are capturing product code values which will just be saved as this_code|that_code|whatever_code
        Bruce Golub
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment


          #5
          Big thanks to you Bruce - PhosphorMedia ! You helped breath some sanity into my coding fever dream. :high fives:

          So I did more hunting in the docs, and found the developer videos, which had a short one on the mvt:foreach -- https://docs.miva.com/videos/foreach-loops

          It reminded me that I completely over thinking my original approach.

          I now have the the following snippit that collects the codes for all basket items and it is embarrassingly more simple than what I was doing before.
          Code:
          <mvt:foreach iterator="item" array="basket:items">
          
            &mvt:item:code;
          
          </mvt:foreach>
          Yeah it's just a damn for loop. :facepalm: With the above code stuck in the header of the BASK page I see the codes written out in a line. So hey! That's progress.

          Comment


            #6
            Argh.
            So I can pull in the item code/sku. Excellent! Splendid!
            Now I'm looking though the docs for *anything* to do with categories, and I am not seeing anything. Are they not addressable from the MivaScript?
            OK, i was in the wrong section. I was looking at the MivaScript functions not the Miva API functions :facepalm:
            Last edited by cpp-dev; 04-12-19, 09:22 AM. Reason: I'm dumb

            Comment


              #7
              When talking this feature out with one of my co-workers, he pointed out that I was over thinking this. I was engineering a solution which already probably existed in the platform, I just need to find the correct settings.

              The solution as it turns out was, a special price group. While it is a little tedious to select the products individually, it's not the end of the world, and it will be simple for anyone in the office to manage.

              I'm sure there will be a need for something like this in the future, but it will likely have a better trigger for it's application rather than this odd reverse lookup that I was contriving.
              Last edited by cpp-dev; 04-15-19, 08:01 AM. Reason: A little grammar shift

              Comment


                #8
                Back again. I'm very frustrated by this.

                It seems like taking an item code and looking up the categories it's assigned to shouldn't be very odd or hard to do.

                I'm not much of a dev, the dev in the user name is more of a description of role than actual skill set. As such I'm sure I'm missing something.

                Maybe I craft an array of the categories, and when a product is added to the cart, the script loads each category and checks if the item exists in it? Seems like it would work, but it might not be very quick.

                Comment


                  #9
                  Code:
                   <mvt:do file="g.Module_Library_DB" name="l.success" value="ProductList_Load_Offset_Category_Assigned(cat_id, offset, search, searchable_fields, max, nextoffset var, products var)" />
                  This seems like a likely candidate for some tweaking.

                  It would seem that I could iterate over an array of 'hand coded' category ids (cat_id=category_var[i]), and use that to capture the assigned item codes, and then iterate over the BASK items to see if any of those codes are present.

                  Then when/if there is a match, apply a pre-defined discount/coupon.
                  Last edited by cpp-dev; 05-03-19, 11:43 AM.

                  Comment


                    #10
                    Very basically, are you trying to check to see if a product is in a particular category?
                    M.A.D.* since 1997

                    http://www.scotsscripts.com

                    *miva application developers

                    Comment


                      #11
                      That is the essence of it.
                      Have - item code > get - associated category > Confirm category matches expected
                      commit action based on confirmation

                      Comment


                        #12
                        Ok, so use this function:

                        Code:
                        <mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(cat_id, product_id, categoryxproduct var)" />
                        Give it a cat_id and a product_id and then if it finds the product in the category it will load up the reference variable, categoryxproduct.

                        Code:
                        <mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(l.settings:category:id, l.settings:product:id, l.settings:_catproduct)" />
                        Then test for l.settings:_catproduct which will have this structure:

                        Code:
                        l._catproduct:cat_id
                        l._catproduct:product_id
                        l._catproduct:disp_order
                        if the product is found in the category. If it's not, then the structure will be empty.
                        M.A.D.* since 1997

                        http://www.scotsscripts.com

                        *miva application developers

                        Comment


                          #13
                          Thank you for the guidance, Scot - ScotsScripts.com !

                          I think I have this setup correctly in the BASK > Header but I'm not seeing output. Which probably means that the category isn't quite right ...

                          Code:
                          <mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(atopica-cats, l.settings:product:code, l.settings:_catproduct)" />
                          
                          &mvt:settings:_catproduct:cat_id; <br>
                          &mvt:settings:_catproduct:product_id; <br>
                          &mvt:settings:_catproduct:disp_order; <br>

                          also tried the following config ...

                          Code:
                          <mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(atopica-cats, atopcat17, l.settings:_catproduct)" />
                          
                          &mvt:_catproduct:cat_id; <br>
                          &mvt:_catproduct:product_id; <br>
                          &mvt:_catproduct:disp_order; <br>

                          Comment


                            #14
                            What is atopica-cats? and atopat17? If they are variables they need to be scoped with l. or g.

                            If they are strings, then you won't get anything because they are not being presented as strings, meaning, you have to quote them like

                            'atopica-cats'

                            Next, they won't work quoted either because they are not ids. You need the id and if you don't have it you'll need to load it.

                            If you need help on finding the ids, I put up a short tutorial based on my post above that shows how to load the ids if you only have codes (examples for both product and category:)

                            https://www.scotsscripts.com/blog/mi...-category.html


                            M.A.D.* since 1997

                            http://www.scotsscripts.com

                            *miva application developers

                            Comment


                              #15
                              I'm sure I'm putting the "duh" in dumb, but I'm struggling to understand this.

                              Code:
                              <mvt:assign name="l.product_code" value="'atopcat17'" />
                              <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="'atopica-cats'" />
                              <mvt:do file="g.Module_Library_DB" name="l.success" value="Product_Load_Code(l.category_code, l.settings:category)" />
                              
                              
                              <mvt:do file="g.Module_Library_DB" name="l.success" value="CategoryXProduct_Load(l.settings:category:id, l.settings:product:id, l.settings:_catproduct)" />
                              
                              &mvt:l.settings:_catproduct:cat_id; <br>
                              &mvt:l.settings:_catproduct:product_id; <br>
                              &mvt:l.settings:_catproduct:disp_order; <br>
                              Attached Files

                              Comment

                              Working...
                              X