Check if product exists without exception in API
-
16-10-2019 - |
Question
Is there a way to check whether a product already exists in Magento using the SOAP API that doesn't require using exception handling for the control flow?
I have the following code that works, but I wouldn't say it follows best practices.
Dim productExists As Boolean
Try
Dim webService As MagentoService = New MagentoWS.MagentoService
Dim productInfo As catalogProductReturnEntity = webService.catalogProductInfo(sSessionId, sku, String.Empty, Nothing, "sku")
productExists = True
Catch ex As SoapHeaderException
productExists = False
End Try
I have to catch the SoapHeaderException and then use the fact that the exception was thrown to know that the product doesn't exist.
Solution
I'm not sure how .NET
s SOAP API would handle this, but I'd try using the catalog_product.list
method with a SKU filter. If the resulting list/array/collection/whatever-its-serialized-as is empty, then you know the product doesn't exist.
Per the comments below, the following code should get you that list
Dim productExists As Boolean
Dim webService As MagentoService = New MagentoWS.MagentoService
Dim filters As filters = New filters()
filters.filter = {New associativeEntity() With {.key = "sku", .value = sku}}
Dim productInfo As catalogProductEntity() = webService.catalogProductList(sSessionId, filters, String.Empty)
productExists = productInfo.Count() > 0
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange