Skip to content

Commit

Permalink
Implemented category base functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jansentjeu committed Dec 6, 2024
1 parent bc8ab8f commit 65cf055
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Magento 2 module for Tweakwise JS",
"type": "magento2-module",
"require": {
"php": "^8.0"
"php": "^8.2",
"tweakwise/magento2-tweakwise-export": "^7.0"
},
"require-dev": {
"magento/framework": "*",
Expand Down
38 changes: 38 additions & 0 deletions src/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
private readonly StoreManagerInterface $storeManager
) {
parent::__construct($context);
}

/**
* @param int $entityId
* @return string
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function getTweakwiseId(int $entityId): string
{
$storeId = $this->storeManager->getStore()->getId();
return '100014';
// return $this->exportHelper->getTweakwiseId($storeId, $entityId); // TODO: require export module
}
}
48 changes: 48 additions & 0 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Config
{
private const XML_PATH_ENABLED = 'tweakwisejs/general/enabled';
private const XML_PATH_INSTANCE_KEY = 'tweakwisejs/general/instance_key';

private const XML_PATH_MERCHANDISING_ENABLED = 'tweakwisejs/merchandising/enabled';

/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
private readonly ScopeConfigInterface $scopeConfig
) {
}

/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE);
}

/**
* @return string|null
*/
public function getInstanceKey(): ?string
{
return $this->scopeConfig->getValue(self::XML_PATH_INSTANCE_KEY, ScopeInterface::SCOPE_STORE);
}

/**
* @return bool
*/
public function isMerchandisingEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_MERCHANDISING_ENABLED, ScopeInterface::SCOPE_STORE);
}
}
56 changes: 56 additions & 0 deletions src/Observer/AddPageAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\View\Page\Config as PageConfig;
use Tweakwise\TweakwiseJs\Model\Config;

class AddPageAssets implements ObserverInterface
{
/**
* @param PageConfig $pageConfig
* @param Config $config
*/
public function __construct(
private readonly PageConfig $pageConfig,
private readonly Config $config
) {
}

/**
* @param Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(Observer $observer)
{
if (!$this->config->isEnabled()) {
return;
}

$instanceKey = $this->config->getInstanceKey();

$this->pageConfig->addRemotePageAsset(
'https://gateway.tweakwisenavigator.net/js/starter.js',
'link',
['attributes' => ['rel' => 'preload', 'as' => 'script']]
);
$this->pageConfig->addRemotePageAsset(
sprintf('https://gateway.tweakwisenavigator.net/js/%s/tweakwise.js', $instanceKey),
'link',
['attributes' => ['rel' => 'preload', 'as' => 'script']]
);
$this->pageConfig->addRemotePageAsset(
sprintf('https://gateway.tweakwisenavigator.net/js/%s/tweakwise.js', $instanceKey),
'js',
['attributes' => [
'data-failover' => sprintf('https://gateway.tweakwisenavigator.com/js/%s/tweakwise.js', $instanceKey),
'onerror' => 'window.tweakwiseFailover(this.dataset.failover)'
]]
);
}
}
117 changes: 117 additions & 0 deletions src/Observer/ManageLayoutBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Observer;

use Magento\Catalog\Block\Category\View;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Layout;
use Tweakwise\TweakwiseJs\Model\Config;
use Tweakwise\TweakwiseJs\ViewModel\TweakwiseJs;

class ManageLayoutBlocks implements ObserverInterface
{
/**
* @param Http $request
* @param Config $config
* @param TweakwiseJs $viewModel
* @param Resolver $layerResolver
*/
public function __construct(
private readonly Http $request,
private readonly Config $config,
private readonly TweakwiseJs $viewModel,
private readonly Resolver $layerResolver
) {
}

/**
* @param Observer $observer
* @return void
* @throws LocalizedException
*/
public function execute(Observer $observer)
{
if (!$this->config->isEnabled()) {
return;
}

$layout = $observer->getLayout();

$this->addDefaultBlock($layout);

if (!$this->isCategoryPage() || !$this->showTweakwiseJsCategoryViewBlock()) {
return;
}

$this->addTweakwiseJsCategoryViewBlock($layout);
}

/**
* @param Layout $layout
* @return void
*/
private function addDefaultBlock(Layout $layout): void
{
$blockName = 'tweakwise-js-default';
$layout->createBlock(Template::class, $blockName)
->setTemplate('Tweakwise_TweakwiseJs::default.phtml');
$layout->setChild('after.body.start', $blockName, $blockName);
}

/**
* @return bool
*/
private function isCategoryPage(): bool
{
return $this->request->getFullActionName() === 'catalog_category_view';
}

/**
* @param Layout $layout
* @return void
*/
private function addTweakwiseJsCategoryViewBlock(Layout $layout): void
{
$blockName = 'tweakwise-js-lister';
$layout->createBlock(
View::class,
$blockName,
[
'data' => [
'view_model' => $this->viewModel
]
]
)->setTemplate('Tweakwise_TweakwiseJs::category/listing.phtml');
$layout->setChild('page.wrapper', $blockName, $blockName);
}

/**
* @return bool
*/
private function showTweakwiseJsCategoryViewBlock(): bool
{
if (!$this->config->isMerchandisingEnabled()) {
return false;
}

$currentCategory = $this->layerResolver->get()->getCurrentCategory();
if (!$currentCategory) {
return false;
}

$displayMode = $currentCategory->getDisplayMode();
if ($displayMode && $displayMode === Category::DM_PAGE) {
return false;
}

return true;
}
}
31 changes: 31 additions & 0 deletions src/ViewModel/TweakwiseJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\ViewModel;

use Magento\Catalog\Block\Category\View;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Tweakwise\TweakwiseJs\Helper\Data;

class TweakwiseJs implements ArgumentInterface
{
/**
* @param Data $dataHelper
*/
public function __construct(
private readonly Data $dataHelper
) {
}

/**
* @param View $block
* @return string
* @throws NoSuchEntityException
*/
public function getTweakwiseCategoryId(View $block): string
{
return $this->dataHelper->getTweakwiseId((int) $block->getCurrentCategory()->getId());
}
}
41 changes: 41 additions & 0 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="tweakwisejs" translate="label" type="text" sortOrder="130" showInDefault="1" showInWebsite="1"
showInStore="0">
<label>TweakwiseJS</label>
<tab>catalog</tab>
<resource>Tweakwise_TweakwiseJs::config</resource>
<group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="instance_key" translate="label comment" type="text" sortOrder="20" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Instance Key</label>
<comment>Insert your Tweakwise instance key here</comment>
<validate>required-entry</validate>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
<group id="merchandising" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Merchandising</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable or disable the Tweakwise merchandising functionality</comment>
</field>
<depends>
<field id="tweakwisejs/general/enabled">1</field>
</depends>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<tweakwisejs>
<merchandising>
<category_products_list_block_name>category.products.list</category_products_list_block_name>
<sidebar_block_name>sidebar.main</sidebar_block_name>
</merchandising>
</tweakwisejs>
</default>
</config>
12 changes: 12 additions & 0 deletions src/etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_generate_blocks_after">
<observer name="Tweakwise_TweakwiseJs_Observer_AddJsToPages"
instance="Tweakwise\TweakwiseJs\Observer\AddPageAssets"/>
</event>
<event name="layout_generate_blocks_after">
<observer name="Tweakwise_TweakwiseJs_Observer_ManageLayoutBlocks"
instance="Tweakwise\TweakwiseJs\Observer\ManageLayoutBlocks"/>
</event>
</config>
14 changes: 14 additions & 0 deletions src/view/frontend/templates/category/listing.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types = 1);

use Magento\Catalog\Block\Category\View;

/** @var View $block */

$viewModel = $block->getViewModel();
?>

<script>
window['twn-starter-config'].navigation.cid = <?= $viewModel->getTweakwiseCategoryId($block);?>;
</script>

Loading

0 comments on commit 65cf055

Please sign in to comment.