How to 'Manipulate' Cart Outside the Magento System - API

Asked

Viewed 282 times

3

Hello, Well, I would like to integrate the Magento system in my site (only 1 page), because it has a module for quick integration of the MOIP checkout system, so I wouldn’t need to do the whole system in hand, since there is a module ready for this platform(Magento).

So that was the only way I knew how to get a transparent MOIP checkout quickly. In order to do this, I want to know how to add items in the Magento cart (through PHP for example), so that when I redirect the client to the checkout page (where the Magento system enters with the MOIP module) the items in the cart can appear right, as if the person had from the beginning adding the items in the cart through the Magento platform.

Is that possible? How do I do? Alternative?

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

Any clarification, just ask. Thank you!

1 answer

2


I don’t know if what I say will help you, because it’s not a definitive answer but I’ve been through a similar problem but in my case I really needed "emulate" a purchase outside of Magento, but pretending that the person had done all the process inside him and I say that I managed to get close, however I gave up the Magento is full of freshness and nuances which makes the WELL complicated thing. So I honestly think that the time and head bump you’ll have trying to do this will be better to use the MOIP SDK that gives more game. But I’ll tell you how far I’ve come.

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

Something more or less 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 intend to do, so you will have to deepen us codes of the version you are using and in what you want to do, there are several examples on the internet do not know which could be last for you, I will play some for you to see.

Example 1 - Example 2 - Example 3 - Example 4

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

From this just access the API with a PHP file any even 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 API will have to study the code as it is fragmented into several mini calls like shipping, payment, users, coupon among other things I will point out the links I think you need.

Introduction to the API - Creation of Cart with User - Create Order with Cart

In both cases a problem that maybe I see is how you will attach the user to the cart, in general Magento needs to know before who is to assemble the cart, you can even use a 'guest' user but when arriving at chekout this is a lot of payment and freight problems within Magento, so I wouldn’t know how you could do it, in my case I received the data first and looked inside Magento (in DB) the person’s email if I thought I would take the user’s ID and embed it inside the cart, in case I didn’t find out I created a user, and then assemble the cart, order and etc.

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

  • Thanks for the @SK15 reply, but as I said in the topic, I hoped it would be simpler and the idea was that it would be faster, but as the content was scarce for me to start crawling and carry out the process, I decided to use the Moip API to do it on the site anyway, thanks for the topic, maybe I’ll come back here in the future if I want to embark on this adventure! Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.