Skip to content

Commit

Permalink
Merge pull request #4 from aligent/feature/sitemap-adobe246-compatibl…
Browse files Browse the repository at this point in the history
…e-fixes

ALG-125: fixing 2.4.6 compatible issues
  • Loading branch information
harshaaligent authored Jul 24, 2024
2 parents 0d90be2 + c874a3a commit 71dcc03
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@

class GetValidCategoryPagesForSitemap
{

/**
* @var Select
*/
private $select = null;

/**
* Attribute cache
*
* @var array
*/
private $_attributesCache = [];
private $attributesCache = [];

/**
* @param AligentSitemapConfig $aligentSitemapConfig
Expand Down Expand Up @@ -85,20 +91,20 @@ public function aroundGetCollection(Category $subject, callable $proceed, int $s
$showInSitemapAttributeDetails = $this->getAttributeDetails('show_in_sitemap');

// phpcs:disable
$this->_select = $connection->select()->from(
$this->select = $connection->select()->from(
$subject->getMainTable()
)->where(
$subject->getIdFieldName() . '=?',
$store->getRootCategoryId()
);
// phpcs:enable
$categoryRow = $connection->fetchRow($this->_select);
$categoryRow = $connection->fetchRow($this->select);

if (!$categoryRow) {
return [];
}

$this->_select = $connection->select()->from(
$this->select = $connection->select()->from(
['e' => $subject->getMainTable()],
[$subject->getIdFieldName(), 'updated_at']
)->joinLeft(
Expand All @@ -118,7 +124,7 @@ public function aroundGetCollection(Category $subject, callable $proceed, int $s

$this->addFilter($subject, $storeId, 'is_active', 1);

$query = $connection->query($this->_select);
$query = $connection->query($this->select);
while ($row = $query->fetch()) {
$category = $this->prepareCategory($subject, $row);
$categories[$category->getId()] = $category;
Expand All @@ -136,18 +142,18 @@ public function aroundGetCollection(Category $subject, callable $proceed, int $s
*/
private function getAttributeDetails(string $attributeCode): array
{
if (!isset($this->_attributesCache[$attributeCode])) {
if (!isset($this->attributesCache[$attributeCode])) {
$attribute = $this->categoryResource->getAttribute($attributeCode);

$this->_attributesCache[$attributeCode] = [
$this->attributesCache[$attributeCode] = [
'entity_type_id' => $attribute->getEntityTypeId(),
'attribute_id' => $attribute->getId(),
'table' => $attribute->getBackend()->getTable(),
'is_global' => $attribute->getIsGlobal(),
'backend_type' => $attribute->getBackendType(),
];
}
$attribute = $this->_attributesCache[$attributeCode];
$attribute = $this->attributesCache[$attributeCode];

return $attribute;
}
Expand Down Expand Up @@ -191,7 +197,7 @@ private function addFilter(Category $subject, int $storeId, string $attributeCod
$meta = $this->metadataPool->getMetadata(CategoryInterface::class);
$linkField = $meta->getLinkField();

if (!$this->_select instanceof Select) {
if (!$this->select instanceof Select) {
return false;
}

Expand All @@ -206,13 +212,12 @@ private function addFilter(Category $subject, int $storeId, string $attributeCod
break;
default:
return false;
break;
}

if ($attribute['backend_type'] == 'static') {
$this->_select->where('e.' . $attributeCode . $conditionRule, $value);
$this->select->where('e.' . $attributeCode . $conditionRule, $value);
} else {
$this->_select->join(
$this->select->join(
['t1_' . $attributeCode => $attribute['table']],
'e.' . $linkField . ' = t1_' . $attributeCode . '.' . $linkField .
' AND t1_' . $attributeCode . '.store_id = 0',
Expand All @@ -223,14 +228,14 @@ private function addFilter(Category $subject, int $storeId, string $attributeCod
);

if ($attribute['is_global']) {
$this->_select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value);
$this->select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value);
} else {
$ifCase = $subject->getConnection()->getCheckSql(
't2_' . $attributeCode . '.value_id > 0',
't2_' . $attributeCode . '.value',
't1_' . $attributeCode . '.value'
);
$this->_select->joinLeft(
$this->select->joinLeft(
['t2_' . $attributeCode => $attribute['table']],
$subject->getConnection()->quoteInto(
't1_' .
Expand All @@ -254,6 +259,6 @@ private function addFilter(Category $subject, int $storeId, string $attributeCod
}
}

return $this->_select;
return $this->select;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@

class GetValidProductPagesForSitemap
{
/**
* @var Select
*/
private $select = null;

/**
* @var array
*/
private $attributesCache = null;

/**
* Product constructor.
*
Expand Down Expand Up @@ -102,7 +112,7 @@ public function aroundGetCollection(Product $subject, callable $proceed, int $st
}

$connection = $subject->getConnection();
$this->_select = $connection->select()->from(
$this->select = $connection->select()->from(
['e' => $subject->getMainTable()],
[$subject->getIdFieldName(), $this->productResource->getLinkField(), 'updated_at']
)->joinInner(
Expand All @@ -125,8 +135,20 @@ public function aroundGetCollection(Product $subject, callable $proceed, int $st
$store->getWebsiteId()
)->where('x.value = ?', 1);

$this->addFilter($subject, (int)$store->getId(), 'visibility', $this->productVisibility->getVisibleInSiteIds(), 'in');
$this->addFilter($subject, (int)$store->getId(), 'status', $this->productStatus->getVisibleStatusIds(), 'in');
$this->addFilter(
$subject,
(int)$store->getId(),
'visibility',
$this->productVisibility->getVisibleInSiteIds(),
'in'
);
$this->addFilter(
$subject,
(int)$store->getId(),
'status',
$this->productStatus->getVisibleStatusIds(),
'in'
);

// Join product images required attributes
$imageIncludePolicy = $this->sitemapData->getProductImageIncludePolicy($store->getId());
Expand All @@ -139,7 +161,7 @@ public function aroundGetCollection(Product $subject, callable $proceed, int $st
}
}

$query = $connection->query($subject->prepareSelectStatement($this->_select));
$query = $connection->query($subject->prepareSelectStatement($this->select));
while ($row = $query->fetch()) {
$product = $this->prepareProduct($subject, $row, (int)$store->getId());
$products[$product->getId()] = $product;
Expand Down Expand Up @@ -174,20 +196,18 @@ private function isCategoryProductURLsConfig(int $storeId = null): bool
*/
private function getAttribute(string $attributeCode): array
{
if (!isset($this->_attributesCache[$attributeCode])) {
if (!isset($this->attributesCache[$attributeCode])) {
$attribute = $this->productResource->getAttribute($attributeCode);

$this->_attributesCache[$attributeCode] = [
$this->attributesCache[$attributeCode] = [
'entity_type_id' => $attribute->getEntityTypeId(),
'attribute_id' => $attribute->getId(),
'table' => $attribute->getBackend()->getTable(),
'is_global' => $attribute->getIsGlobal(),
'backend_type' => $attribute->getBackendType(),
];
}
$attribute = $this->_attributesCache[$attributeCode];

return $attribute;
return $this->attributesCache[$attributeCode];
}

/**
Expand All @@ -206,7 +226,7 @@ private function getAttribute(string $attributeCode): array
*/
private function addFilter(Product $subject, int $storeId, string $attributeCode, mixed $value, string $type = '=')
{
if (!$this->_select instanceof Select) {
if (!$this->select instanceof Select) {
return false;
}

Expand All @@ -223,22 +243,22 @@ private function addFilter(Product $subject, int $storeId, string $attributeCode

$attribute = $this->getAttribute($attributeCode);
if ($attribute['backend_type'] == 'static') {
$this->_select->where('e.' . $attributeCode . $conditionRule, $value);
$this->select->where('e.' . $attributeCode . $conditionRule, $value);
} else {
$this->joinAttribute($subject, $storeId, $attributeCode);
if ($attribute['is_global']) {
$this->_select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value);
$this->select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value);
} else {
$ifCase = $subject->getConnection()->getCheckSql(
't2_' . $attributeCode . '.value_id > 0',
't2_' . $attributeCode . '.value',
't1_' . $attributeCode . '.value'
);
$this->_select->where('(' . $ifCase . ')' . $conditionRule, $value);
$this->select->where('(' . $ifCase . ')' . $conditionRule, $value);
}
}

return $this->_select;
return $this->select;
}

/**
Expand All @@ -260,7 +280,7 @@ private function joinAttribute(Product $subject, int $storeId, string $attribute
$attribute = $this->getAttribute($attributeCode);
$linkField = $this->productResource->getLinkField();
$attrTableAlias = 't1_' . $attributeCode;
$this->_select->joinLeft(
$this->select->joinLeft(
[$attrTableAlias => $attribute['table']],
"e.{$linkField} = {$attrTableAlias}.{$linkField}"
. ' AND ' . $connection->quoteInto($attrTableAlias . '.store_id = ?', Store::DEFAULT_STORE_ID)
Expand All @@ -272,7 +292,7 @@ private function joinAttribute(Product $subject, int $storeId, string $attribute

if (!$attribute['is_global']) {
$attrTableAlias2 = 't2_' . $attributeCode;
$this->_select->joinLeft(
$this->select->joinLeft(
['t2_' . $attributeCode => $attribute['table']],
"{$attrTableAlias}.{$linkField} = {$attrTableAlias2}.{$linkField}"
. ' AND ' . $attrTableAlias . '.attribute_id = ' . $attrTableAlias2 . '.attribute_id'
Expand All @@ -281,13 +301,14 @@ private function joinAttribute(Product $subject, int $storeId, string $attribute
);
// Store scope attribute value
$columnValue = $subject->getConnection()->getIfNullSql(
't2_' . $attributeCode . '.value', $columnValue
't2_' . $attributeCode . '.value',
$columnValue
);
}

// Add attribute value to result set if needed
if (isset($column)) {
$this->_select->columns(
$this->select->columns(
[
$column => $columnValue
]
Expand Down
6 changes: 1 addition & 5 deletions Setup/Patch/Data/AddShowInXmlSitemapCategoryAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Zend_Validate_Exception;

class AddShowInXmlSitemapCategoryAttribute implements DataPatchInterface
{
Expand Down Expand Up @@ -49,11 +47,10 @@ public function getAliases(): array

/**
* @inheritDoc
* @throws LocalizedException
* @throws Zend_Validate_Exception
*/
public function apply(): AddShowInXmlSitemapCategoryAttribute
{

$this->moduleDataSetup->startSetup();

$attributeSetId = $this->eavSetup->getDefaultAttributeSetId(
Expand All @@ -73,7 +70,6 @@ public function apply(): AddShowInXmlSitemapCategoryAttribute
);
$config['sort_order'] = $sortOrder;
$config['group'] = $attributeGroupId;

$this->eavSetup->addAttribute(
CategoryAttributeInterface::ENTITY_TYPE_CODE,
self::ATTRIBUTE_CODE,
Expand Down
7 changes: 2 additions & 5 deletions Setup/Patch/Data/AddShowInXmlSitemapProductAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Exception\LocalizedException;
use Zend_Validate_Exception;
use Magento\Framework\Validator\ValidateException;

class AddShowInXmlSitemapProductAttribute implements DataPatchInterface
{
Expand Down Expand Up @@ -49,9 +49,6 @@ public static function getDependencies(): array

/**
* @inheritDoc
* @return $this
* @throws LocalizedException
* @throws Zend_Validate_Exception
*/
public function apply(): AddShowInXmlSitemapProductAttribute
{
Expand All @@ -67,7 +64,7 @@ public function apply(): AddShowInXmlSitemapProductAttribute
*
* @return void
* @throws LocalizedException
* @throws Zend_Validate_Exception
* @throws ValidateException
*/
private function addShowInXmlSitemap(): void
{
Expand Down

0 comments on commit 71dcc03

Please sign in to comment.