0

I have the following code in my Block/Main.php:

<?php
namespace Foo\TireSearch\Block;
class Main extends \Magento\Framework\View\Element\Template
{
    protected $productCollectionFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    )
    {
        $this->productCollectionFactory = $productCollectionFactory;
        parent::__construct($context);
    }

    function _prepareLayout(){

        $collection = $this->productCollectionFactory->create();
        $product = $collection->load(21704);
        var_dump($product->getData());

        exit;
}

By my understanding, this should show the data for the product with id 21704, but I seem to be seeing all the products, along with the query at the beginning:

SELECT `e`.* FROM `catalog_product_entity` AS `e`array(11821) { [0]=> array(15) { ["entity_id"]=> string(5) "18710" ["attribute_set_id"]=> string(1) "4" ["type_id"]=> string(12) "configurable" ["sku"]=> string(9) "CONTI_DWS" ["has_options"]=> string(1) "0" ["required_options"]=> string(1) "0" ["created_at"]=> string(19) "2016-05-18 20:42:55" ["updated_at"]=> string(19) "2016-05-18 20:42:55" ["price"]=> string(8) "150.2900" ["tax_class_id"]=> string(1) "2" ["final_price"]=> string(8) "150.2900" ["minimal_price"]=> string(8) "150.2900" ["min_price"]=> string(8) "150.2900" ["max_price"]=> string(8) "150.2900" ["tier_price"]=> NULL } 
[1]=> array(15) { ["entity_id"]=> string(5) "18711" ["attribute_set_id"]=> string(1) "4" ["type_id"]=> string(12) "configurable" ["sku"]=> string(8) "DURATRAC" ["has_options"]=> string(1) "0" ["required_options"]=> string(1) "0" ["created_at"]=> string(19) "2016-05-18 20:42:55" ["updated_at"]=> string(19) "2016-05-18 20:42:55" ["price"]=> string(8) "185.9800" ["tax_class_id"]=> string(1) "2" ["final_price"]=> string(8) "185.9800" ["minimal_price"]=> string(8) "185.9800" ["min_price"]=> string(8) "185.9800" ["max_price"]=> string(8) "185.9800" ["tier_price"]=> NULL } ... 

What am I doing wrong? And If I wanted filter the products by some attribute stored in the database, how would I do that? For example, say there is information about the size (cm) of each of my products in the database, How can I filter the products by this attribute? Thanks

0

Check Below code for load product by id:

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

For add product ->addAttributeToFilter() should works like in M1 on an EAV collection and ->addFieldToFilter() on a Flat Collection.

1

Get product by id using object manager

$productId = "2"; //Product Id

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

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

echo $product->getName()."<br>"; 
echo $product->getPrice()."<br>";

Get Product filter by attribute Example Here i am taking filter by sku = Samsung Galaxy M30 , where Samsung Galaxy M30 is my sku.

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

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

$collection = $productCollection->create()
              ->addAttributeToSelect('*')
              ->addAttributeToFilter('sku', 'Samsung Galaxy M30')
              ->load();

echo "<pre>";
print_r($collection->getData());
0

Don't use objectManager directly in files as magento 2 ECGM2 coding standard

With objectManager

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

With Factory Method

protected $_productRepository;

public function __construct(
    ...
    \Magento\Catalog\Model\ProductRepository $productRepository,
    ...
) {
    ...
    $this->_productRepository = $productRepository;
    ...
}

public function getProductById($id)
{
    return $this->_productRepository->getById($id);
}
2

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.