Created
October 16, 2023 10:29
-
-
Save sandipklevu/c04e625674f856c5753b557815f8fefe to your computer and use it in GitHub Desktop.
Magento 2 mview remove subscription if not being used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Magento\Framework\App\Bootstrap; | |
require __DIR__ . '/../app/bootstrap.php'; | |
$params = $_SERVER; | |
$bootstrap = Bootstrap::create(BP, $params); | |
$objectManager = $bootstrap->getObjectManager(); | |
$url = \Magento\Framework\App\ObjectManager::getInstance(); | |
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface'); | |
try { | |
$state = $objectManager->get('\Magento\Framework\App\State'); | |
$indexerFactory = $objectManager->get('\Magento\Framework\Indexer\IndexerInterfaceFactory'); | |
$subscriptionFactory = $objectManager->get('\Magento\Framework\Mview\View\SubscriptionFactory'); | |
$collection = $objectManager->get('\Magento\Framework\Mview\View\CollectionInterface'); | |
$viewCollection = $collection->getItems(); | |
$state->setAreaCode('frontend'); | |
/** @var \Magento\Framework\Indexer\IndexerInterface $indexer */ | |
$indexer = $indexerFactory->create()->load('klevu_product_sync'); | |
$subscriptionInstance = $subscriptionFactory->create( | |
[ | |
'view' => $indexer->getView(), | |
'tableName' => 'catalog_product_index_price', | |
'columnName' => 'entity_id', | |
] | |
); | |
$subscriptionInstance->remove(); | |
echo 'catalog_product_index_price Mview removed;' . PHP_EOL; | |
$subscriptionInstance = $subscriptionFactory->create( | |
[ | |
'view' => $indexer->getView(), | |
'tableName' => 'cataloginventory_stock_status', | |
'columnName' => 'product_id', | |
] | |
); | |
$subscriptionInstance->remove(); | |
echo 'cataloginventory_stock_status mview removed;' . PHP_EOL; | |
foreach ($viewCollection as $key => $view) { | |
echo $view->getId() . '<=>' . print_r($view->getState()->getStatus(), true) . PHP_EOL; | |
} | |
} catch (\Magento\Framework\Exception $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment