Getting current cart ID in prestashop - web-services

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);

Related

Is there an easy solution to export a filtered backendlist

I have a list with products and locations. I've created a filterdefinition and can filter products based on a location.
Now I want to export te filtered selection to print it to an pdf.
I've tried to acces the filtered items with the ajax framework, but can't find any information on how to retrieve the filtered id's.
My printhandler works fine, I only need to get the ID's of selected items in the filter.
scopes:
location:
label: Location
modelClass: XXX\xxxx\Models\Location
conditions: location_id in (:filtered)
nameFrom: location
As always, found a solution after asking for one;
Found a piece of code:
function getCurrentFilters() {
$filters = [];
foreach (\Session::get('widget', []) as $name => $item) {
if (str_contains($name, 'Filter')) {
$filter = #unserialize(#base64_decode($item));
if ($filter) {
//$filters[] = $filter;
array_push($filters, $filter);
}
}
}
return $filters;
}

how use model for where query in laravel

Hy!
i am using where query and check where user is exist or not in my login system and save the data in a variable $user and then i compact this variable to my view to get the name of logged in user but the query just return in my variable 1 or 0.
what i do. how can I use model for such type of queries please guide me..
here is my little code....
$user = new user();
$user = DB::table('users')->where('name', $request->name)->first() && DB::table('users')->where('password', $request->password)->first() ;
if($user==null){
return redirect('/login');
}
return view('user.dashboard',compact('user'));
You are performing Boolean logic in that long line assigning to $user, hence the true/false you're seeing.
// using eloquent
if ( $user = User::where(['name' => $request->name, 'password' => Hash::make($request->password)])->first()) {
return view('user.dashboard',compact('user'));
}
return redirect('/login');

How to create Categories webservice api in magento and in android app

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.

add product prestashop webservice. So difficult?

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();
}

Zend_Auth: Join Query Issue

I'm a new guy for zendframework. i am facing zend auth join query issue..
Here I attach my zend_auth login sample code.
My login information's are stored in two tables. I mean email address in separate table and password separate. Here I was try to join my table, but I am getting Following error...
Message: The supplied parameters to Zend_Auth_Adapter_DbTable
failed to produce a valid sql statement, please check table and column
names for validity.
Please advise me.
My code is here...
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName(array('users','details'))
->setIdentityColumn('name')
->setCredentialColumn('pwd');
$name = 'test';
$pwd = '123';
$authAdapter->setIdentity($name)
->setCredential($pwd);
$select = $authAdapter->getDbSelect();
$select->where('pwd = 123')
->joinLeft( array('d' => 'details'),'d.id = users.id');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);