Map API details to Wooocommerce Fields - api

I'm looking to map API attributes from Unleashed to Woocommerce as products. I can currenly access the Unleashed API and retrieve data, but to insert this into Woocommerce is quite challenging.
Does anyone know a way to do it?
Here's a code snippet:
foreach( $customer_details[0] as $customer ) {
$customer_slug = sanitize_title($customer->CustomerName . '-' . $customer->CustomerCode);
$inserted_customer = wp_insert_post([
'post_name' => $customer_slug,
'post_title' => $customer_slug,
'post_type' => '',
'post_status' => 'publish'
]);
if( is_wp_error( $inserted_customer ) ) {
continue;
}
$product->get_id();
$fillable = [
'$product->name' => '$customer->CustomerName';
];
}
Where $product->name references the product name and $customer->CustomerName is the Unleashed data that needs to go into the $product->name field.
Can anyone point me in the direction?

Related

API post request not reaching the application

i'm new to laravel and i'm facing a painful problem.
I'm using Crinsane/LaravelShoppingcart in my ecommerce api and i'm trying to send a post request with axios in vuejs that adds a product to the cart by sending the product id and the quantity. The problem is the id and quantity are not reaching the application although i'm pretty sure i specified the correct route link in axios and i'm getting "No query results for model [App\Product]." which i assume means that the controller function that handles the request is working but the id is not being sent/transformed to the resource collection. I don't know if the problem is with the package i'm using or the code or something else.
this is axios request
addCart(item) {
axios
.post('/api/cart/add', item)
.then(response => (response.data.data))
.catch(error => console.log(error.response.data))
this is the route :
Route::post('cart/add', [
'uses' => 'ShoppingController#store',
'as' => 'cart.add'
]);
this is the cart collection
public function toArray($request)
{
return [
'id' => $this->id,
'qty' => $this->qty
];
}
this is the controller
public function store(){
$pdt = Product::findOrFail(request()->id);
$cart = Cart::add([
'id' => $pdt->id,
'name' => $pdt->name,
'qty' => request()->qty,
'price' => $pdt->price
]);
and this is the product model
class Product extends Model
{
protected $fillable = [
'name', 'description', 'image', 'category', 'quantity', 'price', 'sold','remaining','rating', 'bestSelling', 'featured'
];
}
Thank you in advance
The problem seems to be in your controller.
From the docs:
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method.
Try this:
public function store(Request, $request){
// Make sure the 'id' exists in the request
if ($request->get('id')) {
$pdt = Product::find($request->get('id'));
if ($request->get('qty')) {
$qty = $request->get('qty')
}
$cart = Cart::add([
'id' => $pdt->id,
'name' => $pdt->name,
'qty' => $qty,
'price' => $pdt->price
]);
}
Then, at the top of your controller, add:
use Illuminate\Http\Request;
So i found that it needed a json object to work and i had to put this code at the end of the store method :
return response()->json(['cart' => $cart], 201);

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 set manufacturer via web service in Prestashop 1.6?

It seems to be a stupid question, but i need to set via web service, my product, quantity and manufacturer.
There is no more documentation in web about this.
When i insert a product to prestashop via web service , it's important declare:
unset($resources->position_in_category);
unset($resources->manufacturer_name);
unset($resources->quantity);
Only in this way, it's works.
Now, i found a solution to set quantity, but nothing about manufacturer.
Can Someone help me please ?
Thanks.
Get the manufacturer ID and in Products resource, assign it by:
$resources-> id_manufacturer = MANUFACTURER_ID
Example on getting Manufacturer ID by its name
function GetManufacturerID($name) {
try {
$webService = new PrestaShopWebservice($url, PS_WS_AUTH_KEY, DEBUG);
$opt = array(
'resource' =>'manufacturers',
'display' => '[id]',
'filter[name]' => $name);
$xml = $webService->get($opt);
return $xml->children()->children()->manufacturer->id;
} catch (PrestaShopWebserviceException $e) {
$trace = $e->getTrace();
}
}
Example to add new manufacturer
function AddManufacturer($manu_name) {
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService->get(array('resource' => 'manufacturers?schema=synopsis'));
$resources = $xml->children()->children();
$resources->name = $manu_name;
$resources->active = 1;
unset($resources -> link_rewrite);
$webService->add(
array(
'resource' => 'manufacturers',
'active' => array(),
'postXml' => $xml->asXML()
)
);
}

Magento API - Get Product Names from specific Category

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)

Drupal Creating Votes in Voting API Through Code

I have a custom module I'm writing, part of what I want it to do is create a vote associated with a node, I'm trying to figure out how to call the voting API from my module. I loookd in the documentation but it's a little sparse.
Here is an example from a module I wrote a while ago.
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
$node_terms = taxonomy_node_get_terms($node);
$vote['value'] = 0;
$vote['value_type'] = 'points';
foreach ($node_terms as $term) {
$vote['value'] = $vote['value'] + $users_tags[$term->name];
}
$vote['content_id'] = $node->nid;
if (isset($vote['content_id'])) {
votingapi_set_votes($vote);
}
}
Just another example of using this:
function _ept_set_vote($nid, $status, $uid = NULL) {
global $user;
$vote = array(
array(
'entity_type' => 'node',
'value' => 1,
'entity_id' => $nid,
'uid' => (!$uid) ? $user->uid : $uid,
'tag' => $status
)
);
votingapi_set_votes($vote, array());
}
I call it like this:
switch($task_status){
case('start'):
_ept_set_vote($nid, "Start");
break;
case('completed'):
_ept_set_vote($nid, "Completed");
break;
}