ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Amasty Improved Sorting GraphQl for Magento 2 (System)
|
||||
*/
|
||||
|
||||
namespace Amasty\SortingGraphQl\Model\Resolver;
|
||||
|
||||
class ArgumentResolver
|
||||
{
|
||||
/**
|
||||
* @param array $args
|
||||
* @return array
|
||||
*/
|
||||
public function convertArgs(array $args): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($args as $key => $arg) {
|
||||
$result[$this->convertFromCamelCase($key)] = $arg;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function convertFromCamelCase(string $name): string
|
||||
{
|
||||
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Amasty Improved Sorting GraphQl for Magento 2 (System)
|
||||
*/
|
||||
|
||||
namespace Amasty\SortingGraphQl\Model\Resolver;
|
||||
|
||||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
|
||||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
||||
use Magento\Framework\GraphQl\Config\Element\Field;
|
||||
use Magento\Framework\GraphQl\Query\ResolverInterface;
|
||||
|
||||
class FeaturedList implements ResolverInterface
|
||||
{
|
||||
|
||||
public const AM_FEATURED_WIDGET = 'am_featured_widget';
|
||||
/**
|
||||
* @var \Amasty\Sorting\Block\Widget\Featured
|
||||
*/
|
||||
private $featured;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Product\Compare
|
||||
*/
|
||||
private $compareHelper;
|
||||
|
||||
/**
|
||||
* @var ArgumentResolver
|
||||
*/
|
||||
private $argumentResolver;
|
||||
|
||||
public function __construct(
|
||||
\Amasty\Sorting\Block\Widget\Featured $featured,
|
||||
\Magento\Catalog\Helper\Product\Compare $compareHelper,
|
||||
ArgumentResolver $argumentResolver
|
||||
) {
|
||||
$this->featured = $featured;
|
||||
$this->compareHelper = $compareHelper;
|
||||
$this->argumentResolver = $argumentResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Field $field
|
||||
* @param ContextInterface $context
|
||||
* @param ResolveInfo $info
|
||||
* @param array|null $value
|
||||
* @param array|null $args
|
||||
* @return array|\Magento\Framework\GraphQl\Query\Resolver\Value|mixed
|
||||
*/
|
||||
public function resolve(
|
||||
Field $field,
|
||||
$context,
|
||||
ResolveInfo $info,
|
||||
array $value = null,
|
||||
array $args = null
|
||||
) {
|
||||
$storeId = (int) $context->getExtensionAttributes()->getStore()->getId();
|
||||
|
||||
try {
|
||||
$data = $this->argumentResolver->convertArgs($args);
|
||||
$this->setFeaturedData($data, $storeId);
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$productCollection = $this->featured->createCollection()->setFlag(self::AM_FEATURED_WIDGET, true);
|
||||
$productCollection->setCurPage($data['current_page'] ?? 1);
|
||||
foreach ($productCollection as $product) {
|
||||
$data[] = $this->prepareData($product, $context);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param int $storeId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function setFeaturedData(array $data, int $storeId): void
|
||||
{
|
||||
$this->featured->setNameInLayout('amsorting_featured_list');
|
||||
$this->featured->setData($data);
|
||||
$this->featured->setData('store_id', $storeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $product
|
||||
* @param ContextInterface $context
|
||||
* @return array
|
||||
*/
|
||||
private function prepareData($product, ContextInterface $context)
|
||||
{
|
||||
$data['id'] = $product->getEntityId();
|
||||
$data['productUrl'] = $this->getRelativePath($this->featured->getProductUrl($product), $context);
|
||||
$data['name'] = $product->getName();
|
||||
$data['isSalable'] = $product->isSaleable() || $product->getIsSalable();
|
||||
$data['hasRequiredOptions'] = $product->getTypeInstance()->hasRequiredOptions($product);
|
||||
$data['addToCartUrl'] = $this->featured->getAddToCartUrl($product);
|
||||
$data['addToCompareParams'] = $this->compareHelper->getPostDataParams($product);
|
||||
$data['sku'] = $product->getSku();
|
||||
$data['model'] = $product;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getRelativePath(string $url, ContextInterface $context): string
|
||||
{
|
||||
$baseUrl = trim($context->getExtensionAttributes()->getStore()->getBaseUrl(), '/');
|
||||
|
||||
return str_replace($baseUrl, '', $url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Amasty Improved Sorting GraphQl for Magento 2 (System)
|
||||
*/
|
||||
|
||||
namespace Amasty\SortingGraphQl\Model\Resolver;
|
||||
|
||||
use Amasty\Sorting\Model\Catalog\Toolbar\GetDefaultDirection;
|
||||
use Magento\Catalog\Model\Config as CatalogConfig;
|
||||
use Magento\Framework\GraphQl\Config\Element\Field;
|
||||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
|
||||
use Magento\Framework\GraphQl\Query\ResolverInterface;
|
||||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
||||
|
||||
class GetAvailableOrders implements ResolverInterface
|
||||
{
|
||||
/**
|
||||
* @var CatalogConfig
|
||||
*/
|
||||
private $catalogConfig;
|
||||
|
||||
/**
|
||||
* @var GetDefaultDirection
|
||||
*/
|
||||
private $getDefaultDirection;
|
||||
|
||||
public function __construct(CatalogConfig $catalogConfig, GetDefaultDirection $getDefaultDirection)
|
||||
{
|
||||
$this->catalogConfig = $catalogConfig;
|
||||
$this->getDefaultDirection = $getDefaultDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Field $field
|
||||
* @param ContextInterface $context
|
||||
* @param ResolveInfo $info
|
||||
* @param array|null $value
|
||||
* @param array|null $args
|
||||
* @return array
|
||||
*/
|
||||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->catalogConfig->getAttributeUsedForSortByArray() as $code => $label) {
|
||||
$result[] = [
|
||||
'attribute' => $code,
|
||||
'id' => $code,
|
||||
'text' => $label,
|
||||
'sortDirection' => strtoupper($this->getDefaultDirection->execute($code))
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Amasty Improved Sorting GraphQl for Magento 2 (System)
|
||||
*/
|
||||
|
||||
namespace Amasty\SortingGraphQl\Model\Resolver;
|
||||
|
||||
use Amasty\Sorting\Model\Catalog\Toolbar\GetDefaultDirection;
|
||||
use Amasty\Sorting\Model\ConfigProvider;
|
||||
use Amasty\Sorting\Model\Source\ListSort;
|
||||
use Amasty\Sorting\Model\Source\SearchSort;
|
||||
use Magento\Framework\GraphQl\Config\Element\Field;
|
||||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
|
||||
use Magento\Framework\GraphQl\Query\ResolverInterface;
|
||||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
||||
|
||||
class GetDefaultOrders implements ResolverInterface
|
||||
{
|
||||
public const CATEGORY_PAGE_TYPE = 'CATEGORY';
|
||||
|
||||
public const PAGE_TYPE_PARAM = 'pageType';
|
||||
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var GetDefaultDirection
|
||||
*/
|
||||
private $getDefaultDirection;
|
||||
|
||||
/**
|
||||
* @var ListSort
|
||||
*/
|
||||
private $listSort;
|
||||
|
||||
/**
|
||||
* @var SearchSort
|
||||
*/
|
||||
private $searchListSort;
|
||||
|
||||
public function __construct(
|
||||
ConfigProvider $config,
|
||||
GetDefaultDirection $getDefaultDirection,
|
||||
ListSort $listSort,
|
||||
SearchSort $searchListSort
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->getDefaultDirection = $getDefaultDirection;
|
||||
$this->listSort = $listSort;
|
||||
$this->searchListSort = $searchListSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Field $field
|
||||
* @param ContextInterface $context
|
||||
* @param ResolveInfo $info
|
||||
* @param array|null $value
|
||||
* @param array|null $args
|
||||
* @return array
|
||||
*/
|
||||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
||||
{
|
||||
$result = [];
|
||||
if ($args[self::PAGE_TYPE_PARAM] === self::CATEGORY_PAGE_TYPE) {
|
||||
$sortOrders = $this->config->getDefaultSortingCategoryPages();
|
||||
$labels = $this->getSortLabels($sortOrders, $this->listSort->getAllOptions());
|
||||
} else {
|
||||
$sortOrders = $this->config->getDefaultSortingSearchPages();
|
||||
$labels = $this->getSortLabels($sortOrders, $this->searchListSort->toOptionArray());
|
||||
}
|
||||
|
||||
foreach ($sortOrders as $sortOrderCode) {
|
||||
if ($sortOrderCode) {
|
||||
$result[] = [
|
||||
'attribute' => $sortOrderCode,
|
||||
'id' => $sortOrderCode,
|
||||
'text' => $labels[$sortOrderCode],
|
||||
'sortDirection' => strtoupper($this->getDefaultDirection->execute($sortOrderCode))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getSortLabels(array $sortValues, array $sortOptions): array
|
||||
{
|
||||
$labels = [];
|
||||
foreach ($sortOptions as $sortOption) {
|
||||
if (in_array($sortOption['value'], $sortValues)) {
|
||||
$labels[$sortOption['value']] = $sortOption['label'];
|
||||
}
|
||||
}
|
||||
|
||||
return $labels;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Amasty Improved Sorting GraphQl for Magento 2 (System)
|
||||
*/
|
||||
|
||||
namespace Amasty\SortingGraphQl\Model\Resolver\Product;
|
||||
|
||||
use Exception;
|
||||
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductImage;
|
||||
use Magento\Framework\GraphQl\Config\Element\Field;
|
||||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
|
||||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
||||
|
||||
class Image extends ProductImage
|
||||
{
|
||||
public const IS_AMASTY_FLAG = 'is_amasty_query';
|
||||
|
||||
/**
|
||||
* @param Field $field
|
||||
* @param ContextInterface $context
|
||||
* @param ResolveInfo $info
|
||||
* @param array|null $value
|
||||
* @param array|null $args
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function resolve(
|
||||
Field $field,
|
||||
$context,
|
||||
ResolveInfo $info,
|
||||
array $value = null,
|
||||
array $args = null
|
||||
): array {
|
||||
$result = parent::resolve($field, $context, $info, $value, $args);
|
||||
$result[self::IS_AMASTY_FLAG] = true;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user