Announcement

Collapse
No announcement yet.

Target Last Product Added to Cart

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

    Target Last Product Added to Cart

    Wondering if anyone has sucessfully targeted the last product that was added to the cart even when products match up and the product quantity/subtotal is the only thing that changes in the cart?

    Is there a variable I'm missing to target that via template code?

    The only variable I can see that will differentiate products on BASK/Global Mini Bask with the same CODE or ID would be &mvt:group:group_id;.

    I'm not quite sure how to connect the product added to the group_id it would be grouped with or if that is even possible. There is no group_id submitted when added to the cart.

    Add to cart form is ajaxed. (Suivant Theme)

    The Scenario:
    1. Customer adds T-shirt 1: Size M, Qty: 1
    2. Customer adds T-shirt 1: Size L, Qty: 1
    3. Customer adds T-shirt 1: Size M, Qty: 1

    The Cart:
    1. T-shirt 1: Size: M, Qty: 2 (last Item Added)
    2. T-shirt 1: Size L, Qty: 1
    Nick Harkins
    www.loveisarose.com

    #2
    You could roll through the basket items and see what item has the largest basketitems:line_id number.

    Assuming the items array is l.settings:basket:items (or basket:groups) - you may have to manually load it based on g.basket:basket_id. A quick way to find the most recently added item would be to find the highest line_id.

    <mvt:foreach iterator="item" array="basket:items">
    <mvt:if expr="l.item:line_id GT l.temp_item:line_id">
    <mvt:assign name="l.temp_item" value="l.item:line_id" />
    </mvt:if>
    </mvt:foreach>

    When the loop is done l.temp_item will be the one you want.

    Sorry this isn't formatted for code, for some reason the text area here has no menu for formatting right now.
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers

    Comment


      #3
      Originally posted by Scot - ScotsScripts.com View Post
      You could roll through the basket items and see what item has the largest basketitems:line_id number.

      Assuming the items array is l.settings:basket:items (or basket:groups) - you may have to manually load it based on g.basket:basket_id. A quick way to find the most recently added item would be to find the highest line_id.

      <mvt:foreach iterator="item" array="basket:items">
      <mvt:if expr="l.item:line_id GT l.temp_item:line_id">
      <mvt:assign name="l.temp_item" value="l.item:line_id" />
      </mvt:if>
      </mvt:foreach>

      When the loop is done l.temp_item will be the one you want.

      Sorry this isn't formatted for code, for some reason the text area here has no menu for formatting right now.

      Scot - ScotsScripts.com, no need for menu, just wrap "code" and "/code" using open and close brackets [ ] between the snippet similar to html less and greater symbols.

      Example:

      Code:
      snippet
      Last edited by William Davis; 03-14-23, 09:40 AM.
      Thank you, Bill Davis

      Comment


        #4
        Thanks for the snippet Scot - ScotsScripts.com .

        I'm having trouble understanding it though.

        Wouldn't l.item:line_id always be greater than l.temp_item:line_id since l.temp_item:line_id has no value until it is assigned within that conditional?

        Using it as is unfortunatley just shows the first item in the basket regardless or whether its the same product added or not.

        Nick Harkins
        www.loveisarose.com

        Comment


          #5
          What do you mean by "target?" What is it that you're trying to accomplish?
          Kent Multer
          Magic Metal Productions
          http://TheMagicM.com
          * Web developer/designer
          * E-commerce and Miva
          * Author, The Official Miva Web Scripting Book -- available on-line:
          http://www.amazon.com/exec/obidos/IS...icmetalproducA

          Comment


            #6
            Yes, l.item:line_id could very well always be greater since the loop probably goes from first to last added to the cart. The highest line_id will be the last item added to the cart. You could just assign the last item of the items array like <mvt:assign name="l.temp_item" value="l.items(miva_array_elements(l.items))" /> as well, but I don't know if the last item is always going to be a high line_id which is why I used the loop - that way you KNOW l.temp_item is going to have the highest line_id.
            M.A.D.* since 1997

            http://www.scotsscripts.com

            *miva application developers

            Comment


              #7
              Originally posted by Scot - ScotsScripts.com View Post
              Yes, l.item:line_id could very well always be greater since the loop probably goes from first to last added to the cart. The highest line_id will be the last item added to the cart. You could just assign the last item of the items array like <mvt:assign name="l.temp_item" value="l.items(miva_array_elements(l.items))" /> as well, but I don't know if the last item is always going to be a high line_id which is why I used the loop - that way you KNOW l.temp_item is going to have the highest line_id.
              Actually, I don't think that's correct. Looking at Nick's scenario from the first post, the third item will be "added" to the cart by changing the quantity of the first item. So the highest line ID will be found on the Large shirt, not on the two Mediums. The line ID doesn't always tell you what is the last thing that the customer clicked the Add To Cart button for.
              Kent Multer
              Magic Metal Productions
              http://TheMagicM.com
              * Web developer/designer
              * E-commerce and Miva
              * Author, The Official Miva Web Scripting Book -- available on-line:
              http://www.amazon.com/exec/obidos/IS...icmetalproducA

              Comment


                #8
                Oh, right, they create new basket items sometimes when the basket is manipulated. Cancel all that stuff I said earlier :)

                So another way would be to use custom basket fields to log items that are added to the cart with a time stamp (s.dyn_time_t) and reference it. more template coding involved but pretty bomb proof.
                M.A.D.* since 1997

                http://www.scotsscripts.com

                *miva application developers

                Comment


                  #9
                  Hey Kent Multer I would like to show just the product and it's selected attributes that was just added to the cart in it's own standalone "modal" after the ajax add to cart success that is built into the Suivant Theme.

                  I believe the ajax call loads the BASK screen asynchronously and then selects the #js-mini-basket-container element to refresh/reload it's contents on success.

                  So within the global mini basket, I'm trying to use a second <mvt:foreach iterator="item" array="global_minibasket:items"> that will only show the last item added using the snippet provided by Scot - ScotsScripts.com .

                  Code:
                  <!-- first loop for minibasket (displays all items) -->
                  <mvt:foreach iterator="item" array="global_minibasket:items">
                      <mvt:if expr="l.item:line_id GT l.temp_item:line_id">
                          <mvt:assign name="l.temp_item:line_id" value="l.item:line_id" />
                      </mvt:if>
                  
                      rest of mini bask code
                  
                  </mvt:foreach>
                  
                  
                  <!-- second loop for minibasket (only display last item added) -->
                  <mvt:foreach iterator="item" array="global_minibasket:items">
                      <mvt:if expr="l.temp_item">
                  
                          mini bask code for just the last item
                  
                          <mvt:foreachstop />
                  
                      </mvt:if>
                  </mvt:foreach>
                  This is contained in a hidden div that will open after the ajax success using the magnific popup plugin (also built into Suivant).

                  What I am seeing is that when the same product is added to the cart a second time, the line_id does not change. So it looks like the highest line_id would not be reliable when the same product is added to the cart a second time.
                  Nick Harkins
                  www.loveisarose.com

                  Comment


                    #10
                    Come to think of it, now that my memory has been jogged, I seem to recall that the situation can be more complex than that. I don't recall the details, but I think there are cases where the store will change line IDs, or replace old items with new ones, in ways that are harder to track. This may be related to basket groups and/or certain types of price-group discounts. Sorry I can't be more specific. Maybe someone from in-house can give us some more info?

                    When writing modules, my approach has been to use the SystemModule_Action function to make a snapshot of the items in the basket when an ADPR action comes in. Then I use LogModule_Action to make another snapshot after the ADPR is completed, and compare the two snapshots to see what changed.

                    I have a small utility module that can be used to track the ADPR actions. Nick, if you PM or email me, I may be able to provide a solution for this. I'll probably need to have a look at your page templates to see how it can be done.
                    Kent Multer
                    Magic Metal Productions
                    http://TheMagicM.com
                    * Web developer/designer
                    * E-commerce and Miva
                    * Author, The Official Miva Web Scripting Book -- available on-line:
                    http://www.amazon.com/exec/obidos/IS...icmetalproducA

                    Comment


                      #11
                      Kent Multer No worries on being more specific. There's definitely stuff that's happening behind the scenes with this scenario that is not easy to track.

                      I tried the custom field approach suggested by Scot - ScotsScripts.com (using code borrowed from SteveoSoule 's Recently Viewed Products Code) but was running into the same issues.

                      Code:
                      <mvt:item name="customfields" param="Read_Basket('recently_added', l.settings:recently_added:product_codes)" />
                      
                      <mvt:assign name="l.settings:recently_added:product_codes" value="miva_array_deserialize( l.settings:recently_added:product_codes )" />
                      
                      <mvt:if expr="g.Action EQ 'ADPR' AND l.settings:page:code EQ 'BASK'">
                      
                          <mvt:assign name="g.time_added" value="s.dyn_time_t" />
                      
                          <mvt:assign name="l.insert_index" value="miva_array_insert( l.settings:recently_added:product_codes, g.Product_Code $ g.time_added, 1 )" />
                      
                          <mvt:assign name="l.settings:recently_added:product_count" value="miva_array_elements( l.settings:recently_added:product_codes )" />
                      
                          <mvt:item name="customfields" param="Write_Basket('recently_added', miva_array_serialize(l.settings:recently_added:product_codes))" />
                      
                      </mvt:if>
                      
                      <mvt:comment> Uncomment to View Raw Data Collected
                          &mvt:recently_added:product_codes;
                      </mvt:comment>
                      
                      <mvt:item name="customfields" param="Read_Basket( 'line_id_time_added', l.settings:line_id:time_added )" />
                      
                      <mvt:assign name="l.settings:line_id:time_added" value="miva_array_deserialize( l.settings:line_id:time_added )" />
                      
                      
                      <!-- first loop for minibasket (displays all items) -->
                      <mvt:foreach iterator="item" array="global_minibasket:items">
                      
                          <mvt:if expr="g.Action EQ 'ADPR' AND l.settings:page:code EQ 'BASK'">
                      
                              <mvt:assign name="l.insert_index" value="miva_array_insert( l.settings:line_id:time_added, l.settings:item:line_id $ '_' $ g.time_added, 1 )" />
                      
                              <mvt:assign name="l.settings:line_id:time_added_count" value="miva_array_elements( l.settings:line_id:time_added )" />
                      
                              <mvt:item name="customfields" param="Write_Basket('line_id_time_added', miva_array_serialize( l.settings:line_id:time_added ))" />
                      
                          </mvt:if>
                      
                          rest of mini bask item code
                      
                      </mvt:foreach>
                      
                      <mvt:comment> Uncomment to View Raw Data Collected
                          &mvt:line_id:time_added;
                      </mvt:comment>
                      
                      <mvt:if expr="g.Action EQ 'ADPR' AND (g.Product_Code CIN 'g.treeheartCheck' OR g.Product_Code CIN g.treediscCheck OR g.Product_Code CIN g.familytreeCheck)">
                          <!-- second loop for minibasket (only display last item added) -->
                          <mvt:foreach iterator="item" array="global_minibasket:items">
                              <mvt:if expr="((l.settings:item:code $ g.time_added) CIN l.settings:recently_added:product_codes) AND ((l.settings:item:line_id $ '_' $ g.time_added) CIN l.settings:line_id:time_added)">
                      
                                  mini bask code for just the last item
                      
                              </mvt:if>
                          </mvt:foreach>
                      </mvt:if>
                      For now I have opted to move the Magnific Popup div to the PROD template which will open on the add to cart ajax success for certain products. Otherwise the mini bask opens as normal.

                      I may try to append the selected options to that Magnific Popup div via javascript or just go with your module and move it back to the global mini bask if I decide it's necessary.

                      Your modules (Kent) have certainly been very reliable and have been serving us well over the years so that would probably be the easier option.

                      I appreciate the suggestions though guys.

                      Cheers.
                      Nick Harkins
                      www.loveisarose.com

                      Comment


                        #12
                        another tweak on this scenario and borrowing on Kent's idea. (no idea if this would work) instead of using the custom basket field to track line items, see if you can't just log the LAST g.action and the prod code and quantity etc.

                        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


                          #13
                          Originally posted by Bruce - PhosphorMedia View Post
                          another tweak on this scenario and borrowing on Kent's idea. (no idea if this would work) instead of using the custom basket field to track line items, see if you can't just log the LAST g.action and the prod code and quantity etc.
                          As much as I would love to try this, I'm not too familar with those functions as of yet. Thanks for the suggestion though.

                          Kent Multer I've sent you an email about your utility module.
                          Nick Harkins
                          www.loveisarose.com

                          Comment

                          Working...
                          X