Skip to content

Commit

Permalink
Merge pull request #57 from EmicoEcommerce/beta
Browse files Browse the repository at this point in the history
Merge beta into master
  • Loading branch information
ah-net authored Oct 16, 2024
2 parents c82dfd7 + d7b9cc6 commit 3c74d84
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 148 deletions.
3 changes: 2 additions & 1 deletion src/Block/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getProduct()
$request = $this->context->getRequest();
$productId = $request->getParam('product_id');

if(
if (
!$productId ||
!$this->cacheHelper->isEsiRequest($request) ||
!$this->cacheHelper->personalMerchandisingCanBeApplied()
Expand All @@ -120,6 +120,7 @@ public function getProduct()
/**
* @param string $route
* @param array $params
*
* @return string
*/
public function getUrl($route = '', $params = [])
Expand Down
13 changes: 12 additions & 1 deletion src/Plugin/ViewModel/ProductListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(
* @param string $templateType
* @param string $imageDisplayArea
* @param bool $showDescription
*
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
Expand All @@ -64,7 +65,16 @@ public function aroundGetItemHtmlWithRenderer(
if ($isVisual) {
return $this->getVisualHtml($product);
}
return $proceed($itemRendererBlock, $product, $parentBlock, $viewMode, $templateType, $imageDisplayArea, $showDescription);

return $proceed(
$itemRendererBlock,
$product,
$parentBlock,
$viewMode,
$templateType,
$imageDisplayArea,
$showDescription
);
}

$itemId = (string) $product->getId();
Expand Down Expand Up @@ -102,6 +112,7 @@ public function aroundGetItemHtmlWithRenderer(

/**
* @param Visual $visual
*
* @return string
*/
private function getVisualHtml(Visual $visual): string
Expand Down
19 changes: 19 additions & 0 deletions src/Plugin/ViewModel/SearchForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tweakwise\TweakwiseHyva\Plugin\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Search\Helper\Data as SearchHelper;

class SearchForm implements ArgumentInterface
{
public function __construct(private readonly SearchHelper $searchHelper)
{

}

public function getSearchHelper()
{
return $this->searchHelper;
}
}
59 changes: 30 additions & 29 deletions src/ViewModel/ProductList/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2Tweakwise\Model\Config\TemplateFinder;
use Hyva\Theme\ViewModel\ProductList;
use Magento\Catalog\Block\Product\ProductList\Related;
use Magento\Catalog\Model\Product;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Model\Cart\Crosssell as TweakwiseCrosssell;
Expand Down Expand Up @@ -48,17 +46,20 @@ class Plugin extends AbstractRecommendationPlugin
* @param TemplateFinder $templateFinder
* @param ObjectManagerInterface $objectManager
*/

public function __construct(Config $config, Registry $registry, Context $context, TemplateFinder $templateFinder, ObjectManagerInterface $objectManager)
{
public function __construct(
Config $config,
Registry $registry,
Context $context,
TemplateFinder $templateFinder,
ObjectManagerInterface $objectManager
) {
parent::__construct($config, $registry, $context, $templateFinder);
$this->objectManager = $objectManager;
}

/**
* @return string
*/

protected function getType()
{
return $this->type;
Expand All @@ -77,9 +78,12 @@ public function aroundGetCrosssellItems(ProductList $subject, Closure $proceed,
}

// return most recently added product crosssell items first
usort($cartItems, function (QuoteItem $itemA, QuoteItem $itemB) {
return ($itemA->getCreatedAt() <=> $itemB->getCreatedAt()) * -1;
});
usort(
$cartItems,
function (QuoteItem $itemA, QuoteItem $itemB) {
return ($itemA->getCreatedAt() <=> $itemB->getCreatedAt()) * -1;
}
);

$items = [];

Expand All @@ -99,7 +103,8 @@ public function aroundGetCrosssellItems(ProductList $subject, Closure $proceed,
* @param ProductList $subject
* @param Closure $proceed
* @param string $linkType
* @param Product|ProductInterface|QuoteItem ...$items
* @param Product|ProductInterface|QuoteItem $items
*
* @return ProductInterface[]
*/
public function aroundGetLinkedItems(ProductList $subject, Closure $proceed, string $linkType, $items): array
Expand All @@ -109,14 +114,13 @@ public function aroundGetLinkedItems(ProductList $subject, Closure $proceed, str

/**
* Overwrite Hyva function to get items for upsell/crossell
*
* @param ProductList $subject
* @param Closure $proceed
* @param string $linkType
* @param ...$items
* @param mixed ...$items
*
* @return array|Collection
*/
private function loadLinkedTweakwiseItems(Closure $proceed, string $linkType, ...$items): array
private function loadLinkedTweakwiseItems(Closure $proceed, string $linkType, ...$items): array|Collection
{
$this->type = Config::RECOMMENDATION_TYPE_UPSELL;
if ($linkType === 'crosssell' || $linkType === 'related') {
Expand All @@ -142,6 +146,8 @@ private function loadLinkedTweakwiseItems(Closure $proceed, string $linkType, ..

/**
* @return Collection
* @throws ApiException
* @throws InvalidArgumentException
*/
protected function getCollection()
{
Expand All @@ -168,7 +174,8 @@ protected function getCollection()
return $this->collection->getItems();
}

private function getShoppingcartTweakwiseItems (ProductInterface $product, array $result, array $cartItems) {
private function getShoppingcartTweakwiseItems(ProductInterface $product, array $result, array $cartItems)
{
$items = [];

//show featured products
Expand All @@ -189,15 +196,11 @@ private function getShoppingcartTweakwiseItems (ProductInterface $product, array
$this->context->setRequest($request);

try {
$collection = Parent::getCollection();
$collection = parent::getCollection();
} catch (ApiException $e) {
return $result;
}

if (!empty($ninProductIds)) {
$collection = $this->removeCartItems($collection, $cartItems);
}

foreach ($collection as $item) {
$items[] = $item;
}
Expand All @@ -206,19 +209,21 @@ private function getShoppingcartTweakwiseItems (ProductInterface $product, array
}

/**
* @param $collection
* @param $filteredProducts
* @return void
* @param Collection $collection
* @param array $cartItems
*
* @return array
*/
protected function removeCartItems($collection, $cartItems)
protected function removeCartItems($collection, array $cartItems)
{
$items = $collection->getItems();

if(!empty($cartItems)) {
if (!empty($cartItems)) {
foreach ($cartItems as $cartItem) {
unset($items[$cartItem]);
}
}

return $items;
}

Expand All @@ -238,10 +243,6 @@ private function getFeaturedItems()
return [];
}

if (!empty($cartItems)) {
$collection = $this->removeCartItems($collection, $cartItems);
}

foreach ($collection as $item) {
$items[] = $item;
}
Expand Down
33 changes: 21 additions & 12 deletions src/ViewModel/SwatchRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Swatches\Helper\Data as SwatchHelper;

use function array_map as map;
use function array_keys as keys;

Expand Down Expand Up @@ -69,17 +70,24 @@ public function beforeListItemToHtml(AbstractBlock $itemRendererBlock, Product $

$filterAttributes = $this->getUsedSwatchFilters($product);
ksort($filterAttributes);
$swatchCacheKey = implode('', map(function (string $code) use ($filterAttributes): string {
if (is_array($filterAttributes[$code])) {
$codes = '';
foreach ($filterAttributes[$code] as $filterAttributesValues) {
$codes .= $filterAttributesValues;
}

return "$code={$codes}";
}
return "$code={$filterAttributes[$code]}";
}, keys($filterAttributes)));
$swatchCacheKey = implode(
'',
map(
function (string $code) use ($filterAttributes): string {
if (is_array($filterAttributes[$code])) {
$codes = '';
foreach ($filterAttributes[$code] as $filterAttributesValues) {
$codes .= $filterAttributesValues;
}

return "$code={$codes}";
}

return "$code={$filterAttributes[$code]}";
},
keys($filterAttributes)
)
);

if ($swatchCacheKey) {
$newKey = $this->blockCache->hashCacheKeyInfo([$itemRendererBlock->getData('cache_key'), $swatchCacheKey]);
Expand Down Expand Up @@ -119,7 +127,8 @@ private function canReplaceImageWithSwatch($attribute)
$result = false;
}

if (!$attribute->getUsedInProductListing()
if (
!$attribute->getUsedInProductListing()
|| !$attribute->getIsFilterable()
|| !$attribute->getData('update_product_preview_image')
) {
Expand Down
3 changes: 3 additions & 0 deletions src/view/frontend/layout/hyva_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<action method="setTemplate" ifconfig="tweakwise/autocomplete/enabled">
<argument name="template" xsi:type="string">Tweakwise_Magento2Tweakwise::html/header/search-form.phtml</argument>
</action>
<arguments>
<argument name="tweakwiseSearchForm" xsi:type="object">Tweakwise\TweakwiseHyva\Plugin\ViewModel\SearchForm</argument>
</arguments>
<block class="Tweakwise\Magento2Tweakwise\Block\Autocomplete\FormMini" ifconfig="tweakwise/autocomplete/enabled"
name="tweakwise.quick.search" template="Tweakwise_Magento2Tweakwise::quick-search.phtml"/>
</referenceBlock>
Expand Down
6 changes: 3 additions & 3 deletions src/view/frontend/templates/html/header/search-form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ declare(strict_types=1);
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magento\Search\Helper\Data as SearchHelper;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
use Tweakwise\TweakwiseHyva\Plugin\ViewModel\SearchForm;

/** @var Escaper $escaper */
/** @var Template $block */
/** @var SearchHelper $helper */
/** @var SearchForm $tweakwiseSearchForm */

$helper = $this->helper(SearchHelper::class);
$helper = $block->getData('tweakwiseSearchForm')->getSearchHelper();

?>
<script>
Expand Down
19 changes: 19 additions & 0 deletions src/view/frontend/templates/layer/navigation-slider.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ $noUiSliderUrl = $block->getViewFileUrl('Tweakwise_TweakwiseHyva::js/lib/nouisli
formFilters: false,
ajaxFilters: false,
urlInputSelector: 'input.slider-url-value',
containsBuckets: false,
containsClickpoints: false,
initSlider(initialValues) {
const sliderOptions = Object.values(initialValues)[0];

Expand All @@ -43,6 +45,8 @@ $noUiSliderUrl = $block->getViewFileUrl('Tweakwise_TweakwiseHyva::js/lib/nouisli
this.formFilters = sliderOptions.formFilters;
this.max = sliderOptions.max;
this.min = sliderOptions.min;
this.containsBuckets = sliderOptions.containsBuckets;
this.containsClickpoints = sliderOptions.containsClickpoints;
}

const script = document.createElement('script');
Expand Down Expand Up @@ -104,6 +108,12 @@ $noUiSliderUrl = $block->getViewFileUrl('Tweakwise_TweakwiseHyva::js/lib/nouisli
const sliderContainer = this.$root;
sliderContainer.querySelector('input.slider-min').addEventListener('change', () => this.updateSliderUrlInput());
sliderContainer.querySelector('input.slider-max').addEventListener('change', () => this.updateSliderUrlInput());
if (this.containsBuckets) {
const bucketLinks = sliderContainer.querySelectorAll('div.buckets a.bucket-link');
bucketLinks.forEach(bucketLink => {
bucketLink.addEventListener('click', () => this.clickBucketSlider());
});
}
},
updateSliderUrlInput() {
const sliderContainer = this.$root;
Expand Down Expand Up @@ -157,6 +167,15 @@ $noUiSliderUrl = $block->getViewFileUrl('Tweakwise_TweakwiseHyva::js/lib/nouisli
const sliderContainer = this.$root;
sliderContainer.dataset.min = minValue;
sliderContainer.dataset.max = maxValue;
},
clickBucketSlider() {
const sliderContainer = this.$root;
const sliderUrlInput = sliderContainer.querySelector(this.urlInputSelector);
const bucket = event.target.parentElement;
const minValue = bucket.getAttribute('data-rangemin');
const maxValue = bucket.getAttribute('data-rangemax');
this.updateLabels(minValue, maxValue);
this.filterChange(minValue, maxValue);
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/view/frontend/templates/layer/state.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use Tweakwise\Magento2Tweakwise\Block\LayeredNavigation\Navigation\State;
?>
<?php /** @var Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Filter\Item[] $filters */ ?>
<?php $filters = $block->getActiveFilters() ?>
<?php if (!empty($filters)) : ?>
<?php if (!empty($filters)): ?>
<div class="filter-current card my-4 pb-0"
x-data="{ open: true }">
<div class="filter-options-title flex justify-between items-center cursor-pointer hover:text-secondary-darker border-container pb-2"
Expand All @@ -33,7 +33,7 @@ use Tweakwise\Magento2Tweakwise\Block\LayeredNavigation\Navigation\State;
</span>
</div>
<div class="items pt-1 py-3" x-show="open">
<?php foreach ($filters as $filter) : ?>
<?php foreach ($filters as $filter): ?>
<div class="item flex justify-between items-center mb-1">
<span>
<span class="filter-label block"><?= $escaper->escapeHtml(__($filter->getName())) ?></span>
Expand All @@ -42,9 +42,8 @@ use Tweakwise\Magento2Tweakwise\Block\LayeredNavigation\Navigation\State;
<?php $currentFilterName = $escaper->escapeHtmlAttr(__($filter->getName()) . " " . $block->stripTags($filter->getLabel())); ?>
<a class="action remove text-center block py-1.5 px-1.5 rounded border border-container text-primary hover:text-primary-darker"
href="<?= $escaper->escapeUrl($filter->getRemoveUrl()) ?>"
data-js-filter-id="<?= $block->getActiveFilterCssId($filter) ?>"
title="<?= /* @noEscape */
$escaper->escapeHtml(__('Remove')) . " " . $currentFilterName ?>">
data-js-filter-id="<?= $escaper->escapeHtmlAttr($block->getActiveFilterCssId($filter)) ?>"
title="<?= $escaper->escapeHtmlAttr(__('Remove') . " " . $currentFilterName) ?>">
<svg class="fill-current" width="20" height="20" viewBox="0 0 20 20" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd"
Expand Down
Loading

0 comments on commit 3c74d84

Please sign in to comment.