4

I am trying to get the custom attribute vales from products collection as like below code but i didn't get attribute values.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
             $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
                                                ->addAttributeToSelect('cb_city');
             $collection = $productCollection->create()
                                             ->addAttributeToSelect('cb_city')
                                             ->load();
             print_r($collection);
3
  • try with print_r($productCollection->getData()) Sep 26, 2016 at 10:06
  • @Rakesh , i tried that
    – Ramesh
    Sep 26, 2016 at 10:07
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); $collection = $productCollection->create() ->addAttributeToSelect('sku'); print_r($collection->getData()); i tried like this but i didn't get collection based on attribute
    – Ramesh
    Sep 26, 2016 at 10:13

2 Answers 2

4

You can use following code for return collection base on particular attribute, here $cb_city is a value based on which you want to filter-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $prodCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $prodCollection->create()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('cb_city',$cb_city)
        ->load();
        return $collection;
1

I got the collection with below script

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
             $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
             $collection = $productCollection->create()
                                             ->addAttributeToSelect('*')
                                             ->load();
             echo "<pre>";
             print_r($collection->getData());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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