Skip to content

Instantly share code, notes, and snippets.

@sandipklevu
Last active November 1, 2023 15:12
Show Gist options
  • Save sandipklevu/3d41259c9fff583785bbed989aca4e99 to your computer and use it in GitHub Desktop.
Save sandipklevu/3d41259c9fff583785bbed989aca4e99 to your computer and use it in GitHub Desktop.
<?php
ini_set('display_errors', 1);
ini_set('memory_limit', -1);
use Klevu\Search\Helper\Config;
use Klevu\Search\Model\Product\LoadAttributeInterface;
use Klevu\Search\Model\Product\ProductInterface;
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\ObjectManager;
use Klevu\Search\Helper\Stock as KlevuStockHelper;
use Klevu\Search\Service\Catalog\Product\Stock as KlevuStockService;
use Magento\CatalogInventory\Api\StockRegistryInterface;
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);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$om = ObjectManager::getInstance();
$storeManager = ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
$productService = ObjectManager::getInstance()->get(ProductInterface::class);
$searchHelperConfig = ObjectManager::getInstance()->get(Config::class);
$stockHelper = ObjectManager::getInstance()->get(KlevuStockHelper::class);
$stockServiceClass = ObjectManager::getInstance()->get(KlevuStockService::class);
$stockRegistryInterface = ObjectManager::getInstance()->get(StockRegistryInterface::class);
$loadAttributeProduct = ObjectManager::getInstance()->get(LoadAttributeInterface::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 = ObjectManager::getInstance()
->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addAttributeToSelect($loadAttributeProduct->getUsedMagentoAttributes());
$collection->addAttributeToSelect('price_type');
$collection->addIdFilter($product_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);
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;
$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;
}
@sandipklevu
Copy link
Author

sandipklevu commented Nov 1, 2023

php klevu_loadAttribute_3_4_2.php <product_id> <store_id>  >> var/log/Klevu_Script.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment