Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PHP endpoints for product filters #1714

Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
25e4e6e
Introduce routes to get product filter options
Quetzacoalt91 Mar 25, 2024
61581e0
Plug AttributeOptionsProvider to the Core
Quetzacoalt91 Mar 26, 2024
5268af2
Add routes to load options for categories and brands
Quetzacoalt91 Mar 26, 2024
e1dd0ab
Repeat key in attribute values
Quetzacoalt91 Apr 2, 2024
75aef33
Rework repositories to limit load of data to a specific shop ID
Quetzacoalt91 Apr 4, 2024
7a1706f
Remove uneeded code
Quetzacoalt91 Apr 4, 2024
85d2665
Introduce route to count products and filter validator class
Quetzacoalt91 Apr 2, 2024
90b5aa7
Add tests
Quetzacoalt91 Apr 2, 2024
413438f
Introduce Query builders
Quetzacoalt91 Apr 3, 2024
1c02ace
Rename array keys from reference to id
Quetzacoalt91 Apr 5, 2024
c0a4b6c
Add features to list of options
Quetzacoalt91 Apr 5, 2024
f5f7cdc
Allow values of attributes and features to be grouped by id
Quetzacoalt91 Apr 5, 2024
29e1f85
Start implementation of filters of features
Quetzacoalt91 Apr 5, 2024
592ae41
Update comments
Quetzacoalt91 Apr 8, 2024
1c52203
feat: add does not contain condition and map attribute with condition…
ga-devfront Apr 24, 2024
0be5e17
feat: remove custom attribute
ga-devfront Apr 24, 2024
d0e1c28
feat: update all query builder according to change on data received
ga-devfront Apr 24, 2024
a9faf2d
fix: lint
ga-devfront Apr 24, 2024
3877d97
fix: remove last custom attribute artifact
ga-devfront Apr 24, 2024
6de2616
fix: typo
ga-devfront Apr 25, 2024
8b0f83b
fix: remove custom attribute options provider
ga-devfront Apr 25, 2024
a4941f7
feat: optimise readability and perf for provider and query builder
ga-devfront Apr 25, 2024
57357ca
fix: typo
ga-devfront Apr 25, 2024
327c4dd
fix: remove custom attribute from product filter config yml
ga-devfront Apr 25, 2024
1160062
fix: remove unused getCustomAttributesWithLocalizedValues function
ga-devfront Apr 25, 2024
b120f9a
feat: add feature query builder
ga-devfront Apr 25, 2024
c9b030b
fix: cs fixer
ga-devfront Apr 25, 2024
c85597f
fix: foreach feature query builder
ga-devfront Apr 25, 2024
78c8d61
fix: use map for brand option provider instead of foreach
ga-devfront Apr 25, 2024
d04e770
fix: typo
ga-devfront Apr 25, 2024
e7967b1
refacto: feature option provider getOptions method
ga-devfront Apr 25, 2024
cf9b362
fix: remove iset in foreach for feature query builder
ga-devfront Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions classes/Adapter/ConfigurationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
namespace PrestaShop\Module\PsxMarketingWithGoogle\Adapter;

use Configuration;
use Shop;

class ConfigurationAdapter
{
/**
* @var Shop
* @var int
*/
private $shopId;

Expand Down
119 changes: 119 additions & 0 deletions classes/ProductFilter/AttributeMapConditionOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter;

class AttributeMapConditionOutput
{
const STRING = 'string';
const INT = 'int';
const BOOLEAN = 'boolean';
const OBJECT = 'object';

const MAP = [
AttributeType::BRAND => [
Condition::DOES_CONTAIN => [
'multiple' => true,
'type' => self::STRING,
],
Condition::DOES_NOT_CONTAIN => [
'multiple' => true,
'type' => self::STRING,
],
Condition::IS => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['value', 'id'],
],
Condition::IS_NOT => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['value', 'id'],
],
],
AttributeType::CATEGORY => [
Condition::DOES_CONTAIN => [
'multiple' => true,
'type' => self::STRING,
],
Condition::DOES_NOT_CONTAIN => [
'multiple' => true,
'type' => self::STRING,
],
Condition::IS => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['value', 'id'],
],
Condition::IS_NOT => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['value', 'id'],
],
],
AttributeType::PRICE => [
Condition::IS => [
'multiple' => false,
'type' => self::INT,
'positive' => false,
],
Condition::GREATER => [
'multiple' => false,
'type' => self::INT,
'positive' => false,
],
Condition::LOWER => [
'multiple' => false,
'type' => self::INT,
'positive' => false,
],
],
AttributeType::PRODUCT_ID => [
Condition::IS => [
'multiple' => true,
'type' => self::INT,
'positive' => true,
],
Condition::IS_NOT => [
'multiple' => true,
'type' => self::INT,
'positive' => true,
],
],
AttributeType::OUT_OF_STOCK => [
Condition::IS => [
'multiple' => false,
'type' => self::BOOLEAN,
],
],
AttributeType::FEATURE => [
Condition::IS => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['id', 'key', 'value', 'language'],
],
Condition::IS_NOT => [
'multiple' => true,
'type' => self::OBJECT,
'keys' => ['id', 'key', 'value', 'language'],
],
],
];
}
47 changes: 47 additions & 0 deletions classes/ProductFilter/AttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter;

/**
* enum only exists from PHP 8 and the module is compliant with PHP 7.2+,
* thus cannot be used here.
*/
class AttributeType
{
const BRAND = 'brand';
const CATEGORY = 'category';
const FEATURE = 'feature';
const PRICE = 'price';
const PRODUCT_ID = 'id';
const OUT_OF_STOCK = 'out_of_stock';

public static function all()
{
return [
static::BRAND,
static::CATEGORY,
static::FEATURE,
static::PRICE,
static::PRODUCT_ID,
static::OUT_OF_STOCK,
];
}
}
35 changes: 35 additions & 0 deletions classes/ProductFilter/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter;

/**
* enum only exists from PHP 8 and the module is compliant with PHP 7.2+,
* thus cannot be used here.
*/
class Condition
{
const DOES_CONTAIN = 'does_contain';
const DOES_NOT_CONTAIN = 'does_not_contain';
const GREATER = 'greater';
const LOWER = 'lower';
const IS = 'is';
const IS_NOT = 'is_not';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder;

use DbQuery;
use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\Condition;

class BrandQueryBuilder implements QueryBuilderInterface
{
public function addWhereFromFilter(DbQuery $query, $filter): DbQuery
{
switch ($filter['condition']) {
case Condition::DOES_CONTAIN:
$queryConditions = [];
foreach ($filter['values'] as $value) {
$queryConditions[] = 'm.name LIKE "%' . pSQL($value) . '%"';
}

return $query->where('(' . implode(' OR ', $queryConditions) . ')');

case Condition::DOES_NOT_CONTAIN:
$queryConditions = [];
foreach ($filter['values'] as $value) {
$queryConditions[] = 'm.name NOT LIKE "%' . pSQL($value) . '%"';
}

return $query->where('(' . implode(' OR ', $queryConditions) . ')');

case Condition::IS:
$filteredValues = array_map(function ($item) {
return $item['value'];
}, $filter['values']);

return $query->where('m.name IN ["' . implode('", "', array_map('pSQL', $filteredValues)) . '"]');

case Condition::IS_NOT:
$filteredValues = array_map(function ($item) {
return $item['value'];
}, $filter['values']);

return $query->where('m.name NOT IN ["' . implode('", "', array_map('pSQL', $filteredValues)) . '"]');
}

return $query;
}

public function addRelations(DbQuery $query): DbQuery
{
return $query->innerJoin('manufacturer', 'm', 'm.id_manufacturer = p.id_manufacturer')
->where('m.active = 1');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder;

use Context;
use DbQuery;
use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\Condition;

class CategoryQueryBuilder implements QueryBuilderInterface
{
/**
* @var Context
*/
protected $context;

public function __construct(Context $context)
{
$this->context = $context;
}

public function addWhereFromFilter(DbQuery $query, $filter): DbQuery
{
// At the time of implementation, CloudSync gets only the default category of the product.
// We add the condition based on the default category here as well.
switch ($filter['condition']) {
case Condition::DOES_CONTAIN:
$queryConditions = [];
foreach ($filter['values'] as $value) {
$queryConditions[] = 'cl.name LIKE "%' . pSQL($value) . '%"';
}

return $query->where('(' . implode(' OR ', $queryConditions) . ')');

case Condition::DOES_NOT_CONTAIN:
$queryConditions = [];
foreach ($filter['values'] as $value) {
$queryConditions[] = 'cl.name NOT LIKE "%' . pSQL($value) . '%"';
}

return $query->where('(' . implode(' OR ', $queryConditions) . ')');

case Condition::IS:
$filteredValues = array_map(function ($item) {
return $item['id'];
}, $filter['values']);

return $query->where('c.id_category_default IN [' . implode(', ', array_map('intval', $filteredValues)) . ']');

case Condition::IS_NOT:
$filteredValues = array_map(function ($item) {
return $item['id'];
}, $filter['values']);

return $query->where('c.id_category_default NOT IN [' . implode(', ', array_map('intval', $filteredValues)) . ']');
}

return $query;
}

public function addRelations(DbQuery $query): DbQuery
{
return $query->innerJoin('category', 'c', 'c.id_category = p.id_category_default')
->innerJoin('category_lang', 'cl', 'c.id_category = cl.id_category')
->where('cl.id_lang = ' . (int) $this->context->language->id)
->where('c.active = 1');
}
}
Loading
Loading