How to filter products using category_id and brand in magento soap API v2
-
13-12-2019 - |
Question
I have created a function to filter products by brands as follows:
$complexFilter = array(
'complex_filter' => array(
array(
'key' => 'manufacturer',
'value' => array('key' => 'like', 'value' => $brand_id)
),
array(
'key' => 'name',
'value' => array('key' => 'like', 'value' => '%'.$name_keyword.'%')
)
)
);
$result = $soapClient->catalogProductList($sessionId, $complexFilter);
var_dump ($result);
This function return correct data. but when I used category_ids in complexFilter it return me exception.
My Custom function for filter by brands and category_ids is as follows:
$complexFilter = array(
'complex_filter' => array(
array(
'key' => 'category_ids',
'value' => array('key' => 'in', 'value' => array("353"))
),
array(
'key' => 'manufacturer',
'value' => array('key' => 'like', 'value' => $brand_id)
)
)
);
$result = $soapClient->catalogProductList($sessionId, $complexFilter);
var_dump ($result);
And Exception are :
{"faultstring":"Call to a member function getBackend() on a non-object","faultcode":"SOAP-ENV:Server"}
How to filter products using category_id and brand in Magento SOAP API?
Solution
Finallly I have found my solution :
$allProductValue = (array)$soapClient->catalogCategoryAssignedProducts($sessionId, $category_id);
$result = array();
if(!empty($allProductValue))
{
for($i = 0;$i < count($allProductValue);$i++)
{
$product_id = $allProductValue[$i]->product_id;
/* get product info from product id */
$attributes = new stdclass();
$attributes->attributes = array('name', 'short_description', 'price');
$attributes->additional_attributes = array('manufacturer');
$productInfo = $soapClient->catalogProductInfo($sessionId, $product_id,'1',$attributes);
$product_info = get_object_vars($productInfo);
$manufacturer = get_object_vars($product_info['additional_attributes'][0]);
if($manufacturer['value'] == $brand_id)
{
array_push($result,$product_info);
}
}
if(!empty($result))
{
$returnResult = array('ProductList' => $result);
}
else
{
$returnResult = array('Message' => 'No Products founds of this brand');
}
}
else
{
$returnResult = array('Message' => 'No Products founds of this Category');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange