3

I need a query to fetch the following data by passing the product sku.

catalog_product_entity as sku, catalog_product_entity_varchar as productName, catalog_product_entity_text as description

following are the tables

SQL Query

  • In which page you need these details? – Jeeva Chezhiyan Nov 17 '17 at 10:21
  • jeeva I need it for custom module, I need a SQL query – Sam Nov 17 '17 at 10:24
5

You don't need to write any query for product collection in magento please use below code for get products data in .phtml or any custom module,

Using object method

$product_id = YOUR_PRODUCT_ID;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

$name = $product->getName();
$sku = $product->getSku();
$description = $product->getDescription();

Using Factory Method

<?php
namespace Test\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;  


  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader

    ) {


        $this->_productloader = $_productloader;
        parent::__construct($context);
    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }

}

Add following code in your .phtml file if you use Factory Method

$product=$this->getLoadProduct(20);
echo $product->getName();

In Magento 2.1

<?php
    namespace Test\Module\Block;
    use Magento\Catalog\Api\ProductRepositoryInterface;
    class Product extends \Magento\Framework\View\Element\Template
     {
          protected $productRepository; 
          protected $_storeManager; 

          public function __construct(
            \Magento\Framework\App\Action\Context $context,
            ProductRepositoryInterface $productRepository
          ) {
              parent::__construct($context);

              $this->productRepository = $productRepository;
          }
          public function getProduct()
          {

              $productId=1;
              return $product = $this->productRepository->getById($productId);
          }
      }
  • will I get more information details?? of the product? – Sam Nov 17 '17 at 10:44
  • Yes, you can get all details of product – Aasim Goriya Nov 17 '17 at 10:50
  • 1
    try \Zend_Debug::dump($product->getData()); for all product information – Aasim Goriya Nov 17 '17 at 10:51
  • I wanted for my Controller ,but how to run in block the code – Sam Nov 17 '17 at 10:54
  • what do you want to run in block code – Aasim Goriya Nov 17 '17 at 11:02
2

You don't need to write any query for product collection in magento. you can load product collection in external script as below :

<?php
use Magento\Framework\App\Bootstrap;

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');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
     echo 'Sku  =  '.$product->getSku().'<br><br>';
}  

?>
  • I want to fetch more details – Sam Nov 17 '17 at 11:10
  • you can fetch description & all other details like : $product->getDescription(); – Charvi Parikh Nov 17 '17 at 11:12
1

You don't need to write sql query please use model to get product collection find the code below,

 <?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
        ->load();
    foreach ($collection as $product){
         echo 'Name  =  '.$product->getName().'<br>';
         echo 'Name  =  '.$product->getSku().'<br>';
         echo 'Name  =  '.$product->getDescription().'<br>';

    }  
1

How to get product by id and SKU in Magento 2

$_helper = $this->helper('Magento\Catalog\Helper\Output'); 
$_product = $block->getProduct();
echo 'Product Id = '.$_product->getId().'<br>';
echo 'Product Name = '.$_product->getName().'<br>';
echo 'Product Sku = '.$_product->getSku().'<br>';
  • How to get product by id and sku in magento 2 – sherasiya bhavesh Feb 20 '18 at 10:15

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.