Skip to content

Commit c349a2f

Browse files
committed
[feat] GraphQl support
1 parent e9dd8fa commit c349a2f

9 files changed

Lines changed: 73 additions & 15 deletions

File tree

Model/AdditionalAttribute.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Ghoster\OutOfStockAtLast\Model;
4+
5+
class AdditionalAttribute
6+
{
7+
public const ATTRIBUTE_CODE = 'out_of_stock_at_last';
8+
}

Model/Elasticsearch/Adapter/DataMapper/Stock.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace GhoSter\OutOfStockAtLast\Model\Elasticsearch\Adapter\DataMapper;
55

6+
use Ghoster\OutOfStockAtLast\Model\AdditionalAttribute;
67
use Magento\Framework\Exception\NoSuchEntityException;
78
use Magento\Store\Model\StoreManagerInterface;
89
use GhoSter\OutOfStockAtLast\Model\ResourceModel\Inventory;
@@ -49,14 +50,14 @@ public function map($entityId, $storeId): array
4950
$sku = $this->inventory->getSkuRelation((int)$entityId);
5051

5152
if (!$sku) {
52-
return ['out_of_stock_at_last' => 1];
53+
return [AdditionalAttribute::ATTRIBUTE_CODE => 1];
5354
}
5455

5556
$value = $this->inventory->getStockStatus(
5657
$sku,
5758
$this->storeManager->getStore($storeId)->getWebsite()->getCode()
5859
);
5960

60-
return ['out_of_stock_at_last' => $value];
61+
return [AdditionalAttribute::ATTRIBUTE_CODE => $value];
6162
}
6263
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace GhoSter\OutOfStockAtLast\Plugin\DataProvider\Product\SearchCriteriaBuilder;
3+
4+
use Ghoster\OutOfStockAtLast\Model\AdditionalAttribute;
5+
6+
class AddDefaultOrders
7+
{
8+
/**
9+
* Add default sorting
10+
*
11+
* phpcs:disable Magento2.Annotation.MethodArguments.NoTypeSpecified
12+
*
13+
* @param $subject
14+
* @param array $args
15+
* @param bool $includeAggregation
16+
* @return array
17+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
18+
*/
19+
public function beforeBuild(
20+
$subject,
21+
array $args,
22+
bool $includeAggregation
23+
): array {
24+
if (!isset($args['sort'])) {
25+
$args['sort'] = [];
26+
}
27+
28+
if (!isset($args['sort'][AdditionalAttribute::ATTRIBUTE_CODE])
29+
) {
30+
$args['sort'][AdditionalAttribute::ATTRIBUTE_CODE] = 'DESC';
31+
}
32+
33+
return [$args, $includeAggregation];
34+
}
35+
}

Plugin/Model/Adapter/AdditionalFieldMapperPlugin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace GhoSter\OutOfStockAtLast\Plugin\Model\Adapter;
55

6+
use Ghoster\OutOfStockAtLast\Model\AdditionalAttribute;
7+
68
/**
79
* Class AdditionalFieldMapperPlugin for es attributes mapping
810
*/
@@ -12,7 +14,7 @@ class AdditionalFieldMapperPlugin
1214
* @var string[]
1315
*/
1416
protected $allowedFields = [
15-
'out_of_stock_at_last' => 'integer'
17+
AdditionalAttribute::ATTRIBUTE_CODE => 'integer'
1618
];
1719

1820
/**

Plugin/Model/Adapter/BatchDataMapper/ProductDataMapperPlugin.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public function __construct(StockDataMapper $stockDataMapper, Inventory $invento
3939
* Map more attributes
4040
*
4141
* @param ProductDataMapper $subject
42-
* @param mixed $documents
43-
* @param mixed $documentData
42+
* @param array $documents
43+
* @param array $documentData
4444
* @param mixed $storeId
45-
* @param mixed $context
46-
* @return mixed
45+
* @param array $context
46+
* @return array
4747
* @throws NoSuchEntityException
4848
*
4949
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5050
*/
5151
public function afterMap(
5252
ProductDataMapper $subject,
53-
$documents,
54-
$documentData,
53+
array $documents,
54+
array $documentData,
5555
$storeId,
56-
$context
57-
) {
56+
array $context
57+
): array {
5858
$this->inventory->saveRelation(array_keys($documents));
5959

6060
foreach ($documents as $productId => $document) {

Plugin/Model/ResourceModel/Product/CollectionPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace GhoSter\OutOfStockAtLast\Plugin\Model\ResourceModel\Product;
55

6+
use Ghoster\OutOfStockAtLast\Model\AdditionalAttribute;
67
use Magento\Catalog\Model\ResourceModel\Product\Collection;
78
use Magento\Framework\DB\Select;
89

@@ -87,7 +88,7 @@ private function applyOutOfStockAtLastOrders(Collection $collection)
8788
{
8889
if (!$collection->getFlag('sorted_by_oos_flag')) {
8990
$collection->setFlag('sorted_by_oos_flag', true);
90-
$collection->setOrder('out_of_stock_at_last', Select::SQL_DESC);
91+
$collection->setOrder(AdditionalAttribute::ATTRIBUTE_CODE, Select::SQL_DESC);
9192
}
9293
}
9394

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "ghoster/module-outofstockatlast",
33
"description": "Magento 2.4.x module Sort Out Of Stock Product At last the product list",
44
"require": {
5-
"php": "~7.4.0||~8.0.0||~8.1.0||~8.2.0||~8.3.0||~8.4.0",
6-
"magento/framework": "^102.0|^103.0"
5+
"php": ">=8.1.0",
6+
"magento/framework": ">=102.0.0"
77
},
88
"minimum-stability": "stable",
99
"archive": {

etc/graphql/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<type name="Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder">
4+
<plugin name="GhoSter_OutOfStockAtLast::add-default-orders" type="GhoSter\OutOfStockAtLast\Plugin\DataProvider\Product\SearchCriteriaBuilder\AddDefaultOrders" sortOrder="10" />
5+
</type>
6+
</config>

etc/module.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
4-
<module name="GhoSter_OutOfStockAtLast" setup_version="1.0.0">
4+
<module name="GhoSter_OutOfStockAtLast">
55
<sequence>
66
<module name="Magento_Catalog"/>
77
<module name="Magento_Elasticsearch7"/>
8+
<module name="Magento_ConfigurableProduct"/>
9+
<module name="Magento_CatalogGraphQl"/>
10+
<module name="Magento_Store"/>
11+
<module name="Magento_Elasticsearch"/>
12+
<module name="Magento_CatalogInventory"/>
813
</sequence>
914
</module>
1015
</config>

0 commit comments

Comments
 (0)