Skip to content

Commit e016cc2

Browse files
a.laurowskiAleksander Laurowski
authored andcommitted
FFWEB-2332 : add PPP configuration
1 parent c6c1da4 commit e016cc2

9 files changed

Lines changed: 158 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Changelog
2+
# [Unreleased]
3+
### Add
4+
- Search Result Page, Category Page
5+
- add Products per Page configurartion which allows user to define custom configuration without any change in code
6+
27
## [v2.5.0] - 2022.03.04
38
- Import
49
- fix error while push import feed using CLI after generate it for API version 7.x

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"magento/magento-coding-standard": "^6.0",
2424
"pdepend/pdepend": "^2.8",
2525
"phpmd/phpmd": "^2.9",
26-
"phpunit/phpunit": ">=6.5 <10.0"
26+
"phpunit/phpunit": ">=9.5 <10.0"
2727
},
2828
"autoload": {
2929
"files": [
28.9 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\Factfinder\Block\Adminhtml\System\Config\Field;
6+
7+
use Magento\Backend\Block\Template\Context;
8+
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
9+
use Magento\Config\Model\Config\Source\Yesno;
10+
use Magento\Framework\Data\OptionSourceInterface;
11+
use Magento\Framework\DataObject;
12+
13+
/**
14+
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
15+
*/
16+
class ProductsPerPage extends AbstractFieldArray
17+
{
18+
19+
protected function _prepareToRender()
20+
{
21+
$this->_addAfter = false;
22+
$this->addColumn('value', [
23+
'label' => __('Num of records'),
24+
'class' => 'required-entry',
25+
]);
26+
}
27+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\Factfinder\Test\Unit\ViewModel;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
use Magento\Framework\Serialize\Serializer\Json;
9+
use Omikron\Factfinder\ViewModel\ProductsPerPage;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ProductPerPageTest extends TestCase
13+
{
14+
/** @var ProductsPerPage */
15+
private $productsPerPage;
16+
17+
private $scopeConfigMock;
18+
19+
public function test_will_unserialize_stored_values()
20+
{
21+
$this->scopeConfigMock->method('getValue')->with('factfinder/components_options/products_per_page')
22+
->willReturn('{"_1648807417584_584":{"value":"8"},"_1648807420544_544":{"value":"12"},"_1648807422801_801":{"value":"16"}}');
23+
24+
$this->assertIsString($this->productsPerPage->getProductsPerPageConfiguration());
25+
}
26+
27+
public function test_output_have_correct_structure()
28+
{
29+
$this->scopeConfigMock->method('getValue')->with('factfinder/components_options/products_per_page')
30+
->willReturn('{"_1648807417584_584":{"value":"8"},"_1648807420544_544":{"value":"12"},"_1648807422801_801":{"value":"16"}}');
31+
32+
$actual = $this->productsPerPage->getProductsPerPageConfiguration();
33+
34+
$this->assertStringContainsString('{"value":"8","selected":false,"default":false}', $actual);
35+
}
36+
37+
public function test_return_empty_array_literal_if_no_stored_values()
38+
{
39+
$this->scopeConfigMock->method('getValue')->with('factfinder/components_options/products_per_page')
40+
->willReturn(null);
41+
42+
$this->assertEquals('[]', $this->productsPerPage->getProductsPerPageConfiguration());
43+
}
44+
45+
protected function setUp(): void
46+
{
47+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
48+
$this->productsPerPage = new ProductsPerPage(
49+
$this->scopeConfigMock,
50+
new Json(),
51+
);
52+
}
53+
}

src/ViewModel/ProductsPerPage.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\Factfinder\ViewModel;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
use Magento\Framework\Serialize\Serializer\Json;
9+
use Magento\Framework\Serialize\SerializerInterface;
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
12+
class ProductsPerPage implements ArgumentInterface
13+
{
14+
private const PRODUCT_PER_PAGE_CONFIG_PATH = 'factfinder/components_options/products_per_page';
15+
16+
/** @var ScopeConfigInterface */
17+
private $scopeConfig;
18+
19+
/** @var SerializerInterface */
20+
private $serializer;
21+
22+
public function __construct(ScopeConfigInterface $scopeConfig, Json $json)
23+
{
24+
$this->scopeConfig = $scopeConfig;
25+
$this->serializer = $json;
26+
}
27+
28+
public function getProductsPerPageConfiguration(): string
29+
{
30+
$storedValue = $this->scopeConfig->getValue(self::PRODUCT_PER_PAGE_CONFIG_PATH);
31+
$unserialized = array_map(function (array $row) {
32+
return $row + [
33+
'selected' => false,
34+
'default' => false
35+
];
36+
}, array_values($this->serializer->unserialize($storedValue ?: '[]')));
37+
38+
return count($unserialized) ? $this->serializer->serialize($unserialized) : '[]';
39+
}
40+
}

src/etc/adminhtml/system/components.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,16 @@
5353
<label>Max Result for Recommendation</label>
5454
<tooltip>This will apply to a template offered by the module. If you have overriden it, you must manually provide stored value to the frontend</tooltip>
5555
</field>
56+
57+
<field id="products_per_page" translate="label comment" sortOrder="10" showInDefault="1" showInWebsite="1">
58+
<label>Product per Page Configuration</label>
59+
<frontend_model>Omikron\Factfinder\Block\Adminhtml\System\Config\Field\ProductsPerPage</frontend_model>
60+
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
61+
<comment>
62+
This configuration allows you to add new product per page entries to the existing default entry which you define in FACT-Finder UI.
63+
Please remember that the default entry received from FACT-Finder response will be always put as first.
64+
Because of that, all entries here should have bigger values than default entry. There is no possibility to sort all entries before rendering
65+
</comment>
66+
</field>
5667
</group>
5768
</include>

src/view/frontend/layout/default.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<block class="Magento\Framework\View\Element\Template" name="factfinder.communication" ifconfig="factfinder/general/is_enabled" template="Omikron_Factfinder::ff/communication.phtml" before="-">
2020
<arguments>
2121
<argument name="view_model" xsi:type="object">Omikron\Factfinder\ViewModel\Communication</argument>
22+
<argument name="ppp" xsi:type="helper" helper="Omikron\Factfinder\ViewModel\Cart::getItemIds" />
23+
</arguments>
24+
</block>
25+
<block class="Magento\Framework\View\Element\Template" name="factfinder.communication.ppp" ifconfig="factfinder/general/is_enabled" template="Omikron_Factfinder::ff/products-per-page-configuration.phtml" after="factfinder.communication">
26+
<arguments>
27+
<argument name="view_model" xsi:type="object">Omikron\Factfinder\ViewModel\ProductsPerPage</argument>
2228
</arguments>
2329
</block>
2430
<block class="Magento\Framework\View\Element\Template" name="factfinder.search.redirect" ifconfig="factfinder/general/is_enabled" template="Omikron_Factfinder::ff/search-redirect.phtml" after="factfinder.communication" />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/** @var Magento\Framework\View\Element\Template $block */
3+
/** @var Omikron\Factfinder\ViewModel\ProductsPerPage $viewModel */
4+
$viewModel = $block->getViewModel();
5+
?>
6+
7+
<script>
8+
document.addEventListener('ffReady', function () {
9+
const conf = <?= /* @noEscape */ $viewModel->getProductsPerPageConfiguration() ?>;
10+
if (conf.length > 0) {
11+
const first = [factfinder.communication.pppConf.entries[0]];
12+
factfinder.communication.pppConf.entries = first.concat(conf);
13+
}
14+
});
15+
</script>

0 commit comments

Comments
 (0)