Skip to content

Commit

Permalink
Merge pull request #130 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
haitv282 authored Nov 4, 2022
2 parents b351ffe + 3123bf8 commit d8c54b1
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 114 deletions.
14 changes: 4 additions & 10 deletions Block/Adminhtml/SeoChecker/CheckForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +45,7 @@ class CheckForm extends Template
protected $_template = 'seocheck.phtml';

/**
* @var UrlBuilder
* @var Page
*/
protected $cmsUrl;

Expand Down Expand Up @@ -88,7 +88,7 @@ class CheckForm extends Template
*/
public function __construct(
Context $context,
UrlBuilder $cmsUrl,
Page $cmsUrl,
PageFactory $cmsPageFactory,
ProductFactory $productFactory,
ProductRepositoryInterface $productRepository,
Expand All @@ -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':
Expand All @@ -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 = '';
Expand Down
104 changes: 81 additions & 23 deletions Plugin/SeoRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +52,9 @@
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;
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite as AssignedStock;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku as SalableQuantity;

/**
* Class SeoRender
Expand Down Expand Up @@ -169,30 +173,48 @@ class SeoRender
*/
protected $collectionFactory;

/**
* @var SourceItems
*/
protected $sourceItemsBySku;

/**
* @var AssignedStock
*/
protected $assignedStock;

/**
* @var SalableQuantity
*/
protected $salableQuantity;

/**
* 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
* @param AssignedStock $assignedStock
* @param SalableQuantity $salableQuantity
*/
public function __construct(
PageConfig $pageConfig,
Expand All @@ -215,7 +237,10 @@ public function __construct(
ModuleManager $moduleManager,
RatingFactory $ratingFactory,
ReviewResourceModel $reviewResourceModel,
CollectionFactory $collectionFactory
CollectionFactory $collectionFactory,
SourceItems $sourceItemsBySku,
AssignedStock $assignedStock,
SalableQuantity $salableQuantity
) {
$this->pageConfig = $pageConfig;
$this->request = $request;
Expand All @@ -238,6 +263,9 @@ public function __construct(
$this->ratingFactory = $ratingFactory;
$this->reviewResourceModel = $reviewResourceModel;
$this->collectionFactory = $collectionFactory;
$this->sourceItemsBySku = $sourceItemsBySku;
$this->assignedStock = $assignedStock;
$this->salableQuantity = $salableQuantity;
}

/**
Expand Down Expand Up @@ -346,6 +374,36 @@ public function showProductStructuredData()
$product->getId(),
$product->getStore()->getWebsiteId()
);

if ($sourceItemList = $this->sourceItemsBySku->execute($product->getSku())) {
$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 = (int)$stockQty;
}

$priceValidUntil = $currentProduct->getSpecialToDate();
$modelAttribute = $this->helperData->getRichsnippetsConfig('model_value');
$modelValue = $product->getResource()
Expand All @@ -370,12 +428,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()
],
Expand Down
112 changes: 112 additions & 0 deletions Setup/Patch/Data/RemoveAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Seo
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license http://mageplaza.com/LICENSE.txt
*/

declare(strict_types=1);

namespace Mageplaza\Seo\Setup\Patch\Data;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* Patch is mechanism, that allows to do atomic upgrade data changes
* Class RemoveAttribute
* @package Mageplaza\Seo\Setup\Patch\Data
*/
class RemoveAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface $moduleDataSetup
*/
private $moduleDataSetup;

/**
* @var EavSetupFactory
*/
private $eavSetupFactory;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->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);
}
}
}
Loading

0 comments on commit d8c54b1

Please sign in to comment.