How to 'Handle' Cart Out of Magento System - API

3

Hello, Well, I would like to integrate the Magento system on my site (only 1 page), as it has a fast integration module of the MOIP checkout system, so I would not have to do the entire system on hand, since there is a module ready for this platform (Magento).

So that was the only way out that I realized to have a transparent MOIP checkout quickly. Aiming at this, I want to know how to add items to the Magento cart (via PHP for example), so that when I redirect the client to the checkout page (then the Magento system enters with the MOIP module) cart items may appear right, as if the person had from the beginning adding the items in the cart through the Magento platform.

Is this possible? How do I do? Another alternative?

It seems like a game, to install a platform in a folder just to have a MOIP module, but this is the only alternative (faster, I believe) that I had to go through this problem ..

Any clarification, just ask. Thanks!

    
asked by anonymous 10.07.2015 / 07:06

1 answer

2

I do not know if what I say will help you, because it is not a definitive answer but I already had a similar problem, but in my case I really needed to emulate a purchase outside of Magento, but pretending that the person had done all the process inside it and I say that I got close, but I gave up the Magento is full of freshness and nuances which makes the GOOD thing complicated. So honestly I think the time and head bump that will have trying to do this will be better to use the MOK SDK that gives more game. But I'll say more or less how far I've come.

1) Programming with Objects
The first way I found to do this is to use the classes, functions and objects of Magento itself, you include the file "Mage.php" in any PHP file and start to mount the cart by calling the functions, yes Magento literally mounts the cart gradually, it creates a session for that purchase, then adds the user to which the purchase belongs, then adds the products to that session and user, then adds more freight and payment and finally generates an order, all this within the tables and within that order ie you can not calculate freight without a registered user for example, or just with a single zip code for example.

Something like this (this is just about the syntax to run needs more things):

<?php
require_once("app/Mage.php"); 
$session = Mage::getSingleton("customer/session"); 

$cart = Mage::getSingleton("checkout/cart"); 
$cart->init();

$cart->addProduct(12, 3);
$cart->save();

var_dump($cart);
?>

But it changes a lot from version to version of Magento and what you want to do, so you will have to delve into the codes of the version you are using and what you want to do, there are several examples on the internet, I do not know which one could be the last one for you , I'll play some for you to see.

Example 1 - Example 2 - Example 3 Example 4

2) Using Magento API
Well what a lot of people do not know is that Magento has 2 REST APIs that are used to make small internal requests and another one using SOAP (v1 and v2) that you can do almost all operations within Magento which mega slow because as they are slow if crazy) . To use the API you must first enter the settings there in the 'admin' area and in the API tab create a user and password and set the permissions of that user, it would be something like a key. Remember that there are discrepancies between the v1 version of the API and the v2 version, then you have to test both to see which will work in your proprosito, in my case I used both for each situation that I needed a 'module' that only worked well in one version.

From this, simply access the API with any PHP file even if it is running on your local machine, something like this.

<?php
$proxy = new SoapClient("http://seuhost.com.br/api/v2_soap/?wsdl");
$sessionId = $proxy->login("user-Criado", "senha-Criada");     
$result = $proxy->shoppingCartCreate($sessionId, "3");
var_dump($result);
?>
Well to use these APIs you will have to study the code as it is fragmented into several mini calls like freight, payment, users, coupon among other things I will point out the links that I think you need. Introduction to API - User Cart Creation - Create Order with Cart

In both cases a problem that I may see is how you will tether the user to the cart, Magento generally needs to know beforehand who is to mount the cart, you can even use a 'guest' user, however when you arrive at the chekout this gives a lot of payment and freight problems inside Magento, so I would not know how you could do it, in my case I would receive the data first and look inside Magento (in the DB) the person's email if I thought I would get it the user ID and embedded inside the cart, if I did not find I would create a user, and then mount the cart, order, and so on.

I know that links are not recommended but sample codes are very large and complex, sorry.

    
26.03.2016 / 06:31