Last active
April 29, 2025 10:51
-
-
Save sandipklevu/3d41259c9fff583785bbed989aca4e99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
//php klevu_loadAttribute.php <product_id> <store_id> >> var/log/KlevuScript_LoadAttribute.log | |
ini_set('display_errors', 1); | |
ini_set('memory_limit', -1); | |
use Klevu\Search\Api\Service\Catalog\Product\GetRatingStarsInterface; | |
use Klevu\Search\Api\Service\Catalog\Product\GetReviewCountInterface; | |
use Klevu\Search\Helper\Config; | |
use Klevu\Search\Model\Product\LoadAttributeInterface; | |
use Klevu\Search\Model\Product\ProductInterface; | |
use Klevu\Search\Provider\Catalog\Product\Review\MagentoRatingDataProvider; | |
use Magento\Framework\App\Bootstrap; | |
use Magento\Framework\App\ObjectManager; | |
use Klevu\Search\Helper\Stock as KlevuStockHelper; | |
use Klevu\Search\Helper\Compat as KlevuCompatHelper; | |
use Klevu\Search\Service\Catalog\Product\Stock as KlevuStockService; | |
use Magento\CatalogInventory\Api\StockRegistryInterface; | |
use Magento\Framework\App\ResourceConnection; | |
use Magento\Framework\DataObject; | |
use Magento\Framework\Exception\NoSuchEntityException; | |
use Magento\Store\Api\Data\StoreInterface; | |
require __DIR__ . '/app/bootstrap.php'; | |
$params = $_SERVER; | |
$bootstrap = Bootstrap::create(BP, $params); | |
$objectManagerInstance = $bootstrap->getObjectManager(); | |
$state = $objectManagerInstance->get('Magento\Framework\App\State'); | |
try { | |
$state->setAreaCode('frontend'); | |
} catch (\Magento\Framework\Exception\LocalizedException $e) { | |
// Area code already set | |
} catch (\Exception $e) { | |
echo 'Error setting area code: ' . $e->getMessage() . PHP_EOL; | |
exit(1); | |
} | |
$storeManager = $objectManagerInstance->get('\Magento\Store\Model\StoreManagerInterface'); | |
$productService = $objectManagerInstance->get(ProductInterface::class); | |
$searchHelperConfig = $objectManagerInstance->get(Config::class); | |
$stockHelper = $objectManagerInstance->get(KlevuStockHelper::class); | |
$compatHelper = $objectManagerInstance->get(KlevuCompatHelper::class); | |
$stockServiceClass = $objectManagerInstance->get(KlevuStockService::class); | |
$frameworkModelResource = $objectManagerInstance->get(ResourceConnection::class); | |
$stockRegistryInterface = $objectManagerInstance->get(StockRegistryInterface::class); | |
$loadAttributeProduct = $objectManagerInstance->get(LoadAttributeInterface::class); | |
$ratingDataProvider = $objectManagerInstance->get(MagentoRatingDataProvider::class); | |
$getRatingStars = $objectManagerInstance->get(GetRatingStarsInterface::class); | |
$getReviewCount = $objectManagerInstance->get(GetReviewCountInterface::class); | |
//Change product id here | |
$product_id = $argv[1] | |
?: ''; | |
$parent_id = $argv[3] ?? 0; | |
//Change store id if needed | |
$storeID = $argv[2] ?? 1; | |
$storeObject = ''; | |
try { | |
$storeObject = $storeManager->getStore($storeID); | |
} catch (NoSuchEntityException $e) { | |
echo 'Store not found:' . $e->getMessage() . PHP_EOL; | |
exit; | |
} | |
try { | |
$storeManager->setCurrentStore($storeObject); | |
} catch (\Exception $e) { | |
echo 'Store not able to set to current store:' . $e->getMessage() . PHP_EOL; | |
exit; | |
} | |
$websiteId = $storeObject->getWebsiteId(); | |
echo ' *********************** LoadAttribute Start ***********************'; | |
echo 'Store ID: ' . $storeID . PHP_EOL; | |
echo 'Website ID: ' . $websiteId . PHP_EOL; | |
echo 'Product ID: ' . print_r($product_id, true) . PHP_EOL;; | |
echo 'Parent ID: ' . print_r($parent_id, true) . PHP_EOL;; | |
try { | |
$collection = $objectManagerInstance->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); | |
$collection->addAttributeToSelect($loadAttributeProduct->getUsedMagentoAttributes()); | |
$collection->addAttributeToSelect('price_type'); | |
$collection->addIdFilter([$product_id,$parent_id]); | |
$collection->setStore($storeManager->getStore()); | |
$collection->addStoreFilter(); | |
$collection->setFlag('has_stock_status_filter', true); | |
$collection->load(); | |
$collection->addCategoryIds(); | |
echo 'Query : ' . $collection->getSelect() . PHP_EOL; | |
$item = $collection->getItemById($product_id); | |
if (!$item) { | |
echo 'Product Data NOT available for given Product id : ' . $product_id . PHP_EOL; | |
exit(1); | |
} | |
if ($searchHelperConfig->isCollectionMethodEnabled()) { | |
echo 'Start: Data loading using collection method.' . PHP_EOL;; | |
$parent = $collection->getItemById($parent_id) ?? null; | |
echo 'End: Data loading using collection method.' . PHP_EOL; | |
} else { | |
echo 'Start: Data loading using object method.' . PHP_EOL; | |
$item = \Magento\Framework\App\ObjectManager::getInstance() | |
->create('\Magento\Catalog\Model\Product') | |
->load($product_id); | |
$item->setCustomerGroupId(\Magento\Customer\Model\Group::NOT_LOGGED_IN_ID); | |
$parent = ($parent_id != 0) | |
? \Magento\Framework\App\ObjectManager::getInstance() | |
->create('\Magento\Catalog\Model\Product') | |
->load($parent_id) | |
->setCustomerGroupId(\Magento\Customer\Model\Group::NOT_LOGGED_IN_ID) | |
: null; | |
echo 'End : Data loading using object method.' . PHP_EOL;; | |
} | |
echo 'Product Data is available for given Product id : ' . $product_id . PHP_EOL; | |
echo 'Entity ID : ' . $item->getData('entity_id') . PHP_EOL;; | |
echo 'getProductType : ' . $productService->getProductType($parent, $item) . PHP_EOL; | |
echo 'isCustomOptionsAvailable : ' . $productService->isCustomOptionsAvailable($parent, $item) . PHP_EOL; | |
echo 'getCategory : ' . print_r($productService->getCategory($parent, $item), true) . PHP_EOL; | |
echo 'getListCategory : ' . print_r($productService->getListCategory($parent, $item), true) . PHP_EOL; | |
echo 'getAllCategoryId : ' . print_r($productService->getAllCategoryId($parent, $item), true) . PHP_EOL; | |
echo 'getAllCategoryPaths : ' . print_r($productService->getAllCategoryPaths($parent, $item), true) . PHP_EOL;; | |
echo | |
'getProductType : ' . print_r($productService->getProductType($parent, $item), true) | |
. PHP_EOL; | |
echo | |
'getPriceData: ' . | |
print_r($productService->getPriceData($parent, $item, $productService, $storeObject), true) | |
. PHP_EOL; | |
echo | |
'getSalePriceData: ' . | |
print_r($productService->getSalePriceData($parent, $item, $productService, $storeObject), true) | |
. PHP_EOL; | |
echo | |
'getStartPriceData: ' . | |
print_r($productService->getStartPriceData($parent, $item, $productService, $storeObject), true) | |
. PHP_EOL; | |
echo | |
'getToPriceData: ' . | |
print_r($productService->getToPriceData($parent, $item, $productService, $storeObject), true) | |
. PHP_EOL; | |
echo | |
'getGroupPricesData: ' . | |
print_r($productService->getGroupPricesData($item), true) | |
. PHP_EOL; | |
if ($parent) { | |
echo 'getRatingData from Parent: ' . | |
print_r( | |
$ratingDataProvider->getData($parent_id, $storeID), | |
true | |
) . PHP_EOL; | |
echo 'getRatingStars from Parent: ' . | |
print_r( | |
$getRatingStars->execute([$parent]), | |
true | |
) . PHP_EOL; | |
echo 'getReviewCount from Parent: ' . | |
print_r( | |
$getReviewCount->execute([$parent]), | |
true | |
) . PHP_EOL; | |
} else { | |
echo 'getRatingData: ' . | |
print_r( | |
$ratingDataProvider->getData($product_id, $storeID), | |
true | |
) . PHP_EOL; | |
echo 'getRatingStars: ' . | |
print_r( | |
$getRatingStars->execute([$item]), | |
true | |
) . PHP_EOL; | |
echo 'getReviewCount: ' . | |
print_r( | |
$getReviewCount->execute([$item]), | |
true | |
) . PHP_EOL; | |
} | |
$query = $compatHelper->getProductUrlRewriteSelect( | |
[$product_id,$parent_id], | |
$storeID | |
); | |
$connection = $frameworkModelResource->getConnection("core_read"); | |
/** @var \Magento\Framework\Db\Select $stmt */ | |
$stmt = $connection->query($query); | |
$urlRewriteData = []; | |
while ($row = $stmt->fetch()) { | |
if (!isset($urlRewriteData[$row['entity_id']])) { | |
$urlRewriteData[$row['entity_id']] = $row['request_path']; | |
} | |
} | |
echo 'urlRewriteData: ' . print_r($urlRewriteData, true); | |
$currency = $productService->getCurrency(); | |
echo | |
'Currency: ' . | |
print_r($currency, true) | |
. PHP_EOL;; | |
echo | |
'getOtherPrices: ' . | |
print_r($productService->getOtherPrices($item, $currency), true) | |
. PHP_EOL; | |
$product = [ | |
'product_id' => $item->getData('entity_id'), | |
'image' => '', | |
]; | |
echo | |
'getImage : ' . $productService->getImage('image', ['image'], $parent, $item, $product, $storeObject) | |
. PHP_EOL; | |
echo | |
'inStock through stock helper: ' . print_r($stockHelper->getKlevuStockStatus($parent, $item), true) | |
. PHP_EOL; | |
$isInStock = $stockServiceClass->isInStock($item, $parent, $websiteId) | |
? 'YES' | |
: 'NO'; | |
echo | |
'isInStock through Klevu Service: ' . $isInStock | |
. PHP_EOL; | |
$stockStatusInterface = $stockRegistryInterface->getStockStatus( | |
$item->getId(), | |
$item->getStore()->getWebsiteId() | |
); | |
$stockStatus = $stockStatusInterface->getStockStatus() | |
? 'YES' | |
: 'No'; | |
echo | |
'getStockStatus through Magento: ' . print_r($stockStatus, true) | |
. PHP_EOL; | |
echo | |
'item value getStockStatus: ' . print_r($stockStatusInterface->getStockStatus(), true) | |
. PHP_EOL; | |
echo | |
'item label getStockStatus through Magento: ' . print_r($stockStatus, true) | |
. PHP_EOL; | |
if ($parent) { | |
$stockStatusRegistry = $stockRegistryInterface->getStockStatus( | |
$parent->getId(), | |
$parent->getStore()->getWebsiteId() | |
); | |
$parentInStock = $stockStatusRegistry->getStockStatus(); | |
echo | |
'parent value getStockStatus: ' . print_r($parentInStock, true) | |
. PHP_EOL; | |
} | |
$otherAttributeToIndex = $searchHelperConfig->getOtherAttributesToIndex($storeObject); | |
echo 'otherAttributeToIndex: ' . PHP_EOL . print_r($otherAttributeToIndex, true) . PHP_EOL;; | |
$productDataOtherAttributesToIndex = getOtherAttributes( | |
$storeObject, | |
$otherAttributeToIndex, | |
$item, | |
$parent | |
); | |
echo | |
'productDataToIndex: ' . print_r($productDataOtherAttributesToIndex, true) | |
. PHP_EOL; | |
} catch (NoSuchEntityException $e) { | |
echo 'Exception Thrown' . $e->getMessage() . PHP_EOL;; | |
} | |
function getOtherAttributes($store, $attributes, $item, $parent) | |
{ | |
$data = []; | |
foreach ($attributes as $attribute) { | |
if ($item && $item->getData($attribute)) { | |
$data[$attribute] = getAttributeData($store, $attribute, $item->getData($attribute)); | |
continue; | |
} | |
if ($parent && $parent->getData($attribute)) { | |
$data[$attribute] = getAttributeData($store, $attribute, $parent->getData($attribute)); | |
} | |
} | |
return $data; | |
} | |
function getAttributeData($store, $code, $value = null) | |
{ | |
echo '*************** START ***********************' . PHP_EOL;; | |
echo 'Code: ' . $code . PHP_EOL; | |
$dataObject = ObjectManager::getInstance()->get(DataObject::class); | |
if (empty($value)) { | |
echo 'Value found empty: ' . $value . ' for ' . $code . PHP_EOL; | |
return null; | |
} | |
$currentStoreID = $store->getId(); | |
if ((!$attributeData = $dataObject->getData('attribute_data')) | |
|| ($currentStoreID !== $dataObject->getData('attributeStoreID')) | |
) { | |
$dataObject->setData('attributeStoreID', $store->getId()); | |
$attributeData = getAttributeDataForStore($store); | |
echo print_r($attributeData, true); | |
$dataObject->setData('attribute_data', $attributeData); | |
} | |
// make sure the attribute exists | |
if (isset($attributeData[$code])) { | |
// was $value passed a parameter? | |
if (null !== $value) { | |
echo 'Ok, Value Not null: ' . print_r($value, true) . ' for ' . $code . PHP_EOL;; | |
// If not values are set on attribute_data for the attribute, return just the value passed. | |
// (attributes like: name, description, etc.) | |
if (empty($attributeData[$code]['values'])) { | |
echo 'Value empty attributeData Code Values: ' . print_r($value, true) . ' for ' . $code . PHP_EOL;; | |
return $value; | |
} | |
// break up our value into an array by a comma, this is for catching multiple select attributes. | |
if (is_array($value)) { | |
$values = $value; | |
} else { | |
$values = explode(",", $value); | |
} | |
// loop over our array of attribute values | |
foreach ($values as $key => $valueOption) { | |
// if there is a value on the attribute_data, use that value | |
// (it will be the label for a dropdown select attribute) | |
if (isset($attributeData[$code]['values'][$valueOption])) { | |
$values[$key] = $attributeData[$code]['values'][$valueOption]; | |
} else { // If no label was found, log an error and unset the value. | |
echo 'Attribute: ' . $code . ' option label was not found, option ID provided: ' | |
. $valueOption . PHP_EOL;; | |
unset($values[$key]); | |
} | |
} | |
// If there was only one value in the array, return the first (select menu, single option), | |
// or if there was more, return them all (multi-select). | |
if (is_array($values) && count($values) === 1) { | |
$valuesAll = array_values($values); | |
$attributeData[$code]['values'] = array_shift($valuesAll); | |
} else { | |
$attributeData[$code]['values'] = $values; | |
} | |
} | |
echo 'Ok, values after if null check: ' . $value . ' for ' . $code . PHP_EOL;; | |
return $attributeData[$code]; | |
} | |
echo '******************** END ******************' . PHP_EOL;; | |
$result['label'] = $code; | |
$result['values'] = $value; | |
return $result; | |
} | |
function getAttributeDataForStore(StoreInterface $store) | |
{ | |
$productAttributeCollection = ObjectManager::getInstance() | |
->get(\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class); | |
$loadAttributeProduct = ObjectManager::getInstance()->get(LoadAttributeInterface::class); | |
$attributeData = []; | |
$attributeCollection = $productAttributeCollection->addFieldToFilter( | |
'attribute_code', | |
['in' => $loadAttributeProduct->getUsedMagentoAttributes()] | |
); | |
foreach ($attributeCollection as $attr) { | |
$attributeData[$attr->getAttributeCode()] = [ | |
'label' => $attr->getStoreLabel($store->getId()), | |
'values' => [], // compatibility with php 7.1.x versions | |
]; | |
if (!$attr->usesSource()) { | |
continue; | |
} | |
$attr->setStoreId($store->getId()); | |
$source = $attr->getSource(); | |
$options = $source->getAllOptions(false); | |
$data = []; | |
foreach ($options as $option) { | |
if (is_array($option['value'])) { | |
foreach ($option['value'] as $sub_option) { | |
if (!empty($sub_option)) { | |
$data[$sub_option['value']] = $sub_option['label']; | |
} | |
} | |
} else { | |
$data[$option['value']] = $option['label']; | |
} | |
} | |
$attributeData[$attr->getAttributeCode()]['values'] = $data; | |
} | |
return $attributeData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where
product_id
is a required field which isentity_id
of productstore_id
is a optional field default to1
parent_id
is a required only in the case configurable product id and you have passedproduct_id
as associatedproduct_id