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.