Skip to content

Commit 90e18ed

Browse files
committed
ACP2E-2275: REST API /rest/V1/products/attributes doesn't distinguish between "Filterable(with results)" and "Filterable(no results)" options
1 parent c11ef9a commit 90e18ed

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* ADOBE CONFIDENTIAL
5+
* ___________________
6+
*
7+
* Copyright 2014 Adobe
8+
* All Rights Reserved.
9+
*
10+
* NOTICE: All information contained herein is, and remains
11+
* the property of Adobe and its suppliers, if any. The intellectual
12+
* and technical concepts contained herein are proprietary to Adobe
13+
* and its suppliers and are protected by all applicable intellectual
14+
* property laws, including trade secret and copyright laws.
15+
* Dissemination of this information or reproduction of this material
16+
* is strictly forbidden unless prior written permission is obtained
17+
* from Adobe.
18+
* ************************************************************************
19+
*/
20+
declare(strict_types=1);
21+
22+
namespace Magento\Catalog\Api;
23+
24+
/**
25+
* Intended to allow setting 'is_filterable' property for specific attribute as integer value via REST/SOAP API
26+
*
27+
* @api
28+
*/
29+
interface ProductAttributeIsFilterableManagementInterface
30+
{
31+
/**
32+
* Retrieve 'is_filterable' property for specific attribute as integer
33+
*
34+
* @param string $attributeCode
35+
* @return int
36+
* @throws \Magento\Framework\Exception\NoSuchEntityException
37+
*/
38+
public function get(string $attributeCode): int;
39+
40+
/**
41+
* Set 'is_filterable' property for specific attribute as integer
42+
*
43+
* @param string $attributeCode
44+
* @param int $isFilterable
45+
* @return bool
46+
* @throws \Magento\Framework\Exception\NoSuchEntityException
47+
* @throws \Magento\Framework\Exception\InputException
48+
* @throws \Magento\Framework\Exception\StateException
49+
*/
50+
public function set(string $attributeCode, int $isFilterable): bool;
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* ADOBE CONFIDENTIAL
5+
* ___________________
6+
*
7+
* Copyright 2014 Adobe
8+
* All Rights Reserved.
9+
*
10+
* NOTICE: All information contained herein is, and remains
11+
* the property of Adobe and its suppliers, if any. The intellectual
12+
* and technical concepts contained herein are proprietary to Adobe
13+
* and its suppliers and are protected by all applicable intellectual
14+
* property laws, including trade secret and copyright laws.
15+
* Dissemination of this information or reproduction of this material
16+
* is strictly forbidden unless prior written permission is obtained
17+
* from Adobe.
18+
* ************************************************************************
19+
*/
20+
declare(strict_types=1);
21+
22+
namespace Magento\Catalog\Model\Product\Attribute;
23+
24+
use Magento\Catalog\Api\ProductAttributeIsFilterableManagementInterface;
25+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
26+
27+
class IsFilterableManagement implements ProductAttributeIsFilterableManagementInterface
28+
{
29+
/**
30+
* @var ProductAttributeRepositoryInterface
31+
*/
32+
private ProductAttributeRepositoryInterface $productAttributeRepository;
33+
34+
/**
35+
* @param ProductAttributeRepositoryInterface $productAttributeRepository
36+
*/
37+
public function __construct(
38+
ProductAttributeRepositoryInterface $productAttributeRepository
39+
) {
40+
$this->productAttributeRepository = $productAttributeRepository;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function get(string $attributeCode): int
47+
{
48+
$attribute = $this->productAttributeRepository->get($attributeCode);
49+
50+
return (int)$attribute->getIsFilterable();
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
public function set(string $attributeCode, int $isFilterable): bool
57+
{
58+
$attribute = $this->productAttributeRepository->get($attributeCode);
59+
$attribute->setIsFilterable($isFilterable);
60+
$this->productAttributeRepository->save($attribute);
61+
62+
return true;
63+
}
64+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<preference for="Magento\Catalog\Model\ProductLink\Data\ListCriteriaInterface" type="Magento\Catalog\Model\ProductLink\Data\ListCriteria" />
7878
<preference for="Magento\Catalog\Api\CategoryListDeleteBySkuInterface" type="Magento\Catalog\Model\CategoryLinkRepository"/>
7979
<preference for="Magento\Theme\CustomerData\MessagesProviderInterface" type="Magento\Catalog\Model\Theme\CustomerData\MessagesProvider"/>
80+
<preference for="Magento\Catalog\Api\ProductAttributeIsFilterableManagementInterface" type="Magento\Catalog\Model\Product\Attribute\IsFilterableManagement" />
8081
<type name="Magento\Customer\Model\ResourceModel\Visitor">
8182
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
8283
</type>

app/code/Magento/Catalog/etc/webapi.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
<resource ref="Magento_Catalog::attributes_attributes" />
5252
</resources>
5353
</route>
54+
<route url="/V1/products/attributes/:attributeCode/is-filterable" method="GET">
55+
<service class="Magento\Catalog\Api\ProductAttributeIsFilterableManagementInterface" method="get"/>
56+
<resources>
57+
<resource ref="Magento_Catalog::attributes_attributes" />
58+
</resources>
59+
</route>
5460
<route url="/V1/products/attributes" method="GET">
5561
<service class="Magento\Catalog\Api\ProductAttributeRepositoryInterface" method="getList" />
5662
<resources>
@@ -87,6 +93,12 @@
8793
<resource ref="Magento_Catalog::attributes_attributes" />
8894
</resources>
8995
</route>
96+
<route url="/V1/products/attributes/:attributeCode/is-filterable/:isFilterable" method="PUT">
97+
<service class="Magento\Catalog\Api\ProductAttributeIsFilterableManagementInterface" method="set"/>
98+
<resources>
99+
<resource ref="Magento_Catalog::attributes_attributes" />
100+
</resources>
101+
</route>
90102
<route url="/V1/products/attributes/:attributeCode" method="DELETE">
91103
<service class="Magento\Catalog\Api\ProductAttributeRepositoryInterface" method="deleteById"/>
92104
<resources>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* ADOBE CONFIDENTIAL
5+
* ___________________
6+
*
7+
* Copyright 2014 Adobe
8+
* All Rights Reserved.
9+
*
10+
* NOTICE: All information contained herein is, and remains
11+
* the property of Adobe and its suppliers, if any. The intellectual
12+
* and technical concepts contained herein are proprietary to Adobe
13+
* and its suppliers and are protected by all applicable intellectual
14+
* property laws, including trade secret and copyright laws.
15+
* Dissemination of this information or reproduction of this material
16+
* is strictly forbidden unless prior written permission is obtained
17+
* from Adobe.
18+
* ************************************************************************
19+
*/
20+
declare(strict_types=1);
21+
22+
namespace Magento\Catalog\Api;
23+
24+
use Magento\Catalog\Setup\CategorySetup;
25+
use Magento\Catalog\Test\Fixture\Attribute;
26+
use Magento\Framework\Webapi\Rest\Request;
27+
use Magento\TestFramework\Fixture\DataFixture;
28+
use Magento\TestFramework\TestCase\WebapiAbstract;
29+
30+
#[
31+
DataFixture(
32+
Attribute::class,
33+
[
34+
'entity_type_id' => CategorySetup::CATALOG_PRODUCT_ENTITY_TYPE_ID,
35+
'attribute_code' => 'product_custom_attribute',
36+
'is_filterable' => false,
37+
'frontend_input' => 'select',
38+
'backend_type' => 'int',
39+
]
40+
),
41+
]
42+
class ProductAttributeIsFilterableManagementTest extends WebapiAbstract
43+
{
44+
private const SERVICE_NAME = 'catalogProductAttributeIsFilterableManagementV1';
45+
private const SERVICE_VERSION = 'V1';
46+
private const RESOURCE_PATH = '/V1/products/attributes/%s/is-filterable';
47+
48+
/**
49+
* @return void
50+
*/
51+
public function testGet(): void
52+
{
53+
$isFilterable = $this->getAttributeIsFilterable('product_custom_attribute');
54+
55+
$this->assertEquals(0, $isFilterable);
56+
}
57+
58+
/**
59+
* @return void
60+
*/
61+
public function testSet(): void
62+
{
63+
$attributeCode = 'product_custom_attribute';
64+
$isFilterableIntValue = 2;
65+
$serviceInfo = [
66+
'rest' => [
67+
'resourcePath' => sprintf(self::RESOURCE_PATH, $attributeCode) . '/' . $isFilterableIntValue,
68+
'httpMethod' => Request::HTTP_METHOD_PUT,
69+
],
70+
'soap' => [
71+
'service' => self::SERVICE_NAME,
72+
'serviceVersion' => self::SERVICE_VERSION,
73+
'operation' => self::SERVICE_NAME . 'Set',
74+
],
75+
];
76+
77+
$response = $this->_webApiCall(
78+
$serviceInfo,
79+
[
80+
'attributeCode' => $attributeCode,
81+
'isFilterable' => $isFilterableIntValue,
82+
]
83+
);
84+
$this->assertTrue($response);
85+
$this->assertEquals(
86+
$isFilterableIntValue,
87+
$this->getAttributeIsFilterable($attributeCode)
88+
);
89+
}
90+
91+
/**
92+
* @param string $attributeCode
93+
* @return int
94+
*/
95+
private function getAttributeIsFilterable(string $attributeCode): int
96+
{
97+
$serviceInfo = [
98+
'rest' => [
99+
'resourcePath' => sprintf(self::RESOURCE_PATH, $attributeCode),
100+
'httpMethod' => Request::HTTP_METHOD_GET,
101+
],
102+
'soap' => [
103+
'service' => self::SERVICE_NAME,
104+
'serviceVersion' => self::SERVICE_VERSION,
105+
'operation' => self::SERVICE_NAME . 'Get',
106+
],
107+
];
108+
109+
return $this->_webApiCall($serviceInfo, ['attributeCode' => $attributeCode]);
110+
}
111+
}

0 commit comments

Comments
 (0)