From 44452d196ea0dda84c6b333235fc69e97bc63505 Mon Sep 17 00:00:00 2001 From: Onesh Date: Thu, 22 Sep 2022 15:56:50 +0700 Subject: [PATCH 1/5] Compatible 2.4.5 --- Plugin/SeoRender.php | 64 ++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/Plugin/SeoRender.php b/Plugin/SeoRender.php index 4bb7341..8729b64 100755 --- a/Plugin/SeoRender.php +++ b/Plugin/SeoRender.php @@ -51,6 +51,7 @@ use Magento\Store\Model\StoreManagerInterface; use Mageplaza\Seo\Helper\Data as HelperData; use Mageplaza\Seo\Model\Config\Source\PriceValidUntil; +use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface as SourceItems; /** * Class SeoRender @@ -169,30 +170,36 @@ class SeoRender */ protected $collectionFactory; + /** + * @var SourceItems + */ + protected $sourceItemsBySku; + /** * SeoRender constructor. * - * @param PageConfig $pageConfig - * @param Http $request - * @param HelperData $helpData - * @param StockItemRepository $stockItemRepository - * @param Registry $registry - * @param ReviewFactory $reviewFactory - * @param StoreManagerInterface $storeManager - * @param UrlInterface $urlBuilder - * @param ProductFactory $productFactory - * @param ManagerInterface $messageManager + * @param PageConfig $pageConfig + * @param Http $request + * @param HelperData $helpData + * @param StockItemRepository $stockItemRepository + * @param Registry $registry + * @param ReviewFactory $reviewFactory + * @param StoreManagerInterface $storeManager + * @param UrlInterface $urlBuilder + * @param ProductFactory $productFactory + * @param ManagerInterface $messageManager * @param StockRegistryInterface $stockState - * @param SearchHelper $searchHelper - * @param PriceHelper $priceHelper - * @param Manager $eventManager - * @param DateTime $dateTime - * @param TimezoneInterface $timeZoneInterface - * @param ReviewCollection $reviewCollection - * @param ModuleManager $moduleManager - * @param RatingFactory $ratingFactory - * @param ReviewResourceModel $reviewResourceModel - * @param CollectionFactory $collectionFactory + * @param SearchHelper $searchHelper + * @param PriceHelper $priceHelper + * @param Manager $eventManager + * @param DateTime $dateTime + * @param TimezoneInterface $timeZoneInterface + * @param ReviewCollection $reviewCollection + * @param ModuleManager $moduleManager + * @param RatingFactory $ratingFactory + * @param ReviewResourceModel $reviewResourceModel + * @param CollectionFactory $collectionFactory + * @param SourceItems $sourceItemsBySku */ public function __construct( PageConfig $pageConfig, @@ -215,7 +222,8 @@ public function __construct( ModuleManager $moduleManager, RatingFactory $ratingFactory, ReviewResourceModel $reviewResourceModel, - CollectionFactory $collectionFactory + CollectionFactory $collectionFactory, + SourceItems $sourceItemsBySku ) { $this->pageConfig = $pageConfig; $this->request = $request; @@ -238,6 +246,7 @@ public function __construct( $this->ratingFactory = $ratingFactory; $this->reviewResourceModel = $reviewResourceModel; $this->collectionFactory = $collectionFactory; + $this->sourceItemsBySku = $sourceItemsBySku; } /** @@ -346,6 +355,15 @@ public function showProductStructuredData() $product->getId(), $product->getStore()->getWebsiteId() ); + + if ($sourceItemList = $this->sourceItemsBySku->execute($product->getSku())) { + $stockQty = []; + foreach ($sourceItemList as $source) { + $stockQty[] = (int) $source['quantity']; + } + $stockItem = max($stockQty); + } + $priceValidUntil = $currentProduct->getSpecialToDate(); $modelAttribute = $this->helperData->getRichsnippetsConfig('model_value'); $modelValue = $product->getResource() @@ -370,12 +388,12 @@ public function showProductStructuredData() 'description' => $currentProduct->getDescription() ? trim(strip_tags($currentProduct->getDescription())) : '', 'sku' => $currentProduct->getSku(), 'url' => $currentProduct->getProductUrl(), - 'image' => $this->getUrl('pub/media/catalog') . 'product' . $currentProduct->getImage(), + 'image' => $this->getUrl('/media/catalog') . 'product' . $currentProduct->getImage(), 'offers' => [ '@type' => 'Offer', 'priceCurrency' => $this->_storeManager->getStore()->getCurrentCurrencyCode(), 'price' => $currentProduct->getPriceInfo()->getPrice('final_price')->getValue(), - 'itemOffered' => $stockItem->getQty(), + 'itemOffered' => is_integer($stockItem) ? $stockItem : $stockItem->getQty(), 'availability' => 'http://schema.org/' . $availability, 'url' => $currentProduct->getProductUrl() ], From e95d2e82f83b14ef75a3b073952207558dbd0f1f Mon Sep 17 00:00:00 2001 From: Onesh Date: Mon, 26 Sep 2022 15:30:12 +0700 Subject: [PATCH 2/5] Compatible 2.4.5 --- Block/Adminhtml/SeoChecker/CheckForm.php | 14 ++++---------- Plugin/SeoRender.php | 8 +++++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Block/Adminhtml/SeoChecker/CheckForm.php b/Block/Adminhtml/SeoChecker/CheckForm.php index c47d6be..8c75d06 100644 --- a/Block/Adminhtml/SeoChecker/CheckForm.php +++ b/Block/Adminhtml/SeoChecker/CheckForm.php @@ -24,7 +24,7 @@ use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\ProductFactory; -use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder; +use Magento\Cms\Helper\Page; use Magento\Cms\Model\PageFactory; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\Template; @@ -45,7 +45,7 @@ class CheckForm extends Template protected $_template = 'seocheck.phtml'; /** - * @var UrlBuilder + * @var Page */ protected $cmsUrl; @@ -88,7 +88,7 @@ class CheckForm extends Template */ public function __construct( Context $context, - UrlBuilder $cmsUrl, + Page $cmsUrl, PageFactory $cmsPageFactory, ProductFactory $productFactory, ProductRepositoryInterface $productRepository, @@ -115,13 +115,11 @@ public function __construct( public function getLink() { $id = $this->_request->getParam('id'); - $storeCode = $this->_storeManager->getStore()->getCode(); $storeId = $this->_request->getParam('store'); $actionName = $this->_request->getFullActionName(); if ($storeId === 0 || $storeId === null) { $defaultStore = $this->_storeManager->getDefaultStoreView(); $storeId = $defaultStore->getId(); - $storeCode = $defaultStore->getCode(); } switch ($actionName) { case 'catalog_product_edit': @@ -135,11 +133,7 @@ public function getLink() break; case 'cms_page_edit': $pageId = $this->_request->getParam('page_id'); - $url = $this->cmsUrl->getUrl( - $this->cmsPageFactory->create()->load($pageId)->getIdentifier(), - $storeId, - $storeCode - ); + $url = $this->cmsUrl->getPageUrl($pageId); break; default: $url = ''; diff --git a/Plugin/SeoRender.php b/Plugin/SeoRender.php index 8729b64..5b6b6bd 100755 --- a/Plugin/SeoRender.php +++ b/Plugin/SeoRender.php @@ -357,11 +357,13 @@ public function showProductStructuredData() ); if ($sourceItemList = $this->sourceItemsBySku->execute($product->getSku())) { - $stockQty = []; + $stockQty = 0; foreach ($sourceItemList as $source) { - $stockQty[] = (int) $source['quantity']; + if ($source->getSourceCode() != 'default') { + $stockQty += (int) $source['quantity']; + } } - $stockItem = max($stockQty); + $stockItem = $stockQty; } $priceValidUntil = $currentProduct->getSpecialToDate(); From c8e89764bd2c50641e0a3e4cd3520cd23da8021a Mon Sep 17 00:00:00 2001 From: Onesh Date: Tue, 27 Sep 2022 16:16:18 +0700 Subject: [PATCH 3/5] Compatible 2.4.5 --- Plugin/SeoRender.php | 50 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/Plugin/SeoRender.php b/Plugin/SeoRender.php index 5b6b6bd..d482a3a 100755 --- a/Plugin/SeoRender.php +++ b/Plugin/SeoRender.php @@ -28,6 +28,7 @@ use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\CatalogInventory\Model\Stock\StockItemRepository; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Request\Http; use Magento\Framework\DataObject; use Magento\Framework\Event\Manager; @@ -52,6 +53,8 @@ use Mageplaza\Seo\Helper\Data as HelperData; use Mageplaza\Seo\Model\Config\Source\PriceValidUntil; use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface as SourceItems; +use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite as AssignedStock; +use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku as SalableQuantity; /** * Class SeoRender @@ -175,6 +178,16 @@ class SeoRender */ protected $sourceItemsBySku; + /** + * @var AssignedStock + */ + protected $assignedStock; + + /** + * @var SalableQuantity + */ + protected $salableQuantity; + /** * SeoRender constructor. * @@ -200,6 +213,8 @@ class SeoRender * @param ReviewResourceModel $reviewResourceModel * @param CollectionFactory $collectionFactory * @param SourceItems $sourceItemsBySku + * @param AssignedStock $assignedStock + * @param SalableQuantity $salableQuantity */ public function __construct( PageConfig $pageConfig, @@ -223,7 +238,9 @@ public function __construct( RatingFactory $ratingFactory, ReviewResourceModel $reviewResourceModel, CollectionFactory $collectionFactory, - SourceItems $sourceItemsBySku + SourceItems $sourceItemsBySku, + AssignedStock $assignedStock, + SalableQuantity $salableQuantity ) { $this->pageConfig = $pageConfig; $this->request = $request; @@ -247,6 +264,8 @@ public function __construct( $this->reviewResourceModel = $reviewResourceModel; $this->collectionFactory = $collectionFactory; $this->sourceItemsBySku = $sourceItemsBySku; + $this->assignedStock = $assignedStock; + $this->salableQuantity = $salableQuantity; } /** @@ -357,13 +376,32 @@ public function showProductStructuredData() ); if ($sourceItemList = $this->sourceItemsBySku->execute($product->getSku())) { - $stockQty = 0; - foreach ($sourceItemList as $source) { - if ($source->getSourceCode() != 'default') { - $stockQty += (int) $source['quantity']; + $stockQty = 0; + $websiteCode = $this->_storeManager->getWebsite()->getCode(); + $assignedStockId = $this->assignedStock->execute($websiteCode); + + if ($product->getTypeId() === Configurable::TYPE_CODE) { + $typeInstance = $product->getTypeInstance(); + $childProductCollection = $typeInstance->getUsedProducts($product); + foreach ($childProductCollection as $childProduct) { + $qty = $this->salableQuantity->execute($childProduct->getSku()); + foreach ($qty as $value) { + if ($value['stock_id'] == $assignedStockId) { + $stockQty += isset($value['qty']) ? $value['qty'] : 0; + } + } + } + } else { + $qty = $this->salableQuantity->execute($product->getSku()); + foreach ($qty as $value) { + if ($value['stock_id'] == $assignedStockId) { + $stockQty += isset($value['qty']) ? $value['qty'] : 0; + } } + } - $stockItem = $stockQty; + + $stockItem = (int)$stockQty; } $priceValidUntil = $currentProduct->getSpecialToDate(); From 722f0a878fc53705cbae9608342950abaf42a849 Mon Sep 17 00:00:00 2001 From: shox Date: Mon, 24 Oct 2022 14:40:30 +0700 Subject: [PATCH 4/5] - Convert setup scripts to xml --- Setup/Patch/Data/RemoveAttribute.php | 112 +++++++++++++++++++++++++++ Setup/UpgradeData.php | 79 ------------------- 2 files changed, 112 insertions(+), 79 deletions(-) create mode 100644 Setup/Patch/Data/RemoveAttribute.php delete mode 100644 Setup/UpgradeData.php diff --git a/Setup/Patch/Data/RemoveAttribute.php b/Setup/Patch/Data/RemoveAttribute.php new file mode 100644 index 0000000..6db6227 --- /dev/null +++ b/Setup/Patch/Data/RemoveAttribute.php @@ -0,0 +1,112 @@ +moduleDataSetup = $moduleDataSetup; + $this->eavSetupFactory = $eavSetupFactory; + } + + /** + * Do Upgrade + * + * @return void + */ + public function apply() + { + $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); + $this->removeAttribute($eavSetup, Product::ENTITY, 'mp_meta_robots'); + $this->removeAttribute($eavSetup, Product::ENTITY, 'mp_seo_og_description'); + $this->removeAttribute($eavSetup, Category::ENTITY, 'mp_meta_robots'); + } + + /** + * @inheritdoc + */ + public function getAliases() + { + return []; + } + + /** + * @inheritdoc + */ + public static function getDependencies() + { + return []; + } + + /** + * @return string + */ + public static function getVersion() + { + return '2.0.1'; + } + + + /** + * Remove attribute + * + * @param $eavSetup + * @param $model + * @param $id + */ + private function removeAttribute($eavSetup, $model, $id) + { + if ($eavSetup->getAttributeId($model, $id)) { + $eavSetup->removeAttribute($model, $id); + } + } +} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php deleted file mode 100644 index 56d844c..0000000 --- a/Setup/UpgradeData.php +++ /dev/null @@ -1,79 +0,0 @@ -eavSetupFactory = $eavSetupFactory; - } - - /** - * @param ModuleDataSetupInterface $setup - * @param ModuleContextInterface $context - */ - public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) - { - $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); - if (version_compare($context->getVersion(), '2.0.1', '<')) { - $this->removeAttribute($eavSetup, Product::ENTITY, 'mp_meta_robots'); - $this->removeAttribute($eavSetup, Product::ENTITY, 'mp_seo_og_description'); - $this->removeAttribute($eavSetup, Category::ENTITY, 'mp_meta_robots'); - } - } - - /** - * Remove attribute - * - * @param $eavSetup - * @param $model - * @param $id - */ - public function removeAttribute($eavSetup, $model, $id) - { - if ($eavSetup->getAttributeId($model, $id)) { - $eavSetup->removeAttribute($model, $id); - } - } -} From 3123bf8f8dd2ddbddb98ce43246df98de9d0f4d1 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 4 Nov 2022 15:24:19 +0700 Subject: [PATCH 5/5] clean code --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4cafd2c..e621759 100644 --- a/composer.json +++ b/composer.json @@ -2,10 +2,10 @@ "name": "mageplaza/magento-2-seo-extension", "description": "Magento 2 SEO extension", "require": { - "mageplaza/module-core": "^1.4.12" + "mageplaza/module-core": "^1.5.0" }, "type": "magento2-module", - "version": "4.2.0", + "version": "4.2.1", "license": "proprietary", "keywords": [ "magento 2",