ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Rule\Condition;
|
||||
|
||||
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Locale\FormatInterface;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\Rule\Model\Condition\AbstractCondition;
|
||||
use Magento\Rule\Model\Condition\Context;
|
||||
|
||||
class Bestsellers extends AbstractCondition implements CustomConditionInterface
|
||||
{
|
||||
private const FLAG = 'sorting_bestseller';
|
||||
private const BESTSELLER_TABLE = 'amasty_sorting_bestsellers';
|
||||
|
||||
/**
|
||||
* @var FormatInterface
|
||||
*/
|
||||
private $format;
|
||||
|
||||
/**
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
FormatInterface $format,
|
||||
ResourceConnection $resource,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
$this->format = $format;
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductCollection $collection
|
||||
* @return void
|
||||
*/
|
||||
public function collectValidatedAttributes(ProductCollection $collection): void
|
||||
{
|
||||
if (!$collection->getFlag(self::FLAG)) {
|
||||
$collection->setFlag(self::FLAG);
|
||||
$bestSellerTable = $this->resource->getTableName(self::BESTSELLER_TABLE);
|
||||
|
||||
if ($this->resource->getConnection()->isTableExists($bestSellerTable)) {
|
||||
$collection->getSelect()->joinLeft(
|
||||
['bestseller' => $bestSellerTable],
|
||||
sprintf(
|
||||
'e.entity_id = bestseller.product_id AND bestseller.store_id = %s',
|
||||
$collection->getStoreId()
|
||||
),
|
||||
['qty_ordered']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validate(AbstractModel $model): bool
|
||||
{
|
||||
$attribute = $this->getAttribute();
|
||||
|
||||
if ($model->hasData($attribute) && $model->getData($attribute) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::validate($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAttribute(): string
|
||||
{
|
||||
return 'qty_ordered';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Phrase
|
||||
*/
|
||||
public function getAttributeElementHtml()
|
||||
{
|
||||
return __('Bestseller Sales');
|
||||
}
|
||||
|
||||
public function getInputType(): string
|
||||
{
|
||||
return 'numeric';
|
||||
}
|
||||
|
||||
public function getValueElementType(): string
|
||||
{
|
||||
return 'text';
|
||||
}
|
||||
|
||||
public function getDefaultOperatorOptions(): array
|
||||
{
|
||||
$values = parent::getDefaultOperatorOptions();
|
||||
unset(
|
||||
$values['{}'],
|
||||
$values['!{}'],
|
||||
$values['<=>']
|
||||
);
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function loadArray($arr): AbstractCondition
|
||||
{
|
||||
$tmp = [];
|
||||
|
||||
foreach (explode(',', ($arr['value'] ?? '')) as $value) {
|
||||
$tmp[] = $this->format->getNumber($value);
|
||||
}
|
||||
|
||||
$arr['value'] = implode(',', $tmp);
|
||||
|
||||
return parent::loadArray($arr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Rule\Condition;
|
||||
|
||||
use Magento\CatalogRule\Model\Rule\Condition\Combine as MagentoCombineRule;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\Rule\Model\Condition\AbstractCondition;
|
||||
|
||||
class Combine extends MagentoCombineRule
|
||||
{
|
||||
private const SORTING_CONDITIONS = 'amasty_sorting';
|
||||
|
||||
public function getNewChildSelectOptions(): array
|
||||
{
|
||||
$conditions = AbstractCondition::getNewChildSelectOptions();
|
||||
$conditions[] = ['label' => __('Conditions Combination'), 'value' => static::class];
|
||||
$conditions[] = ['label' => __('Product Attribute'), 'value' => $this->getProductConditions()];
|
||||
$conditions[] = [
|
||||
'label' => $this->getSortingConditionsLabel(),
|
||||
'value' => $this->isSortingEnabled() ? $this->getSortingConditions() : []
|
||||
];
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
private function getSortingConditions(): array
|
||||
{
|
||||
$sortingConditions = (array) $this->getData(self::SORTING_CONDITIONS);
|
||||
|
||||
return array_reduce($sortingConditions, [$this, 'formatCondition'], []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formation of an array of conditions in the required format
|
||||
*
|
||||
* @param array $carry
|
||||
* @param CustomConditionInterface $condition
|
||||
* @return array
|
||||
*/
|
||||
private function formatCondition(array $carry, CustomConditionInterface $condition): array
|
||||
{
|
||||
$carry[] = [
|
||||
'label' => $condition->getAttributeElementHtml(),
|
||||
'value' => ltrim(get_class($condition), '/')
|
||||
];
|
||||
|
||||
return $carry;
|
||||
}
|
||||
|
||||
private function getSortingConditionsLabel(): Phrase
|
||||
{
|
||||
$title = __('Improved Sorting (not installed)');
|
||||
|
||||
if ($this->isSortingEnabled()) {
|
||||
$title = __('Improved Sorting');
|
||||
}
|
||||
|
||||
return $title;
|
||||
}
|
||||
|
||||
private function isSortingEnabled(): bool
|
||||
{
|
||||
return $this->getData('module_manager') && $this->getData('module_manager')->isEnabled('Amasty_Sorting');
|
||||
}
|
||||
|
||||
private function getProductConditions(): array
|
||||
{
|
||||
$productAttributes = $this->_productFactory->create()->loadAttributeOptions()->getAttributeOption();
|
||||
$attributes = [];
|
||||
|
||||
foreach ($productAttributes as $code => $label) {
|
||||
$attributes[] = [
|
||||
'value' => 'Magento\CatalogRule\Model\Rule\Condition\Product|' . $code,
|
||||
'label' => $label,
|
||||
];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOperatorElementHtml(): string
|
||||
{
|
||||
return $this->getOperatorElement()->getHtml();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Rule\Condition;
|
||||
|
||||
/**
|
||||
* Custom conditions must implement this interface to be identified
|
||||
*/
|
||||
interface CustomConditionInterface
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user