Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit e7e3e5b
Merge: d46606e 8c06a71
Author: dz-optimizers <[email protected]>
Date:   Wed Sep 6 09:47:50 2023 +0200

    Merge pull request #94 from EmicoEcommerce/bump-version-5.6.5

    bump version to 5.6.5

commit 8c06a71
Author: Dennis <[email protected]>
Date:   Wed Sep 6 09:46:29 2023 +0200

    bump version to 5.6.5

commit d46606e
Merge: 643f2f0 cd079f8
Author: dz-optimizers <[email protected]>
Date:   Wed Sep 6 09:40:25 2023 +0200

    Merge pull request #93 from EmicoEcommerce/bugfix-guzzle-error-autocomplete

    Bugfix guzzle library

commit 643f2f0
Author: ah-net <[email protected]>
Date:   Wed Sep 6 09:32:21 2023 +0200

    Fix store code in url (#90)

commit cd079f8
Author: ah-net <[email protected]>
Date:   Tue Sep 5 15:57:55 2023 +0200

    Update composer.json

commit 5610725
Author: ah-net <[email protected]>
Date:   Tue Sep 5 11:07:23 2023 +0200

    Bugfix guzzle library

commit e737cf5
Author: ah-net <[email protected]>
Date:   Thu Aug 31 09:35:45 2023 +0200

    Bump version nr

commit 5d8df5e
Author: ah-net <[email protected]>
Date:   Fri Aug 25 09:12:17 2023 +0200

    Revert "Fixed fallback (#70)" (#88)

    This reverts commit 5347a53.

commit 1e321ef
Author: ah-net <[email protected]>
Date:   Wed Aug 23 16:41:55 2023 +0200

    Bump version nr

commit 55388e7
Author: ah-net <[email protected]>
Date:   Wed Aug 23 16:39:56 2023 +0200

    Bugfix check hash value on page load (#86)

commit 67adf95
Author: ah-net <[email protected]>
Date:   Wed Aug 23 16:05:57 2023 +0200

    Bugfix query parameter multiple filters (#87)

    * Update QueryParameterStrategy.php

    * Update QueryParameterStrategy.php

    * Update QueryParameterStrategy.php

commit e1686d4
Author: ah-net <[email protected]>
Date:   Wed Aug 23 10:29:26 2023 +0200

    Bump version nr (#85)

commit 5347a53
Author: ah-net <[email protected]>
Date:   Wed Aug 23 09:43:38 2023 +0200

    Fixed fallback (#70)

    * Fixed fallback

commit 18049fd
Author: ah-net <[email protected]>
Date:   Tue Aug 22 15:42:21 2023 +0200

    Fix bug multiple filters (#84)

    Fix bug in query parameter strategy

commit c21add3
Author: ah-net <[email protected]>
Date:   Tue Jul 18 09:35:27 2023 +0200

    Bump version

commit b21f103
Author: Sinan Kaplan <[email protected]>
Date:   Tue Jul 11 12:45:42 2023 +0300

    return last adde item back in around plugin (#75)

    Co-authored-by: Sinan Kaplan <[email protected]>

commit 333270c
Author: ah-net <[email protected]>
Date:   Wed Jul 5 09:30:15 2023 +0200

    Bump version (#73)

commit bf75cc0
Author: ah-net <[email protected]>
Date:   Wed Jul 5 09:21:25 2023 +0200

    Add fallback for categories (#71)

commit 0c68de0
Author: ah-net <[email protected]>
Date:   Fri Jun 30 09:15:34 2023 +0200

    Fix pagination/sorting url (#72)
  • Loading branch information
ah-net committed Sep 6, 2023
1 parent ec1d12d commit 11d2145
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 34 deletions.
3 changes: 3 additions & 0 deletions Block/Catalog/Product/ProductList/Featured.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ protected function configureRequest(FeaturedRequest $request)
$category = $this->_coreRegistry->registry('current_category');
if ($category instanceof Category) {
$templateId = $this->templateFinder->forCategory($category, Config::RECOMMENDATION_TYPE_FEATURED);
if (!$templateId) {
$templateId = $this->config->getRecommendationsTemplate(Config::RECOMMENDATION_TYPE_FEATURED);
}
} else {
$templateId = $this->config->getRecommendationsTemplate(Config::RECOMMENDATION_TYPE_FEATURED);
}
Expand Down
2 changes: 2 additions & 0 deletions Block/Checkout/Cart/Crosssell/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function aroundGetLastAddedProduct(Crosssell $crossell, Closure $proceed)
}
}
$this->lastAddedProduct = $product;

return $this->lastAddedProduct;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Model/Autocomplete/DataProvider/SuggestionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Tweakwise\Magento2Tweakwise\Model\Client\Response\AutocompleteProductResponseInterface;
use Tweakwise\Magento2Tweakwise\Model\Client\Response\Suggestions\SuggestionsResponse;
use Tweakwise\Magento2Tweakwise\Model\Config;
use function GuzzleHttp\Promise\unwrap;
use GuzzleHttp\Promise\Utils;
use Magento\Framework\Exception\LocalizedException;
use Magento\Search\Model\Autocomplete\ItemInterface;

Expand Down Expand Up @@ -146,7 +146,7 @@ public function getItems()
}

$results = [];
$responses = unwrap($promises);
$responses = Utils::unwrap($promises);
foreach ($responses as $key => $response) {
if ($response instanceof AutocompleteProductResponseInterface) {
$results[] = $this->dataProviderHelper->getProductItems($response);
Expand Down
33 changes: 25 additions & 8 deletions Model/Catalog/Layer/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\FilterApplierInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\Strategy\UrlStrategyFactory;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\UrlInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\UrlModel;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\FacetType\SettingsType;
use Tweakwise\Magento2TweakwiseExport\Model\Helper as ExportHelper;
Expand Down Expand Up @@ -63,6 +64,8 @@ class Url
*/
protected $urlStrategyFactory;

protected $magentoUrl;

/**
* Builder constructor.
*
Expand All @@ -75,12 +78,14 @@ public function __construct(
UrlStrategyFactory $urlStrategyFactory,
MagentoHttpRequest $request,
CategoryRepositoryInterface $categoryRepository,
ExportHelper $exportHelper
ExportHelper $exportHelper,
UrlModel $magentoUrl
) {
$this->urlStrategyFactory = $urlStrategyFactory;
$this->categoryRepository = $categoryRepository;
$this->exportHelper = $exportHelper;
$this->request = $request;
$this->magentoUrl = $magentoUrl;
}

/**
Expand Down Expand Up @@ -137,7 +142,7 @@ public function getSelectFilter(Item $item): string
->getCategoryFilterSelectUrl($this->request, $item);
}

return $this->getUrlStrategy()->getAttributeSelectUrl($this->request, $item);
return $this->addBaseUrl($this->getUrlStrategy()->getAttributeSelectUrl($this->request, $item));
}

/**
Expand All @@ -154,7 +159,7 @@ public function getRemoveFilter(Item $item): string
->getCategoryFilterRemoveUrl($this->request, $item);
}

return $this->getUrlStrategy()->getAttributeRemoveUrl($this->request, $item);
return $this->addBaseUrl($this->getUrlStrategy()->getAttributeRemoveUrl($this->request, $item));
}

/**
Expand All @@ -163,8 +168,8 @@ public function getRemoveFilter(Item $item): string
*/
public function getClearUrl(array $activeFilterItems)
{
return $this->getUrlStrategy()
->getClearUrl($this->request, $activeFilterItems);
return $this->addBaseUrl($this->getUrlStrategy()
->getClearUrl($this->request, $activeFilterItems));
}

/**
Expand All @@ -173,8 +178,8 @@ public function getClearUrl(array $activeFilterItems)
*/
public function getFilterUrl(array $activeFilterItems)
{
return $this->getUrlStrategy()
->buildFilterUrl($this->request, $activeFilterItems);
return $this->addBaseUrl($this->getUrlStrategy()
->buildFilterUrl($this->request, $activeFilterItems));
}

/**
Expand All @@ -191,7 +196,19 @@ public function apply(ProductNavigationRequest $navigationRequest)
*/
public function getSliderUrl(Item $item)
{
return $this->getUrlStrategy()->getSliderUrl($this->request, $item);
return $this->addBaseUrl($this->getUrlStrategy()->getSliderUrl($this->request, $item));
}

public function addBaseUrl($url) {

$baseUrl = $this->magentoUrl->getBaseUrl();
//prevent double base urls
$url = str_replace($baseUrl, '', $url);
//remove slashes to prevent double slashes
$baseUrl = rtrim($baseUrl, '/');
$url = ltrim($url, '/');

return $baseUrl . '/' . $url;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions Model/Catalog/Layer/Url/Strategy/PathSlugStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,6 @@ public function getCategoryFilterSelectUrl(
These two are the only unique parts in this situation and so need to be removed.
*/

$explode = explode('/', $url);

if (is_array($explode)) {
$url = implode('/', array_unique($explode));
}


/*
Make sure we dont have any double slashes, add the current filter path to the category url to maintain
Expand All @@ -569,6 +563,10 @@ public function getCategoryFilterSelectUrl(

$lastpart = null;

if (is_array($explode)) {
$url = implode('/', array_unique($explode));
}

if (is_array($explode)) {
$lastpart = end($explode);
}
Expand Down
59 changes: 54 additions & 5 deletions Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ public function getClearUrl(MagentoHttpRequest $request, array $activeFilterItem
*/
protected function getCurrentQueryUrl(MagentoHttpRequest $request, array $query)
{
$selectedFilters = $request->getQuery();
$reservedParams = [
self::PARAM_LIMIT,
self::PARAM_MODE,
self::PARAM_PAGE,
self::PARAM_ORDER,
];

foreach ($selectedFilters as $filter => $value) {
if (!array_key_exists($filter, $query) && (!in_array($filter, $reservedParams))) {
$query[$filter] = $value;
}
}

$params['_query'] = $query;
$params['_escape'] = false;

Expand Down Expand Up @@ -206,12 +220,28 @@ public function getCategoryFilterSelectUrl(MagentoHttpRequest $request, Item $it
$categoryUrlPath = \parse_url($categoryUrl, PHP_URL_PATH);

$url = $this->url->getDirectUrl(
trim($categoryUrlPath, '/'),
[
'_query' => $this->getAttributeFilters($request)
]
sprintf(
'%s/',
trim($categoryUrlPath, '/'),
[
'_query' => $this->getAttributeFilters($request)
]
)
);


/*
We explode the url so that we can capture its parts and find the double values in order to remove them.
This is needed because the categoryUrlPath contains the store code in some cases and the directUrl as well.
These two are the only unique parts in this situation and so need to be removed.
*/

$explode = explode('/', $url);

if (is_array($explode)) {
$url = implode('/', array_unique($explode));
}

$url = str_replace($this->url->getBaseUrl(), '', $url);

return $url;
Expand Down Expand Up @@ -493,7 +523,24 @@ public function getOriginalUrl(MagentoHttpRequest $request) : string
$newOriginalUrl = mb_substr($newOriginalUrl, 1);
}

$newOriginalUrl = $this->url->getDirectUrl($newOriginalUrl);
// This seems ugly, perhaps there is another way?
$query = [];
// Add page and sort
$sort = $request->getParam('product_list_order');
$limit = $request->getParam('product_list_limit');
$mode = $request->getParam('product_list_mode');

if ($sort) {
$query['product_list_order'] = $sort;
}
if ($limit) {
$query['product_list_limit'] = $limit;
}
if ($mode) {
$query['product_list_mode'] = $mode;
}

$newOriginalUrl = $this->url->getDirectUrl($newOriginalUrl, ['_query' => $query]);

return str_replace($this->url->getBaseUrl(), '', $newOriginalUrl);
}
Expand All @@ -512,6 +559,8 @@ private function getCurrentUrl(MagentoHttpRequest $request) : string
return $this->url->getUrl('*/*/*', $params);
}

$url = ltrim($url, '/');

return str_replace($this->url->getBaseUrl(), '', $url);
}
}
5 changes: 5 additions & 0 deletions Model/Client/Type/SuggestionType/SuggestionTypeCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class SuggestionTypeCategory extends SuggestionTypeAbstract
*/
protected $urlInstance;

/**
* @var Config
*/
protected $config;

/**
* SuggestionTypeCategory constructor.
* @param CategoryRepository $categoryRepository Empty category model used to resolve urls
Expand Down
55 changes: 46 additions & 9 deletions Model/Config/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Tweakwise\Magento2Tweakwise\Model\Config;

use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2Tweakwise\Model\Config\Source\RecommendationOption;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\CategoryRepository;

class TemplateFinder
{
Expand All @@ -14,13 +16,25 @@ class TemplateFinder
*/
protected $config;

/**
* @var Registry
*/
protected $registry;

/**
* @var CategoryRepository
*/
protected $categoryRepository;

/**
* TemplateFinder constructor.
* @param Config $config
*/
public function __construct(Config $config)
public function __construct(Config $config, Registry $registry, CategoryRepository $categoryRepository)
{
$this->config = $config;
$this->registry = $registry;
$this->categoryRepository = $categoryRepository;
}

/**
Expand All @@ -33,6 +47,7 @@ public function forProduct(Product $product, $type)
$attribute = $this->getAttribute($type);
$templateId = (int) $product->getData($attribute);

//first try product template
if ($templateId === RecommendationOption::OPTION_CODE) {
$groupAttribute = $this->getGroupCodeAttribute($type);
return (string) $product->getData($groupAttribute);
Expand All @@ -42,9 +57,37 @@ public function forProduct(Product $product, $type)
return $templateId;
}

//try template from the most recent category
$category = $this->registry->registry('current_category');

if ($category) {
$templateId = $this->forCategory($category, $type);

if ($templateId) {
return $templateId;
}
}

//try default product category
$category = $product->getCategory();
if ($category) {
return $this->forCategory($category, $type);
$templateId = $this->forCategory($category, $type);
}

if ($templateId) {
return $templateId;
}

//try template for other categories of the product
$categoryIds = $product->getCategoryIds();

foreach($categoryIds as $categoryId) {
$category = $this->categoryRepository->get($categoryId);
$templateId = $this->forCategory($category, $type);

if ($templateId) {
return $templateId;
}
}

$defaultTemplateId = $this->config->getRecommendationsTemplate($type);
Expand Down Expand Up @@ -80,13 +123,7 @@ public function forCategory(Category $category, $type)
return $this->forCategory($parent, $type);
}

$defaultTemplateId = $this->config->getRecommendationsTemplate($type);

if ($defaultTemplateId === RecommendationOption::OPTION_CODE) {
return $this->config->getRecommendationsGroupCode($type);
}

return $defaultTemplateId;
return null;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Model/FilterFormInputProvider/CategoryInputProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ public function getFilterFormInput(): array
return [];
}

$url = $this->getOriginalUrl();

$url = strtok($url, '?');

$input = [
'__tw_ajax_type' => self::TYPE,
'__tw_object_id' => $this->getCategoryId(),
'__tw_original_url' => $this->getOriginalUrl(),
'__tw_original_url' => $url,
];

$input['__tw_hash'] = $this->hashInputProvider->getHash($input);
Expand Down
5 changes: 5 additions & 0 deletions Model/FilterFormInputProvider/HashInputProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function validateHash($request)
if ($hash === $originalHash) {
$isValid = true;
}
} else {
//hash is empty original url should also be empty
if (empty ($request->getParam('__tw_original_url'))) {
$isValid = true;
}
}

return $isValid;
Expand Down
Loading

0 comments on commit 11d2145

Please sign in to comment.