0

I have a site running Magento 2.2.1. I need to create a very simple PHP page that will look up a given product. I want to look up the product based on the SKU and just print the price and product URL out.

I have NO idea how to even start this. I have tried using this to test loading a product with ID = 1

//Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

//Load product by product id
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(1);

but all that does is throw an exeception that ObjectManager is not found. So I tried including the /app/bootstrap.php file beforehand, and that throws an error that the ObjectManager isn't initialized.

Can anyone provide me with a simple example that I can drop into the root of my site that will allow me to look up a single product by sku? Or point me in the direction of some useful documentation?

Phil
  • 4,029
  • 9
  • 62
  • 107

2 Answers2

1

The solution to load a product programmatically in simple PHP file by using ObjectManager, but this solution is not recommended by Magento 2.

<?php

include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');    

$productId = 1;
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

echo $product->getName();

?>

Recommended Solution (Magento 2)

In Magento 2, the recommended way of loading product is by using ProductRepository and ProductFactory in a proper custom module instead of simple PHP file. Well, by using below (recommended) code, you can load product in your custom block.

ProductFactory Solution

<?php
namespace [Vendor_Name]\[Module_Name]\Block;

use Magento\Catalog\Model\ProductFactory;

class Product extends \Magento\Framework\View\Element\Template
{    
    protected $_productloader;

    public function __construct(
        ProductFactory $_productloader    
    ) {  
        $this->_productloader = $_productloader;
    }

    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }    
}

In Magento 2.1

ProductRepository Solution

namespace [Vendor_Name]\[Module_Name]\Block;

use Magento\Catalog\Api\ProductRepositoryInterface;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $_productRepository;        

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->_productRepository = $productRepository;
    }

    public function getProduct($id)
    {
        return $product = $this->productRepository->getById($id);
    }
}

and, your .phtml file should look like this:

$productId = 1;
$product = $this->getLoadProduct($productId);

echo $product->getName();

I hope, you already know how to create a custom module in Magento 2 or if you want then just read this blog post How to create a basic module in Magento 2

Adeel
  • 2,901
  • 7
  • 24
  • 34
-1

You Cant just Load a Page By simple PHP file in magento Here is the procedure 1)create a layout file in you theme 2)register it inside layout.xml 3)add phtml to your layout file 4)add you code(in your question ) in that phtml file

the 2nd way is quite complecated create module and in module controller render your code

Maverik
  • 32
  • 3
  • here the link https://magento.stackexchange.com/questions/174048/magento2-add-a-new-page-layout – Maverik Jul 13 '18 at 07:17