Reputation: 216
I'm using the Webservice API Version 2 of Magento via Soap. I've used gSoap to generate Wrapper/Proxy-Classes for the Soap-Calls and so far, everything works fine. I can
and I'm able to get all the information I need in my Desktop-Application.
What I want to do now is: I have a shopping site in my Desktop-Application, where the user can select products (which means, add them to the shopping-cart) and confirm his selection by clicking on a button, e.g. 'buy'. By clicking on this button, he will be redirected to the Webshop in the Browser and now there should be (probably after authentification) his shopping cart with all the products in it he selected in the Desktop Application.
So, what I tried to do is
(don't be concerned with the ugly class -and method-names ;o) )
Generate a new Shopping Cart:
void SoapManager::createShoppingCart()
{
ns1__shoppingCartCreateResponse shoppingCartCreation;
int x = m_pProxy->shoppingCartCreate(m_stLoginID, "1", shoppingCartCreation);
if(x!=0)
printFaultDetails(m_pProxy->fault);
m_currentShoppingCart = shoppingCartCreation;
}
Set an existing customer to the created shopping cart:
void SoapManager::setCustomerToCart(ns1__shoppingCartCustomerEntity *customer)
{
ns1__shoppingCartCustomerSetResponse customerResponse;
int x = m_pProxy->shoppingCartCustomerSet(m_stLoginID, m_currentShoppingCart.quoteId, customer, "1", customerResponse);
if(x!=0 || !customerResponse.result)
printFaultDetails(m_pProxy->fault);
}
Add a simple (example) product to the cart:
void SoapManager::addProductToCurrentCart(shoppingCartProductEntityArray *productsToAdd)
{
ns1__shoppingCartProductAddResponse productAdded;
int x = m_pProxy->shoppingCartProductAdd(m_stLoginID, m_currentShoppingCart.quoteId, productsToAdd, "1", productAdded);
if(x!=0 || !productAdded.result)
printFaultDetails(m_pProxy->fault);
}
In general, that's all. What i would expect is that the product is attached to shopping cart of the customer, which was set to the cart. But after Redirection to the Webshop there is no product in the Cart, neither a shopping Cart in the admin panel.
When I retrieve the shopping cart programmatically by using the quote ID, there are all added products in it so I think, it's probably an understanding mistake of mine.
Any suggestions, what I'm doing wrong?
Or is adding a product to the (webshop-)shopping cart only possible using the query string? (And if so: How to add several products?)
Best regards and thanks in advance!
Jan
Upvotes: 1
Views: 2697
Reputation: 216
it's a bit ago but i guess i finally did it by using a mixture of half-knowledge and trial 'n error ;o) Beside this I'm not going to take this solution but to integrate magento completely in my Desktop-Application without redirecting the user to the browser.
But back 2 Topic: There were a few things concerning the magento core-code, which is obviously a little buggy, at least using the webservice api.
First when creating a new shopping cart for the user, this cart isn't marked as active. In 'app/code/core/checkout/model/cart/Api.php' you have the following code snippet which obviously creates a new cart:
public function create($store = null)
{
$storeId = $this->_getStoreId($store);
try {
/*@var $quote Mage_Sales_Model_Quote*/
$quote = Mage::getModel('sales/quote');
//Changed is active to true in order to activate the cart when created
/*$quote->setStoreId($storeId)
->setIsActive(false)
->setIsMultiShipping(false)
->save();*/
$quote->setStoreId($storeId)
->setIsActive(true)
->setIsMultiShipping(false)
->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('create_quote_fault', $e->getMessage());
}
return (int) $quote->getId();
}
The commented code is the original and this sets the shoppingcart false by default which causes (for me) that i couldn't use it. This is one part of the solution that worked for me. The second part is concerning my custom webservice-module extending the magento-webservice-api:
public function getSession($customerID)
{
require_once("app/Mage.php");
Mage::app ();
umask(0);
$webSites = Mage::app()->getWebsites();
$code = $webSites[1]->getCode();
$session = Mage::getSingleton("customer/session"); // This initiates the PHP session
// The following line is the trick here. You simulate that you
// entered Magento through a website (instead of the API) so if you log in your user
// his info will be stored in the customer_your_website scope
$session->init('customer_' . $code);
$bool = $session->loginById($customerID); // Just logging in some example user
return session_id(); // this holds your session id
}
The above code snippet simulates an entry through the normal login-process on the magento-website. Here I initialize a session using the Main-website-code / identifier of my shoppage ($webSites[1]->getCode();) and log the user in using the customerID. After that i return the sessionId to my Desktop-Application. When the user now wants to buy the things he choosed (that were added in the same way as i mentionedin the question) he clicks a button and is redirected to the browser to a small php-script which looks like:
<?php
// Make sure you match the cookie path magento is setting
setcookie('frontend', $_GET['session'], 0, '/');
//echo $_GET['session'];
header('Location: http://yourdomain.com/magento/checkout/cart');
?>
The corresponding call / query-string looks like:
http://yourdomain.com/setCookie.php?session="+sessionId
the '+sessionId' represents the session-ID i received from the webservice. All i do here is setting the cookie for the user and redirect him to his shopping cart, that's all. The user should be logged in and see the products he choosed. One important thing is you have to set right directory to where magento stores it's cookies ('/' above).
Well actually i wanted to give you a bunch of links to very informative websites that help me a lot through this annoying process, but stackoverflow doesn't allow more than 2 hyperlinks and unfortunately that are the 2 above in my code.. I'm sorry.
I hope i could help you a little bit. Contact me or write here if you have more questions, perhaps i can help you. :o)
Best regards, Jan
Upvotes: 1
Reputation: 676
i trying to do the same thing ( ios + soap V1 ), i think its not possible to do it for now, but a solution is to change the log methode in mageto core to ' catch ' the username and the password ( via GET or POST ) and call the log function automatically, then you will have the cart raedy.
you are not alone on this, but if you already do it, please telle how ?
Upvotes: 0