Created
November 23, 2022 05:35
-
-
Save sandipklevu/af52fd2ed7b4a9c80d4b6fcb07e468fe to your computer and use it in GitHub Desktop.
To update product directly to Klevu Indexer
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 | |
/** | |
* From terminal, run as follows | |
* php magento-directUpdateRecordsToKlevu.php <store_id> | |
* store_id will be 1 by default, store_id is an optional parameter | |
* Direct script to update the product to Klevu indexer. | |
* Use this script wisely as this script will not check whether given product needs to add or update or delete etc. | |
* | |
* NOTE: Recommend to use only for troubleshooting. | |
*/ | |
use Magento\Framework\App\Bootstrap; | |
try { | |
include './app/bootstrap.php'; | |
} catch (Exception $e) { | |
echo 'Autoload error: ' . $e->getMessage(); | |
exit(1); | |
} | |
try { | |
echo "-------- Data Index Process started. ---------" . PHP_EOL; | |
echo 'Script class init started. ' . PHP_EOL; | |
$bootstrap = Bootstrap::create(BP, $_SERVER); | |
$objectManager = $bootstrap->getObjectManager(); | |
$appState = $objectManager->get('\Magento\Framework\App\State'); | |
$appState->setAreaCode('frontend'); | |
$registry = $objectManager->get('\Magento\Framework\Registry'); | |
$registry->register('isSecureArea', true); | |
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); | |
$klevuMagentoProductActions = $objectManager->get('\Klevu\Search\Model\Product\MagentoProductActionsInterface'); | |
$klevuProductActions = $objectManager->get('\Klevu\Search\Model\Product\KlevuProductActionsInterface'); | |
echo 'Script class init completed. ' . PHP_EOL; | |
try { | |
//Replace or load storeId as per store | |
$storeId = $argv[1] ?? 1; | |
//Replace product id and parent id | |
/** | |
* Note: Recommend to pass associated product_id and parent_id only, | |
* script won't check whether product_id and parent_id are associated during the execution. | |
* product_id would be simple/downloadable/virtual entity_id etc. | |
* parent_id would be configurable product's entity_id, this would be zero if product_id is simple/downloadable/virtual | |
*/ | |
$productParentIds = [ | |
0 => ['product_id' => 12, 'parent_id' => 11], | |
1 => ['product_id' => 13, 'parent_id' => 11], | |
2 => ['product_id' => 1, 'parent_id' => 0] //Given parent_id = 0, assuming product_id does not have any parents. | |
]; | |
$storeToSetupSession = $storeManager->getStore($storeId); | |
if (!$storeToSetupSession) { | |
echo 'Given store id is not valid' . $storeId . PHP_EOL; | |
exit(1); | |
} | |
//start session is required for authentication | |
$sessionStatus = $klevuProductActions->setupSession($storeToSetupSession); | |
echo $sessionStatus ? 'Session started.' : 'Session Not started.'; | |
if (!$sessionStatus) { | |
echo 'Product Sync is disabled, store is not configured or the session API call fails. Refer. Klevu_Search.<storecode>.log' . PHP_EOL; | |
exit(1); | |
} | |
//pass $productParentIds array with ids | |
$updateProductResult = $klevuMagentoProductActions->updateProducts($productParentIds); | |
echo PHP_EOL; | |
echo 'Response from updateProducts: '; | |
print_r($updateProductResult); | |
echo PHP_EOL; | |
if ($updateProductResult) { | |
echo 'Product has been indexed successfully to Klevu. Refer. Klevu_Search.<storecode>.log for what data has been sent.' . PHP_EOL; | |
} | |
} catch (Exception $e) { | |
echo 'Exception thrown: ' . $e->getMessage() . PHP_EOL; | |
} | |
echo " -------- Data Index Process completed. ---------" . PHP_EOL; | |
} catch (Exception $e) { | |
print_r($e->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment