Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Jan 18, 2019
2 parents 6c2c8e3 + ba9b625 commit cfd8bcf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ class Position extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
const TABLE_NAME = 'smile_virtualcategory_catalog_category_product_position';

/**
* Get product positions for a given categoryId and Store Id.
*
* @param int $categoryId The Category Id.
* @param int $storeId The Store Id.
*
* @return array
*/
public function getProductPositions($categoryId, $storeId)
{
$select = $this->getBaseSelect()
->where('category_id = ?', (int) $categoryId)
->where('store_id = ?', (int) $storeId)
->where('position IS NOT NULL')
->columns(['product_id', 'position']);

return $this->getConnection()->fetchPairs($select);
}

/**
* Get product blacklist for a given categoryId and Store Id.
*
* @param int $categoryId The Category Id.
* @param int $storeId The Store Id.
*
* @return array
*/
public function getProductBlacklist($categoryId, $storeId)
{
$select = $this->getBaseSelect()
->columns(['product_id'])
->where('category_id = ?', (int) $categoryId)
->where('store_id = ?', (int) $storeId)
->where('is_blacklisted = ?', (int) true);

return $this->getConnection()->fetchCol($select);
}

/**
* Load product positions for the given category.
*
Expand All @@ -40,21 +78,15 @@ class Position extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
public function getProductPositionsByCategory($category)
{
$storeId = 0;
$storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
if (is_object($category)) {
if ($category->getUseStorePositions()) {
$storeId = $category->getStoreId();
}
$category = $category->getId();
}

$select = $this->getBaseSelect()
->where('category_id = ?', (int) $category)
->where('store_id = ?', (int) $storeId)
->where('position IS NOT NULL')
->columns(['product_id', 'position']);

return $this->getConnection()->fetchPairs($select);
return $this->getProductPositions($category, $storeId);
}

/**
Expand All @@ -66,21 +98,15 @@ public function getProductPositionsByCategory($category)
*/
public function getProductBlacklistByCategory($category)
{
$storeId = 0;
$storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
if (is_object($category)) {
if ($category->getUseStorePositions()) {
$storeId = $category->getStoreId();
}
$category = $category->getId();
}

$select = $this->getBaseSelect()
->columns(['product_id'])
->where('category_id = ?', (int) $category)
->where('store_id = ?', (int) $storeId)
->where('is_blacklisted = ?', (int) true);

return $this->getConnection()->fetchCol($select);
return $this->getProductBlacklist($category, $storeId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,36 @@ function () use ($category) {
*/
private function getAffectedProductIds($category)
{
$oldPositionProductIds = array_keys($this->saveHandler->getProductPositionsByCategory($category));
$newPositionProductIds = array_keys($category->getSortedProducts());
$oldPositionProductIds = array_keys($this->saveHandler->getProductPositionsByCategory($category));
$defaultPositionProductIds = [];
$newPositionProductIds = array_keys($category->getSortedProducts());

$oldBlacklistedProductIds = array_values($this->saveHandler->getProductBlacklistByCategory($category));
$defaultBlacklistedProductIds = [];
$newBlacklistedProductIds = array_values($category->getBlacklistedProducts() ?? []);

if (true === (bool) $category->getUseStorePositions()) {
$defaultPositionProductIds = array_keys(
$this->saveHandler->getProductPositions(
$category->getId(),
\Magento\Store\Model\Store::DEFAULT_STORE_ID
)
);

$oldBlacklistedProductIds = array_values($this->saveHandler->getProductBlacklistByCategory($category));
$newBlacklistedProductIds = array_values($category->getBlacklistedProducts() ?? []);
$defaultBlacklistedProductIds = array_values(
$this->saveHandler->getProductBlacklist(
$category->getId(),
\Magento\Store\Model\Store::DEFAULT_STORE_ID
)
);
}

$affectedProductIds = array_merge(
$oldPositionProductIds,
$defaultPositionProductIds,
$newPositionProductIds,
$oldBlacklistedProductIds,
$defaultBlacklistedProductIds,
$newBlacklistedProductIds
);

Expand Down

0 comments on commit cfd8bcf

Please sign in to comment.