Cart total doesn't update after adding an item to the cart (using API) using Magento 1.9 CE
-
06-10-2020 - |
Domanda
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:
login
to apicart.create
- create an empty cartcart_customer.set
- set a "guest" usercart_customer.addresses
- set "guest" user addresscart_product.add
- add a product to the cartAt 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"]} ] ]
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?
Soluzione
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.