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);
asked Sep 26, 2016 at 10:05
3
  • try with print_r($productCollection->getData()) Commented Sep 26, 2016 at 10:06
  • @Rakesh , i tried that Commented 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 Commented 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;
answered Apr 10, 2017 at 10:23
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());
answered Sep 26, 2016 at 11:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.