Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Mar 28, 2013 @ 11:38
    Dan
    0

    JavaScript API syntax to add item to cart

    Hi,

    Part of a site I'm developing has a client-side, cookie-based product wish-list whereby a customer can add items to a list to buy at a later date.  The wish-list is essentially a micro shopping cart so I'm using simplecartjs for this, which does the job nicely.  I need to add a button on the wish list which the user can click to add these items to the tea commerce shopping cart.  So to do this I'm looping through the simplecartjs object and pulling out all the relevant properties (product name, quantity, price etc) and I'd like to use these properties to add the item to the TeaCommerce (v2) cart object.

    I'm struggling a little though with the lack of documentation of the JavaScript API and I'm not sure how to perform just a simple, stand-alone 'add item to cart' operation through JavaScript.  The rest of the cart functionality is based on the starter kit, using the combination of AJAX form posts and HTML forms, and the 'ecommerce.js' script to handle everything, which works fine.  I just don't know how I can call a completely stand-alone 'add to cart' through the API.

    So far this is what I have:

    <script type="text/javascript" src="/scripts/tea-commerce.min.js"></script>
    <script type="text/javascript" src="/scripts/simpleCart.js"></script>
    <script type="text/javascript">
        $(function(){
            // Get tea commerce store id
            _storeId = <umbraco:Item field="store" recursive="true" runat="server" />;
            // Instantiate TeaCommerce order object
            var order = TC.getCurrentOrder({storeId:_storeId});
            
            // Add quote items to cart
            $('.add-quote-items-to-cart').click(function(){
                simpleCart.each(function(item,x){
                  // Get simplecart order line properties
                  var productName = item.get('name');
                  var productQuantity = item.get('quantity');
                  // ...etc
                  // Here I need to call the TC API, something like: order.addOrderLine({...something...});
                });
                return false;
            });
    Everything is fine except the part where I need to add the products to the TeaCommerce cart.
    Can anyone show me the correct way to do this?
    Thanks
  • Anders Burla 2560 posts 8256 karma points
    Mar 28, 2013 @ 16:54
    Anders Burla
    100

    Hi Dan

    The JavaScript API looks almost like the HTML api - but of course using JSON format. So a  simple add order line command would look like this.

    TC.addOrUpdateOrderLine({storeId:1, productIdentifier: 1262, quantity: 1})

    If you have the _storeId variable in your javascript (that is in the starter kit, then you dont need to send the storeId as a parameter because of the javascript api looks for a _storeId variable if no storeId is specified.

    Kind regards
    Anders

  • Dan 1288 posts 3921 karma points c-trib
    Mar 28, 2013 @ 17:38
    Dan
    0

    Thanks.  I actually solved it by just doing an ajax post to the base method, but that kind of defeats the point of the TC API, so I'll neaten it up by using the API and doing it properly.  Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft