Magento API - Get Product Names from specific Category - api

I'm working on the Magento API and I got some questions...
I try to get all Product Names from a specific category at Magento.
here's my code for that:
<?php
$host = "www.host.tld/index.php";
$client = new SoapClient("http://".$host."/api/soap/?wsdl");
$apiuser= "user"; //webservice user login
$apikey = "pass"; //webservice user pass
try {
// Login
$sess_id= $client->login($apiuser, $apikey);
// Getting all products from category
$filters = array( 'category_ids' => array('107') );
$productList = $client->call($sess_id, 'catalog_category.assignedProducts', $filters);
//iterate and get all the product_id's and put it into one array
foreach($productList as $products => $values){
if (isset($values['product_id']) || array_key_exists('product_id', $values)) {
$product_ids[] = $values['sku'];
}
}
//Get product details from product_id
foreach($product_ids as $key => $values) {
$details = $client->call( $sess_id, 'product.info', array($values));
#echo $details['name'];
}
}
catch (Exception $e) { //while an error has occured
echo "==> Error: ".$e->getMessage();
exit();
}
?>
The main problem I have is, that the performance I get is not the best. For each "product name" I make one api-call for getting all products. Every time for each product. That's probably not so clever.
What can I optimize. Am I missing something?
I can imagine, that if I want to get the details from more than one category, my server will crash ;). We have about 1000 products in our shop.
Thanks for the help.

Try
$proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$filters = array(
'id' => array('in' => array(<product_id_1>, <product_id_2>, ...))
);
$products = $proxy->call($sessionId, 'product.list', array($filters));
If you need some extended info about product - you should alter magento code, see this my answer on the similar questionL Magento API v1- List prices for all products in one call

You can use to catalog_category.assignedProducts
Retrieve the list of products assigned to a required category.
try this if you are using SOAP V1:
$client = new SoapClient('http://magentohost/api/soap/?wsdl');
// If somestuff requires api authentification,
// then get a session token
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'catalog_category.assignedProducts', '4');
var_dump($result);
// If you don't need the session anymore
//$client->endSession($session);
if you are using SOAP V2:
$proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login('apiUser', 'apiKey'); // TODO : change login and pwd if necessary
$result = $proxy->catalogCategoryAssignedProducts($sessionId, '4');
var_dump($result);
and the respond would be like this:
array
0 =>
array
'product_id' => string '1' (length=1)
'type' => string 'simple' (length=6)
'set' => string '4' (length=1)
'sku' => string 'n2610' (length=5)
'position' => string '1' (length=1)
1 =>
array
'product_id' => string '2' (length=1)
'type' => string 'simple' (length=6)
'set' => string '4' (length=1)
'sku' => string 'b8100' (length=5)
'position' => string '1' (length=1)

Related

How to assign associative array to collection object in Laravel

Hello Laravel Community,
I am new to Laravel and still learning.
Suppose I query data by using the following code
$User = User::where(['UserID' => $request->UserID])->first();
and I have an associative array
$Data = array(
['Username'] => 'Updated Username',
['UserEmail'] => 'Updated#gmail.com'
);
I wanted to load the associative array to the collection, something like
$User->load($Data);
I don't want to assign every field one by one, for example
$User->Username = $Data['Username'];
$User->UserEmail = $Data['UserEmail'];
Is there a way to do this?
Thank you.
**You can try with foreach loop.**
$User = User::where(['UserID' => $request->UserID])->first();
foreach($Data as $key => $value){
$User->$key = $value;
}
For check the value is assign or not then use
dd($User->getAttributes());
$Data = array(
['Username'] => 'Updated Username',
['UserEmail'] => 'Updated#gmail.com'
);
$User = User::where(['UserID' => $request->UserID])->first();
$User->fill($Data);

Prestashop overriding default behavior

I'm not a big expert with PrestaShop but this is what I would like to achieve: Everytime a customer confirms an order for a particular product, I would like to send an http request to another server to start the production of the product.
This is what I have in mind:
Create a file in /override/controlers/front/OrderConfirmationController.php
class OrderConfirmationController extends OrderConfirmationControllerCore
{
public function initContent()
{
parent::initContent();
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
}
The issue is that I need certain specifications from the products (size, color, ...) but not all types of products will be sent to that production center.
I am using version 1.6.1.1
Thanks for your help!
Quentin
take a look into init() method of controller that you override
/controllers/front/OrderConfirmationController.php
there you may see:
$order = new Order((int)($this->id_order));
in child controller you have access to Order object, so:
$products = $order->getProducts();
will to get you products list.

How can i get product details at order confirmation page in prestashop?

I have created custom module to send curl request.In this i get shipping address of current user but i want to get product details as well in order confirmation page. How can i do this?
Here Is my Code:
public function hookOrderConfirmation($params)
{
**Here i want to print product details**
echo $this->context->customer->firstname ."<br/>";
echo $this->context->customer->lastname ."<br/>";
echo $this->context->customer->email ."<br/>";
die("GET ALL DETAILS.");
//set POST variables
$url = 'http://localhost/wrd-folder/web-service/';
$fields = array(
'first_name' => urlencode("Testing1"),
'last_name' => urlencode("Testing2"),
'username' => urlencode("Testing1"),
'email' => urlencode("testing#gmail.com"),
'membership_id' => urlencode("676"),
'password' => urlencode("12345678"),
'password2' => urlencode("12345678")
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//Lets display our template file.
return $this->display(__FILE__, 'sentcurl.tpl');
} // End of hookOrderConfirmation
In a few lines you can acomplish this task. You have 2 options:
Get an products array and their price
$order = $params['objOrder'];
// array with products with price, quantity (with taxes and without)
$products = $order->getProducts();
Get a more detailed array with products:
$order = $params['objOrder'];
$products_with_details = $order->getProductsDetail();
You can get like:
$order = new Order($id_order);
$products = $order->getProducts();
foreach($products as $product):
..
$product has every param that you need.

Trouble passing SQL data to view in codeigniter

I have a form that I am using to allow people to input data and then upload a preset picture. I want to label that picture as the form_id. I am able to run a LAST_INSERT_ID() query and display it after I insert the form in my view but I can't seem to echo that information out anywhere else. I need the query to select_last_id to run after my $update query or the ID number will be off. Could anyone assist in helping me pass just the value of the row to my view? Here is the code from my controller.
function inspection() {
if($this->input->post('submit')) {
$array = array(
'trlr_num' => $this->input->post('trlr_num'),
'seal' => $this->input->post('seal'),
'damaged' => $this->input->post('damaged'),
'truck_num' => $this->input->post('truck_num'),
'driver_name' => $this->input->post('driver_name'),
'car_code' => $this->input->post('car_code'),
'origin' => $this->input->post('origin'),
'lic_plate' => $this->input->post('lic_plate'),
'del_note' => $this->input->post('del_note'),
'live_drop' => $this->input->post('live_drop'),
'temp' => $this->input->post('temp'),
'level' => $this->input->post('level'),
'ship_num' => $this->input->post('ship_num'),
'trlr_stat' => $this->input->post('trlr_stat'),
'comment' => $this->input->post('comment')
);
$update = $this->trailer_model->insert_form($array);
$query = $this->trailer_model->select_last_id();
$result =& $query->result_array();
$this->table->set_heading('ID');
$data['table'] = $this->table->generate_table($result);
unset($query,$result);
}
$level = $this->trailer_model->select_fuel_level();
$result = $level->result_array();
$data['options'] = array();
foreach($result as $key => $row) {
$data['options'][$row['level']] = $row['level'];
}
unset($query,$result,$key,$row);
$data['label_display'] = 'Fuel Level';
$data['field_name'] = 'level';
$status = $this->trailer_model->select_trailer_type();
$result = $status->result_array();
$data['options1'] = array();
foreach($result as $key => $row) {
$data['options1'][$row['trlr_stat']] = $row['trlr_stat'];
}
unset($query,$result,$key,$row);
$data['label_display1'] = 'Trailer Status';
$data['field_name1'] = 'trlr_stat';
$data['page_title'] = 'Trailer Inspection';
$data['main_content'] = 'dlx/inspection/trailer_inspection_view';
return $this->load->view('includes/template',$data);
}
}
Anything you want to display on a view, you must pass to it.
When you're setting $data, you missed adding $query to it.
Controller:
I understand that $query = $this->trailer_model->select_last_id(); is returning just an ID, so:
$data['last_id'] = $query;
But if $query = $this->trailer_model->select_last_id(); does not return just an ID, but an array or something else, you shall include to return the ID in your model method. Actually, it'd be crucial to know what $this->trailer_model->select_last_id(); is returning.
View:
echo $last_id; -> must echo the last ID, if select_last_id() method from your model returns what it has to return.

Magento API SOAP filter website_ids error

I'm using api soap v1 and calling catalog_product.list everytime I add the website_ids in filter It cause an error.
$filter = array(
'status' => array( '=' => 1 ),
'type_id' => array( '=' => 'simple' ),
'website_ids' => array('6'),
);
2nd question, the args of catalog_product.list is filter and storeView, if I add the store view id or code I display all the product I guess It ignores what I add on it.
$proxy->call($sessionId, 'catalog_product.list', $filter = null, '6');
Thanks
I found this solution.
//you're website's id
$result = $client->call($session, 'store.info', '6');
$code = $result['code'];
//here all filters, in 'filters' you can add others filters (like price for example)
$filters = array(
'filters' => array(
'status' => 1,
'type_id' => 'simple',
),
'storeView' => "$code"
);
try{
$result = $client->call($session, 'catalog_product.list',$filters);
} catch (Exception $e){
print_r($e);
}
print_r($result);
now it works? :)