skip to Main Content

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 = MagentoFrameworkAppObjectManager::getInstance();

//Load product by product id
$product = $objectManager->create('MagentoCatalogModelProduct')->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?

2

Answers


  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

    Login or Signup to reply.
  2. 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 MagentoFrameworkAppBootstrap;
    
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    
    $objectManager = $bootstrap->getObjectManager();
    
    $state = $objectManager->get('MagentoFrameworkAppState');
    $state->setAreaCode('frontend');    
    
    $productId = 1;
    $product = $objectManager->create('MagentoCatalogModelProduct')->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 MagentoCatalogModelProductFactory;
    
    class Product extends MagentoFrameworkViewElementTemplate
    {    
        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 MagentoCatalogApiProductRepositoryInterface;
    
    class Product extends MagentoFrameworkViewElementTemplate
    {
        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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search