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
Ramesh
9861 gold badge14 silver badges42 bronze badges
-
try with print_r($productCollection->getData())Rakesh Jesadiya– Rakesh Jesadiya2016年09月26日 10:06:51 +00:00Commented Sep 26, 2016 at 10:06
-
@Rakesh , i tried thatRamesh– Ramesh2016年09月26日 10:07:40 +00:00Commented 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 attributeRamesh– Ramesh2016年09月26日 10:13:01 +00:00Commented Sep 26, 2016 at 10:13
2 Answers 2
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;
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
Ramesh
9861 gold badge14 silver badges42 bronze badges
default