0

I want to get product details from url key.

Is there a way to get product details from url_key, I am new to Magento and could not find any such way, please help I tried below code but it is not working properly.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeIds = array_keys($storeManager->getStores());
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');


while($row = mysqli_fetch_assoc($result)) {
    $updateAttributes = array();

    echo $row["slug"];
    echo "--->";
    $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollectionFactory->create();
    $collection->addAttributeToFilter('url_key',"URL_KEY_HERE");
    $collection->addAttributeToSelect('*');
    foreach ($collection as $product) 
    {
        echo "id is ->".$product->getId();
        echo "<br>";
    }
}
  • Remove while($row = mysqli_fetch_assoc($result)) { – Mohamed El Mrabet Mar 10 at 13:04
  • Remove all unused code its correct what you do with collection – Mohamed El Mrabet Mar 10 at 13:04
  • Please inform what error occurs in my answer. I will solve and Update my post. – Msquare yesterday
1
$urlKey = "strive-shoulder-pack";   // add your url key which you want 

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollectionFactory->addAttributeToFilter('url_key',$urlKey);
$productCollectionFactory->addAttributeToSelect('*');
foreach ($productCollectionFactory as $product) 
{
    echo "id is ->".$product->getId();
    echo "<br>";
    print_r($product->getData());
    echo "<br>";
}

enter image description here

Note: Only Enable Product url can check so please enter proper enable product url key

|improve this answer|||||
  • let me inform it's helps you or not. – Msquare Mar 27 at 8:29
1
For getting product details from url key using ObjectManager:
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$prodColl = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $prodColl->addAttributeToSelect('*')
                       ->addAttributeToFilter('url_key','joust-duffle-bag');
echo '<pre>';
print_r($collection->getData());
echo '</pre>';
|improve this answer|||||
0

firstly don't use objectManager you can inject the used class in the construct:

public function __construct(\Magento\Catalog\Model\ProductFactory $productFactory)
{
    $this->productFactory = $productFactory;
}

And after that in your class use loadByAttribute function

$product = $this->productFactory->create();
$product->loadByAttribute('url_key', $urlKey);

I want my answer to help you

With ObjectManager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create();
$product->loadByAttribute('url_key', $urlKey);
|improve this answer|||||
  • please help me with objectmanager way as i need to do this once for adding few data to products – user1799722 Mar 10 at 15:15
  • See my response @user1799722 – Mohamed El Mrabet Mar 12 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.