I'm searching for a tutorial or documentation where is explained the webservice use of Prestashop (v1.5.6.0).
I'd like simply add or edit (update) o product.
There is'not a tutorial clean or with example about use of prestashop's api.
Coul you help me please ?
For example, i'd like add object a:
define('PS_SHOP_PATH', 'http://localhost/myshop'); // Root path of your PrestaShop store
define('PS_WS_AUTH_KEY', '****'); // Auth key (Get it in your Back Office)
require_once('api/PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'products');
Now, how can i set my values for a new object ? In the example you can insert only required value.
Could you help me ?
And for update ?
Please ,no linked me Prestashop documentation, i have already read it, i'm asking your help.
Thanks and excuse for my bad english.
Here is the link to the official documentation. You will find there all the information you need.
Basically, you'll need to create an XML that represents the object you'd like to PUT.
Lets say you want to create a new category.
First, you need to get the schema :
$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/categories?schema=blank'));
Which is something like this :
<prestashop>
<category>
</category>
</prestashop>
Then you'll have to set the content of the xml ($resources)
$resources = $xml->children()->children();
$resources->active = true;
$resources->..........
etc.
Finally,
try
{
$opt = array('resource' => 'categories');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
}
catch (PrestaShopWebserviceException $e)
{
echo 'Something went wrong: '.$e->getMessage();
}
Related
I am busy with a site built on Code Igniter that needs integration with Prestashop. In the site, when creating a user account, I save a "shop_id" to the database, which is then retrieved as a session variable when logging in.
I am using the Prestashop API to retrieve the customer successfully (using above "shop_id")
$xml = $this->PSWebService->get(
array('resource' => 'customers', 'id' => (int)$this->user['shop_id'])
);
This successfully returns the user in question, but there is no Cart IDs in this result.
Logging in to the back-end of my shop, I see that there are multiple carts associated with the logged in user.
My question is: How to I retrieve the LATEST cart ID using the API?
$userId = (int) $this->user['shop_id'];
$opt['resource'] = 'carts';
$xml = $this->PSWebService->get($opt);
$carts = $xml->carts->children();
foreach ($carts as $cart) {
$cartIds[] = $cart['id'];
}
for ($i = count($cartIds) - 1; $i > -1; $i--) {
$opt['id'] = $cartIds[$i];
$xml = $this->PSWebService->get($opt);
//since cart ids are descending the first found will be the latest
if ($xml->cart->id_customer == $userId) {
$latestCartId = $cartIds[$i];
break;
}
}
Kind of late, but I struggled a bit with this same problem and found a way to do it just from query, based on yenshirak's tip (since cart ids are descending the first found will be the latest).
I query the API directly using postman like this:
get all carts:
GET webserver/api/carts
get cart for customer:
GET webserver/api/carts?filter[id_customer]=1
get the most recent:
GET webserver/api/carts?filter[id_customer]=1&sort=[id_DESC]&limit=1
for a pretty print, you can also add params:
display=full&output_format=JSON
You can do this in php, I have not tested if the syntax is correct, but based on documentation it looks something like this:
$opt = array(
'resource' => 'carts',
'filter[id_customer]' => '[1]',
'sort' => '[id_DESC]',
'limit' => '1'
);
$xml = $webService->get($opt);
I want to know how to create the Webservice in magento 1.8.0 which fetch all the categories name of the store and then I need to show on my Android application.
So please advice.
yes you can crate your own api for category.list with use of below link you can sure create your api
Create new magento Rest api to get category list in magento
Also if you want to create your own function to fetch category you can also create your own function like below
function get_categories(){
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();
if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel('catalog/category');
$cat->load($id);
$arr[$id] = $cat->getName();
}
}
return $arr;
}
$arr = get_categories();
$arr = array_flip($arr);
echo "<pre>";
var_dump($arr);
echo "</pre>";
hope this will sure help you.
I need to get the content of an Article which is on my Joomla website. In order to do this, I was planning to write a simple PHP webservice to get the content (HTML) of my article. But I just don't know how to do this.
I'm new in Joomla, web developping and web services. I just want to get the content of my article in order to display it in my ruby on rails site. Can anyone explain how I can do this?
Well, here is at least a small contribution that should help to get you going: the attached code can be called with "?id={ID of article you want to retrieve}" and it displays the entire object on screen.
Next steps: pick out the pieces you're interested in and return these...
<?php
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
$username="*** insert username ***";
$password = "*** password here ***";
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
// Mark afterLoad in the profiler.
JDEBUG ? $_PROFILER->mark('afterLoad') : null;
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
$app->initialise();
$app ->login(
array('username'=>$username,
'password'=>$password),
array('remember' => true));
$dbo = JFactory::getDBO();
$id=$_GET["id"];
$art = JTable::getInstance('Content');
$art->load($id);
echo "<pre>";
var_dump($art);
echo"</pre>";
?>
I'm new to Prestashop, I cant find examples anywhere of how to get the current cart contents. I can get a list of all carts, but how do I get the current users cart?
it is easy and simple. I am considering you are using PS 1.5.x
In controllers other than cart controller
$cart = new Cart($this->context->cookie->id_cart);
or in an class
$context = new Context();
$cart = new Cart($context->cookie->id_cart);
Now the $cart is an object, and it has all the current cart data.
You can also get the cart products by calling getProducts like below
$cartProducts = $cart->getProducts();
Hope this will help.
Please note that code is not tested and is just a sample code for your idea.
Thank you
For PS 1.4.X you can get using getProducts()
$product_array = $this->getProducts();
print_r($product_array);
Example :
public function getSubTotal() {
$product_array = $this->getProducts();
foreach($product_array as $product_item) {
$sub_total += $product_item['price'] * $product_item['cart_quantity'];
}
return $sub_total;
}
Its my first time to use web service in iOS.
REST was my first choice and I use code igniter to form it.
I have my Controller:
require APPPATH.'/libraries/REST_Controller.php';
class Sample extends REST_Controller
{
function example_get()
{
$this->load->helper('arh');
$this->load->model('users');
$users_array = array();
$users = $this->users->get_all_users();
foreach($users as $user){
$new_array = array(
'id'=>$user->id ,
'name'=>$user->name,
'age'=>$user->age,
);
array_push( $users_array, $new_array);
}
$data['users'] = $users_array;
if($data)
{
$this->response($data, 200);
}
}
function user_put()
{
$this->load->model('users');
$this->users->insertAUser();
$message = array('message' => 'ADDED!');
$this->response($message, 200);
}
}
, using my web browser, accessing the URL http://localhost:8888/restApi/index.php/sample/example/format/json really works fine and gives this output:
{"users":[{"id":"1","name":"Porcopio","age":"99"},{"id":"2","name":"Name1","age":"24"},{"id":"3","name":"Porcopio","age":"99"},{"id":"4","name":"Porcopio","age":"99"},{"id":"5","name":"Luna","age":"99"}]}
, this gives me a great output using RKRequest by RestKit in my app.
The problem goes with the put method. This URL :
http://localhost:8888/restApi/index.php/sample/user
always give me an error like this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<xml>
<status/>
<error>Unknown method.</error>
This is my Users model
<?php
class Users extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_users()
{
$this->load->database();
$query = $this->db->get('users');
return $query->result();
}
function insertAUser(){
$this->load->database();
$data = array('name'=> "Sample Name", 'age'=>"99");
$this->db->insert('users', $data);
}
}
?>
What is the work around for my _put method why am I not inserting anything?
Thanks all!
Unless you set the method to PUT or POST, your web server is not going to treat it as such. When you enter URLs in a browser bar, that is almost always a GET request. You might try to use curl like
curl -X POST -d #filename http://your.url.path/whatever
Another link would be: https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request
So you should be able to do a PUT similarly (perhaps with no data). Not really sure if this should be iOS tagged though :)
I got this problem by using Firefox plug in rest Client.
I just have to indicate the headers and the body to make put and post work.