Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Special Price Limited functionality #39

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
148 changes: 78 additions & 70 deletions Model/Indexer/IndexBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Copyright © Karliuka Vitalii([email protected])
* Copyright © 2011-2018 Karliuka Vitalii([email protected])
*
* See COPYING.txt for license details.
*/
namespace Faonni\SmartCategory\Model\Indexer;
Expand All @@ -16,7 +17,7 @@
use Faonni\SmartCategory\Model\Rule;

/**
* Index builder
* SmartCategory IndexBuilder model
*/
class IndexBuilder
{
Expand All @@ -39,19 +40,20 @@ class IndexBuilder
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $_productFactory;

/**
* @var Product[]
*/
protected $_loadedProducts;

/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected $_connection;


/** @var \Magento\Framework\Indexer\IndexerRegistry */
protected $_indexerRegistry;
protected $_indexerRegistry;

/**
* @param RuleCollectionFactory $ruleCollectionFactory
Expand All @@ -66,14 +68,15 @@ public function __construct(
ResourceConnection $resource,
LoggerInterface $logger,
ProductFactory $productFactory,
IndexerRegistry $indexerRegistry
IndexerRegistry $indexerRegistry

) {
$this->_resource = $resource;
$this->_connection = $resource->getConnection();
$this->_ruleCollectionFactory = $ruleCollectionFactory;
$this->_logger = $logger;
$this->_productFactory = $productFactory;
$this->_indexerRegistry = $indexerRegistry;
$this->_indexerRegistry = $indexerRegistry;
}

/**
Expand Down Expand Up @@ -116,14 +119,14 @@ public function reindexByIds(array $ids)
*/
protected function doReindexByIds($ids)
{
foreach ($this->getAllRules() as $rule) {
foreach ($this->getAllRules() as $rule) {
foreach ($ids as $productId) {
$this->applyRule($rule, $this->getProduct($productId));
$this->productCategoryReindexRow($productId);
$this->productCategoryReindexRow($productId);
}
}
}

/**
* Reindex product categories by productId
*
Expand All @@ -133,9 +136,9 @@ protected function doReindexByIds($ids)
protected function productCategoryReindexRow($productId)
{
$productCategoryIndexer = $this->_indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID);
$productCategoryIndexer->reindexRow($productId);
$productCategoryIndexer->reindexRow($productId);
}

/**
* Reindex category products by productId
*
Expand All @@ -145,9 +148,9 @@ protected function productCategoryReindexRow($productId)
protected function categoryProductReindexRow($categoryId)
{
$categoryProductIndexer = $this->_indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
$categoryProductIndexer->reindexRow($categoryId);
}

$categoryProductIndexer->reindexRow($categoryId);
}
/**
* Full reindex
*
Expand All @@ -174,7 +177,7 @@ protected function doReindexFull()
{
foreach ($this->getAllRules() as $rule) {
$this->updateRuleProductData($rule);
$this->categoryProductReindexRow($rule->getId());
$this->categoryProductReindexRow($rule->getId());
}
}

Expand All @@ -187,12 +190,12 @@ protected function doReindexFull()
*/
protected function cleanByIds($categoryId, $productIds)
{
$this->_connection->delete(
$this->getTable('catalog_category_product'),
['category_id = ?' => $categoryId, 'product_id IN (?)' => $productIds]
);
$this->_connection->delete(
$this->getTable('catalog_category_product'),
['category_id = ?' => $categoryId, 'product_id IN (?)' => $productIds]
);
}

/**
* Insert products
*
Expand All @@ -202,20 +205,20 @@ protected function cleanByIds($categoryId, $productIds)
*/
protected function insertMultiple($categoryId, $productIds)
{
$data = [];
foreach ($productIds as $productId => $position) {
$data[] = [
'category_id' => $categoryId,
'product_id' => $productId,
'position' => $position
];
}
$this->_connection->insertMultiple(
$this->getTable('catalog_category_product'),
$data
);
$data = [];
foreach ($productIds as $productId => $position) {
$data[] = [
'category_id' => $categoryId,
'product_id' => $productId,
'position' => $position
];
}
$this->_connection->insertMultiple(
$this->getTable('catalog_category_product'),
$data
);
}

/**
* @param Rule $rule
* @param Product $product
Expand All @@ -226,19 +229,24 @@ protected function insertMultiple($categoryId, $productIds)
protected function applyRule(Rule $rule, $product)
{
$ruleId = $rule->getId();
$productId = $product->getId();

if ($rule->validate($product)) {
if (!$this->checkPostedProduct($ruleId, $productId)) {
$this->insertMultiple($ruleId, [$productId => '1']);
}
$productId = $product->getId();
if ($this->validateProduct($rule, $product)) {
if (!$this->checkPostedProduct($ruleId, $productId)) {
$this->insertMultiple($ruleId, [$productId => '1']);
}
return $this;
} else {
$this->cleanByIds($ruleId, [$productId]);
}

$this->cleanByIds($ruleId, [$productId]);
return $this;
}

protected function validateProduct($rule, $product) {
return $rule->validate($product);
}

/**
* @param string $tableName
* @return string
Expand All @@ -247,40 +255,40 @@ protected function getTable($tableName)
{
return $this->_resource->getTableName($tableName);
}

/**
* @param integer $categoryId
* @param integer $productId
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function checkPostedProduct($categoryId, $productId)
{
$select = $this->_connection
->select()
->from($this->getTable('catalog_category_product'), [new \Zend_Db_Expr('COUNT(*)')])
->where('category_id = ?', $categoryId)
->where('product_id = ?', $productId);

return (0 < $this->_connection->fetchOne($select)) ? true : false;
}

protected function checkPostedProduct($categoryId, $productId)
{
$select = $this->_connection
->select()
->from($this->getTable('catalog_category_product'), [new \Zend_Db_Expr('COUNT(*)')])
->where('category_id = ?', $categoryId)
->where('product_id = ?', $productId);
return (0 < $this->_connection->fetchOne($select)) ? true : false;
}
/**
* @param integer $categoryId
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function getPostedProductData($categoryId)
{
$select = $this->_connection
->select()
->from($this->getTable('catalog_category_product'), ['product_id', 'position'])
->where('category_id = ?', $categoryId);

return $this->_connection->fetchPairs($select);
}
protected function getPostedProductData($categoryId)
{
$select = $this->_connection
->select()
->from($this->getTable('catalog_category_product'), ['product_id', 'position'])
->where('category_id = ?', $categoryId);
return $this->_connection->fetchPairs($select);
}

/**
* @param Rule $rule
Expand All @@ -295,14 +303,14 @@ protected function updateRuleProductData(Rule $rule)

$deleteIds = array_diff_key($postedProducts, $matchingProducts);
$insertIds = array_diff_key($matchingProducts, $postedProducts);

if (0 < count($deleteIds)) {
$this->cleanByIds($rule->getId(), array_keys($deleteIds));
}

$this->cleanByIds($rule->getId(), array_keys($deleteIds));
}
if (0 < count($insertIds)) {
$this->insertMultiple($rule->getId(), $insertIds);
}
$this->insertMultiple($rule->getId(), $insertIds);
}
return $this;
}

Expand All @@ -324,10 +332,10 @@ protected function getProduct($productId)
{
if (!isset($this->_loadedProducts[$productId])) {
$this->_loadedProducts[$productId] = $this->_productFactory->create()
->load($productId);
->load($productId);
}
return $this->_loadedProducts[$productId];
}
}

/**
* @param \Exception $e
Expand Down
23 changes: 11 additions & 12 deletions Model/Rule/Condition/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use Magento\Rule\Model\Condition\Combine as RuleCombine;
use Magento\Rule\Model\Condition\Context;
use Faonni\SmartCategory\Model\Rule\Condition\ProductFactory;
use Faonni\SmartCategory\Model\Rule\Condition\Product\Sale;
use Faonni\SmartCategory\Model\Rule\Condition\Product\News;
use Faonni\SmartCategory\Model\Rule\Condition\Product;
use Faonni\SmartCategory\Model\Rule\Condition\Product\News;

/**
* Combine model
Expand Down Expand Up @@ -45,6 +44,11 @@ public function __construct(
$this->setType(self::class);
}

/**
* Get inherited conditions selectors
*
* @return array
*/
/**
* Get inherited conditions selectors
*
Expand All @@ -57,23 +61,17 @@ public function getNewChildSelectOptions()
->getAttributeOption();

$attributes = [
[
'value' => Sale::class,
'label' => __('Special Price')
],
[
'value' => News::class,
'label' => __('New')
]
];

foreach ($productAttributes as $code => $label) {
if ('special_price' != $code) {
$attributes[] = [
'value' => Product::class . '|' . $code,
'label' => $label,
];
}
$attributes[] = [
'value' => Product::class . '|' . $code,
'label' => $label,
];
}

$conditions = parent::getNewChildSelectOptions();
Expand All @@ -89,6 +87,7 @@ public function getNewChildSelectOptions()
);
return $conditions;
}


/**
* Collect validated attributes
Expand Down
Loading