cancel
Showing results for 
Search instead for 
Did you mean: 

API - Redirecting user to magento instance to view their cart

SOLVED

API - Redirecting user to magento instance to view their cart

Hi,

 

I am writing a web app that communicates with Magento API (SOAP V2) for reading product data and then allowing the user to add to cart products they wish to purchase.

 

All API calls are done from my server, using a login I added via the admin site.

 

When the user finishes the selection process are redirected to the shopping cart on the Magento instance and complete the purchase.

 

And that's the tricky bit - calls to the API are done using a specific admin user, under this user session. The cart is also added to this admin session. When my user is redirected to the site they won't see anything there, since it's not their session - they are arriving to the site as guests.

 

One way to solve this would be to make the add to cart from JS and not server side, because this will associate the cart with the browser and not with my server side logic, but I'm not aware of a way to do this. Not to mention cross-domain JS issues.

 

So - Is there somewhere in the API something that can help me do something like retrieve a URL I can redirect the user to - this URL will associate the cart with the user who is redirected to this URL and then they can see the cart.

This sounds like a reasonable requirement and I believe it should be available one form or another in the current API. At least I hope so.. Smiley Happy

 

I prefer to avoid (if possible) writing custom APIs for this.

 

Any help would be much appreciated!

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: API - Redirecting user to magento instance to view their cart

OK, I let this lay around for a couple of days and no answer yet. After much digging I came to the conclusion that it cannot be done without customisation on the Magento side.

 

I ended up writing a custom Magento controller to handle the transfer of the user from my custom UX to Magento. Along the way I pass details on which items to add to the cart. The controller handles the addition to the cart.

 

Since the request is executed by the browser then all processing on Magento is done in the user session.

 

Works for me.

 

Happy to hear thoughts on how this can be done better.

 

Cheers.

View solution in original post

3 REPLIES 3

Re: API - Redirecting user to magento instance to view their cart

OK, I let this lay around for a couple of days and no answer yet. After much digging I came to the conclusion that it cannot be done without customisation on the Magento side.

 

I ended up writing a custom Magento controller to handle the transfer of the user from my custom UX to Magento. Along the way I pass details on which items to add to the cart. The controller handles the addition to the cart.

 

Since the request is executed by the browser then all processing on Magento is done in the user session.

 

Works for me.

 

Happy to hear thoughts on how this can be done better.

 

Cheers.

Re: API - Redirecting user to magento instance to view their cart

i'm running into the same issue. do you have a link to the extension you've made?

Re: API - Redirecting user to magento instance to view their cart

Hi,

 

I didn't package this as an extension and it has some code I prefer not to share online, but the overall concept is very straight forward.

 

The user interacts with the products on my 3rd party site (not Magento). When they click on Checkout then I pass the IDs of the added products to a custom controller that I wrote, which adds them to the cart and then redirects the user to the Cart page. Something like this:

 

<?php

class CustomController extends Mage_Core_Controller_Front_Action {
    public function keyAction(){
        //some logic to generate unique key which is passed as part of taking the user to the cart page
    }

    //this is the action that loads the cart and redirects to the cart page
    public function cartAction() {
        $key = $this->getRequest()->getParam('key');
        $input = $this->getRequest()->getParam('input');

        if (!$this->validateKey($key, $input)) {
            Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
            Mage::app()->getResponse()->sendResponse();
            exit;
        }

        // Get customer session
        $session = Mage::getSingleton('customer/session');

        // Get cart instance
        $cart = Mage::getSingleton('checkout/cart');
        $cart->init();

        // Add products
        foreach (explode(',', $this->getRequest()->getParam('products')) as $value) {
            $cart->addProduct(intval($value), 1);
        }

        //also auto add coupon code if passed in the request
        $coupon = $this->getRequest()->getParam('coupon');
        if ($coupon) {
            $cart->getQuote()->getShippingAddress()->setCollectShippingRates(true);
            $cart->getQuote()->setCouponCode($coupon)->collectTotals()->save();
        }

        // update session
        $session->setCartWasUpdated(true);

        // save the cart
        $cart->save();

        //Mage::app()->getResponse()->setRedirect(Mage::helper('checkout/cart')->getCartUrl()); //redirect to Cart
        Mage::app()->getResponse()->setRedirect(Mage::helper('checkout/url')->getCheckoutUrl()); //redirect to Checkout
    }

    function validateKey($key, $input) {
        //some logic to validate passed key
    }
}

?>

I hope this helps!