From f3504e8c543bacbd615e98807d0a2da23f0fc4e0 Mon Sep 17 00:00:00 2001 From: kastoras Date: Tue, 27 Apr 2021 23:47:00 +0300 Subject: [PATCH] Close/CLOSE #17390 Products API, GET /products variants filter Added new parameter for variants filter. Using this filter you can get products without variants only, parrent products only, child products only. --- htdocs/product/class/api_products.class.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 24bb22f41c4..9a04cbc2fea 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -166,10 +166,10 @@ class Products extends DolibarrApi * @param int $category Use this param to filter list by category * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.tobuy:=:0) and (t.tosell:=:1)" * @param bool $ids_only Return only IDs of product instead of all properties (faster, above all if list is long) - * @param bool $without_variants Return only parent products, not variants + * @param int $variant_filter Use this param to filter list (0 = all, 1=products without variants, 2=parent of variants, 3=variants only) * @return array Array of product objects */ - public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $ids_only = false, $without_variants = false) + public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $ids_only = false, $variant_filter = 0) { global $db, $conf; @@ -188,9 +188,16 @@ class Products extends DolibarrApi } $sql .= ' WHERE t.entity IN ('.getEntity('product').')'; - if ($without_variants) { + if ($variant_filter == 1) { + $sql .= ' AND t.rowid not in (select distinct fk_product_parent from '.MAIN_DB_PREFIX.'product_attribute_combination)'; + $sql .= ' AND t.rowid not in (select distinct fk_product_child from '.MAIN_DB_PREFIX.'product_attribute_combination)'; + } + if ($variant_filter == 2) { $sql .= ' AND t.rowid in (select distinct fk_product_parent from '.MAIN_DB_PREFIX.'product_attribute_combination)'; } + if ($variant_filter == 3) { + $sql .= ' AND t.rowid in (select distinct fk_product_child from '.MAIN_DB_PREFIX.'product_attribute_combination)'; + } // Select products of given category if ($category > 0) {