2

I'm using Magento 1.9 CE and am using XML RPC to connect to Magento's API and after I add an item to the cart, the "totals" do not update.

Here's the order of operation:

  1. login to api
  2. cart.create - create an empty cart
  3. cart_customer.set - set a "guest" user
  4. cart_customer.addresses - set "guest" user address
  5. cart_product.add - add a product to the cart
  6. At this point we have a cart with 1 item in it. I can then verify that the cart looks ok with cart_product.list which returns the following:

     [
      [
       {"product_id":"10",
       "sku":"159996",
       "name":"Redwood Turtle",
       "set":"9",
       "type":"simple",
       "category_ids":["2","7"],
       "website_ids":["1"]}
      ]
     ]
    
  7. cart.totals - Now when I try to get the totals for the cart, it is null/empty! Here's what I get back:

     [
       [
         {"title":"Subtotal",
          "amount":null},
         {"title":"Grand Total",
          "amount":null}
       ]
     ]
    

What method do I call to populate the totals for the cart?

1
  • 1
    IIRC the Mage_Sales_Model_Quote needs to collect collectTotals() and save() after that - I don't have access to code at this very right moment.
    – versedi
    Commented Feb 4, 2017 at 12:52

1 Answer 1

1

Strangely enough, if you call cart_customer.addresses after calling cart_product.add, then the totals get updated! Here's how I did it using XML-RPC in javascript:

Note: This code sample assumes that you already have already logged into the Magento API and stored the session number plus cart number to session storage (I used https://github.com/julien-maurel/js-storage to do this)

addToCart(10,1,function(){
    setCartCustomerAddresses(function(){
        getCartTotals(function(totals){
            console.log("Cart Totals = ",totals);
        });
    });
});

function addToCart(productId,qty,callback)
{
    var products = {"product_id":productId,"qty":qty};
    var cartNum = storage.get('cart');
    var session = storage.get('session');

    $.xmlrpc({
        url: 'https://example.com/index.php/api/xmlrpc',
        methodName: 'call',
        params: [session, 'cart_product.add',[cartNum,[products]]],
        success: function(response, status, jqXHR) {
            console.log("cart_product.add. Successfully added "+productId+" to the cart.");
            callback(response);
        },
        error: function(jqXHR, status, error) {
            callback(error);
        }
    });
}

function setCartCustomerAddresses(callback)
{
    var cartNum = storage.get('cart');
    var session = storage.get('session');

    var customerShipping = {"mode":"shipping",
    "firstname":"Guest",
    "lastname":"Guest",
    "company":"test",
    "street":"123 maple",
    "city":"Anaheim",
    "region":"12",
    "postcode":"92806",
    "country_id":"US",
    "telephone":"0123456789",
    'is_default_shipping':0,
    'is_default_billing':0};

    var customerBilling = {"mode":"billing",
    "firstname":"Guest",
    "lastname":"Guest",
    "company":"test",
    "street":"123 maple",
    "city":"Anaheim",
    "region":"12",
    "postcode":"92806",
    "country_id":"US",
    "telephone":"0123456789",
    'is_default_shipping':0,
    'is_default_billing':0};

    custAddresses = [customerShipping,customerBilling];

    $.xmlrpc({
        url: 'https://example.com/index.php/api/xmlrpc',
        methodName: 'call',
        params: [session, 'cart_customer.addresses',[cartNum,custAddresses]],
        success: function(response, status, jqXHR) {
                console.log("success ",response);
                callback(response);
        },
        error: function(jqXHR, status, error) {
                console.log("error ",error); 
                callback(error);
        }
    });
}

function getCartTotals(callback)
{
    var cartNum = storage.get('cart');
    var session = storage.get('session');

    $.xmlrpc({
        url: 'https://example.com/index.php/api/xmlrpc',
        methodName: 'call',
        params: [session, 'cart.totals',[cartNum]],
        success: function(response, status, jqXHR) {
            console.log("success ",response);
            callback(response);
        },
        error: function(jqXHR, status, error) {
            callback(error);
        }
    });
}

Code Sample Above requires:

  • JQuery
  • JQuery Storage (You can use whatever you want to keep the session # and cart # though)
  • JQuery XML-RPC to call Magento's API.
  • You'll also need to login before trying the code above, then create a cart.

Another Note: Be careful when loggin into the API via Javascript. It is NOT SECURE! Anyone can view the code and get your username and password!! Therefore, I highly recommend that you create a NEW API user in the Magento backend that only has access to listing products, retrieving info, etc. Make sure the user cannot do anything terrible to your site because it is public.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.