Skip to content

Commit

Permalink
Merge pull request #67 from magmodules/release/2.1.0
Browse files Browse the repository at this point in the history
Release/2.1.0
  • Loading branch information
Marvin-Magmodules committed Apr 25, 2024
2 parents 821efd4 + b49dcc6 commit 543bf9f
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 48 deletions.
6 changes: 6 additions & 0 deletions Api/Config/System/SearchInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface SearchInterface
public const XML_PATH_ADD_TO_CART = 'sooqr_search/frontend/add_to_cart';
public const XML_PATH_ADD_TO_CART_AJAX = 'sooqr_search/frontend/add_to_cart_ajax';
public const XML_PATH_ADD_TO_WISHLIST = 'sooqr_search/frontend/add_to_wishlist';
public const XML_PATH_ADD_TO_COMPARE = 'sooqr_search/frontend/add_to_compare';

/**
* @return bool
Expand Down Expand Up @@ -60,6 +61,11 @@ public function isAjaxAddToCartEnabled(): bool;
*/
public function addToWishlistController(): bool;

/**
* @return bool
*/
public function addToCompareController(): bool;

/**
* @return string
*/
Expand Down
132 changes: 132 additions & 0 deletions Controller/Compare/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Sooqr\Controller\Compare;

use Exception;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Controller\Product\Compare;
use Magento\Catalog\Helper\Product\Compare as CompareHelper;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Compare\ItemFactory;
use Magento\Catalog\Model\Product\Compare\ListCompare;
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory;
use Magento\Catalog\Model\Session as CatalogSession;
use Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Customer\Model\Visitor;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Result\PageFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magmodules\Sooqr\Api\Config\RepositoryInterface as ConfigProvider;

/**
* Add item to compare list action.
*/
class Add extends Compare implements HttpGetActionInterface
{
/**
* @var AddToCompareAvailability
*/
private $compareAvailability;
/**
* @var ConfigProvider
*/
private $configProvider;
/**
* @var Escaper
*/
private $escaper;
/**
* @var CompareHelper
*/
private $compareHelper;

public function __construct(
Context $context,
ItemFactory $compareItemFactory,
CollectionFactory $itemCollectionFactory,
CustomerSession $customerSession,
Visitor $customerVisitor,
ListCompare $catalogProductCompareList,
CatalogSession $catalogSession,
StoreManagerInterface $storeManager,
Validator $formKeyValidator,
PageFactory $resultPageFactory,
ProductRepositoryInterface $productRepository,
Escaper $escaper,
ConfigProvider $configProvider,
CompareHelper $compareHelper,
AddToCompareAvailability $compareAvailability = null
) {
parent::__construct(
$context,
$compareItemFactory,
$itemCollectionFactory,
$customerSession,
$customerVisitor,
$catalogProductCompareList,
$catalogSession,
$storeManager,
$formKeyValidator,
$resultPageFactory,
$productRepository
);

$this->configProvider = $configProvider;
$this->escaper = $escaper;
$this->compareHelper = $compareHelper;
$this->compareAvailability = $compareAvailability
?: $this->_objectManager->get(AddToCompareAvailability::class);
}

/**
* @throws NoSuchEntityException
* @throws Exception
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$productId = (int)$this->getRequest()->getParam('product');

if (!$this->configProvider->addToCompareController()) {
$this->messageManager->addErrorMessage(__('Controller disabled in admin'));
return $resultRedirect->setRefererOrBaseUrl();
}

if ($productId && ($this->_customerVisitor->getId() || $this->_customerSession->isLoggedIn())) {
$storeId = $this->_storeManager->getStore()->getId();
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId, false, $storeId);
} catch (NoSuchEntityException $e) {
$product = null;
}

if ($product && $this->compareAvailability->isAvailableForCompare($product)) {
$this->_catalogProductCompareList->addProduct($product);
$this->messageManager->addComplexSuccessMessage(
'addCompareSuccessMessage',
[
'product_name' => $this->escaper->escapeHtml($product->getName()),
'compare_list_url' => $this->_url->getUrl('catalog/product_compare'),
]
);

$this->_eventManager->dispatch('catalog_product_compare_add_product', ['product' => $product]);
}

$this->compareHelper->calculate();
}

return $resultRedirect->setRefererOrBaseUrl();
}
}
8 changes: 8 additions & 0 deletions Model/Config/System/SearchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function addToWishlistController(): bool
return $this->isSearchEnabled() && $this->isSetFlag(self::XML_PATH_ADD_TO_WISHLIST);
}

/**
* @inheritDoc
*/
public function addToCompareController(): bool
{
return $this->isSearchEnabled() && $this->isSetFlag(self::XML_PATH_ADD_TO_COMPARE);
}

/**
* @inheritDoc
*/
Expand Down
131 changes: 88 additions & 43 deletions Model/ProductData/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Serialize\Serializer\Json;
use Magmodules\Sooqr\Api\Config\RepositoryInterface as DataConfigRepository;
use Magmodules\Sooqr\Api\ProductData\RepositoryInterface as ProductData;
use Magmodules\Sooqr\Model\Config\Source\FeedType;
Expand Down Expand Up @@ -108,6 +109,10 @@ class Repository implements ProductData
* @var FilterManager
*/
private $filterManager;
/**
* @var Json
*/
private $json;

/**
* Repository constructor.
Expand All @@ -119,12 +124,14 @@ class Repository implements ProductData
*/
public function __construct(
DataConfigRepository $dataConfigRepository,
Json $json,
FilterManager $filterManager,
Filter $filter,
Type $type,
Image $image
) {
$this->dataConfigRepository = $dataConfigRepository;
$this->json = $json;
$this->filterManager = $filterManager;
$this->filter = $filter;
$this->type = $type;
Expand All @@ -144,7 +151,7 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $

$result = [];
foreach ($this->collectProductData($storeId, $type) as $entityId => $productData) {
if (empty($productData['product_id'])) {
if (empty($productData['product_id']) || $productData['status'] == 2) {
continue;
}
$this->addImageData($storeId, (int)$entityId, $productData);
Expand All @@ -162,47 +169,6 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
return $this->postProcess($result, $storeId);
}

/**
* @param array $result
* @param int $storeId
* @return array
*/
private function postProcess(array $result, int $storeId = 0): array
{
if (!$this->dataConfigRepository->getFilters($storeId)['exclude_out_of_stock'] || empty($result)) {
return $result;
}

$unsetSimples = [];
foreach ($result as $id => &$row) {

// Remove parent products without simples
if ($row['sqr:id'] == $row['sqr:assoc_id'] && $row['sqr:price'] == 0.00) {
$unsetSimples[] = $id;
continue;
}

// Remove out of stock products
if ($row['sqr:availability'] != 'out of stock') {
continue;
}
$unsetSimples[] = $id;

if (!empty($row['sqr:assoc_id']) && isset($this->parentSimples[$row['sqr:assoc_id']])) {
$this->parentSimples[$row['sqr:assoc_id']] = array_diff(
$this->parentSimples[$row['sqr:assoc_id']],
[$id]
);
}
}

$emptyParents = array_keys(array_filter($this->parentSimples), function ($value) {
return empty($value);
});

return array_diff_key($result, array_flip($emptyParents) + array_flip($unsetSimples));
}

/**
* Collect all entity ids for collection
*
Expand Down Expand Up @@ -404,7 +370,7 @@ private function prepareAttribute(string $attribute, array $productData)
case 'url':
return $productData['url'] ?? '';
case 'description':
return $this->filterManager->removeTags((string)$value);
return $this->reformatDescription((string)$value);
case 'price':
case 'price_ex':
case 'final_price':
Expand Down Expand Up @@ -439,6 +405,44 @@ private function prepareAttribute(string $attribute, array $productData)
}
}

/**
* @param string $value
* @return string
*/
private function reformatDescription(string $value): string
{
if (strpos($value, "[mgz_pagebuilder]") === 0) {
try {
$pattern = '/\[mgz_pagebuilder\](.*?)\[\/mgz_pagebuilder\]/s';
if (preg_match($pattern, $value, $matches)) {
$content = $this->json->unserialize($matches[1]);
$found = $this->findAllContentInArray($content, 'content');
$value = implode(' ', $found);
}
} catch (\Exception $exception) {
return $this->filterManager->removeTags($value);
}
}

return $this->filterManager->removeTags($value);
}

/**
* @param array $array
* @param string|null $key
* @return array
*/
private function findAllContentInArray(array $array, ?string $key = null): array
{
array_walk_recursive($array, function ($v, $k) use ($key, &$val) {
if ($key === null || ($key && $k == $key)) {
$val[] = $v;
}
});

return array_unique($val ?? []);
}

/**
* Add category data to productData array
*
Expand All @@ -464,6 +468,47 @@ private function categoryData(array $productData): array
return $categoryData;
}

/**
* @param array $result
* @param int $storeId
* @return array
*/
private function postProcess(array $result, int $storeId = 0): array
{
if (!$this->dataConfigRepository->getFilters($storeId)['exclude_out_of_stock'] || empty($result)) {
return $result;
}

$unsetSimples = [];
foreach ($result as $id => &$row) {

// Remove parent products without simples
if ($row['sqr:id'] == $row['sqr:assoc_id'] && $row['sqr:price'] == 0.00) {
$unsetSimples[] = $id;
continue;
}

// Remove out of stock products
if ($row['sqr:availability'] != 'out of stock') {
continue;
}
$unsetSimples[] = $id;

if (!empty($row['sqr:assoc_id']) && isset($this->parentSimples[$row['sqr:assoc_id']])) {
$this->parentSimples[$row['sqr:assoc_id']] = array_diff(
$this->parentSimples[$row['sqr:assoc_id']],
[$id]
);
}
}

$emptyParents = array_keys(array_filter($this->parentSimples), function ($value) {
return empty($value);
});

return array_diff_key($result, array_flip($emptyParents) + array_flip($unsetSimples));
}

/**
* @inheritDoc
*/
Expand Down
5 changes: 3 additions & 2 deletions Service/ProductData/AttributeCollector/Data/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ private function collectUrl(): array
'url_rewrite.redirect_type = ?',
0
)->where(
'url_rewrite.metadata IS NULL'
'url_rewrite.metadata IS NULL OR url_rewrite.metadata = ?',
'[]'
)->where(
'url_rewrite.store_id = ?',
$this->storeId
Expand All @@ -167,7 +168,7 @@ private function collectUrl(): array
}
foreach ($this->entityIds as $entityId) {
if (!array_key_exists($entityId, $result)) {
$result[$this->linkField] = sprintf(
$result[$entityId] = sprintf(
self::URL_PATTERN_EXTRA[$this->type],
$storeUrl,
$entityId
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magmodules/magento2-sooqr",
"description": "Sooqr integration for Magento 2",
"type": "magento2-module",
"version": "2.0.7",
"version": "2.1.0",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down

0 comments on commit 543bf9f

Please sign in to comment.