From c874a3aeca6cffac54f58abf211a602f24a5c640 Mon Sep 17 00:00:00 2001 From: "harsha.amaraweera" Date: Wed, 24 Jul 2024 14:13:48 +0930 Subject: [PATCH] ALG-125: fixing 2.4.6 compatible issues --- .../GetValidCategoryPagesForSitemap.php | 35 +++++++----- .../GetValidProductPagesForSitemap.php | 57 +++++++++++++------ .../AddShowInXmlSitemapCategoryAttribute.php | 6 +- .../AddShowInXmlSitemapProductAttribute.php | 7 +-- 4 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php b/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php index fd57b2f..09e3778 100644 --- a/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php +++ b/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php @@ -23,12 +23,18 @@ class GetValidCategoryPagesForSitemap { + + /** + * @var Select + */ + private $select = null; + /** * Attribute cache * * @var array */ - private $_attributesCache = []; + private $attributesCache = []; /** * @param AligentSitemapConfig $aligentSitemapConfig @@ -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( @@ -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; @@ -136,10 +142,10 @@ 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(), @@ -147,7 +153,7 @@ private function getAttributeDetails(string $attributeCode): array 'backend_type' => $attribute->getBackendType(), ]; } - $attribute = $this->_attributesCache[$attributeCode]; + $attribute = $this->attributesCache[$attributeCode]; return $attribute; } @@ -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; } @@ -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', @@ -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_' . @@ -254,6 +259,6 @@ private function addFilter(Category $subject, int $storeId, string $attributeCod } } - return $this->_select; + return $this->select; } } diff --git a/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php b/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php index 21801a4..396f24d 100644 --- a/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php +++ b/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php @@ -32,6 +32,16 @@ class GetValidProductPagesForSitemap { + /** + * @var Select + */ + private $select = null; + + /** + * @var array + */ + private $attributesCache = null; + /** * Product constructor. * @@ -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( @@ -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()); @@ -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; @@ -174,10 +196,10 @@ 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(), @@ -185,9 +207,7 @@ private function getAttribute(string $attributeCode): array 'backend_type' => $attribute->getBackendType(), ]; } - $attribute = $this->_attributesCache[$attributeCode]; - - return $attribute; + return $this->attributesCache[$attributeCode]; } /** @@ -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; } @@ -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; } /** @@ -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) @@ -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' @@ -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 ] diff --git a/Setup/Patch/Data/AddShowInXmlSitemapCategoryAttribute.php b/Setup/Patch/Data/AddShowInXmlSitemapCategoryAttribute.php index cdef3bb..8e86fc3 100644 --- a/Setup/Patch/Data/AddShowInXmlSitemapCategoryAttribute.php +++ b/Setup/Patch/Data/AddShowInXmlSitemapCategoryAttribute.php @@ -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 { @@ -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( @@ -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, diff --git a/Setup/Patch/Data/AddShowInXmlSitemapProductAttribute.php b/Setup/Patch/Data/AddShowInXmlSitemapProductAttribute.php index 94fd8bb..6380933 100644 --- a/Setup/Patch/Data/AddShowInXmlSitemapProductAttribute.php +++ b/Setup/Patch/Data/AddShowInXmlSitemapProductAttribute.php @@ -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 { @@ -49,9 +49,6 @@ public static function getDependencies(): array /** * @inheritDoc - * @return $this - * @throws LocalizedException - * @throws Zend_Validate_Exception */ public function apply(): AddShowInXmlSitemapProductAttribute { @@ -67,7 +64,7 @@ public function apply(): AddShowInXmlSitemapProductAttribute * * @return void * @throws LocalizedException - * @throws Zend_Validate_Exception + * @throws ValidateException */ private function addShowInXmlSitemap(): void {