1

I am applying these fillers:

$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('sku');

but every time getting full record as comes form:

$collection->addAttributeToSelect('*');

[{"entity_id":"101","attribute_set_id":"9","type_id":"simple","sku":"MH04-XS-White","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"102","attribute_set_id":"9","type_id":"simple","sku":"MH04-XS-Yellow","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"103","attribute_set_id":"9","type_id":"simple","sku":"MH04-S-Green","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"104","attribute_set_id":"9","type_id":"simple","sku":"MH04-S-White","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"105","attribute_set_id":"9","type_id":"simple","sku":"MH04-S-Yellow","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"106","attribute_set_id":"9","type_id":"simple","sku":"MH04-M-Green","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"},{"entity_id":"107","attribute_set_id":"9","type_id":"simple","sku":"MH04-M-White","has_options":"0","required_options":"0","created_at":"2018-10-08 07:35:50","updated_at":"2018-10-08 07:35:50"}]

I am getting collection using class as:

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory

and

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('name')->addAttributeToSelect('sku');
    $page = $this->getRequest()->getParam('page');
    $collection->setPageSize(50)->setCurPage($page);
    return json_encode($collection->getData());

}
  • It's working? @AjwadTaqvi – Rohan Hapani Oct 22 '18 at 10:28
  • how do you get the collection? – Sebastian Oct 22 '18 at 11:00
  • I updated Question. – Ajwad Syed Oct 22 '18 at 11:08
  • Check my updated answer. – Rohan Hapani Oct 22 '18 at 11:41
0

You should use an array:

$collection->addAttributeToSelect(['name','sku']);

OR

$collection->addAttributeToSelect('name')->addAttributeToSelect('sku');

OR

protected $_productCollection;

public function __construct(
...........
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
...........
) {
    ...........
    $this->_productCollection = $productCollection;
    ...........
}

and

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->getCollection()->addAttributeToSelect(['name','sku']);
    $page = $this->getRequest()->getParam('page');
    $collection->setPageSize(50)->setCurPage($page);
    return json_encode($collection->getData());
}
  • No change occured, if apply these filters as in updated question, data appears complete still. where as i need only Name, SKU, Qty. I deleted generated folder and cache cleared. – Ajwad Syed Oct 22 '18 at 11:11
  • Yes. I already mention in my answer that. $collection->addAttributeToSelect('name')->addAttributeToSelect('sku'); – Rohan Hapani Oct 22 '18 at 11:29
  • [] getting empty array with updated solution :d – Ajwad Syed Oct 22 '18 at 11:53
  • Check updated answer now. – Rohan Hapani Oct 22 '18 at 12:05
  • undefined method, getCollection() Fatal error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::getCollection() Removed generation, cache cleared, still same – Ajwad Syed Oct 22 '18 at 12:17
0

I solved this problem by creating my custom collection as:

public function getProductCollection()//In block
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $page = $this->getRequest()->getParam('page');
    $collection->setPageSize(50)->setCurPage($page);
    return $collection;
}


$cusCollection = array(); //Custom Collection for retriving required data in controller 
    $i=0;
    $productCollection = $this->product->getProductCollection();
    foreach ($productCollection as $product) {
        $cusCollection['items'][$i]['itemID'] = $product->getId();
        $id = $product->getId();
        $_productStock = $this->order->getStockItem($id);
        $productQty = $_productStock->getQty();
        $cusCollection['items'][$i]['name '] = $product->getName().' '.$productQty; 
        $cusCollection['items'][$i]['sku '] = $product->getSku();
        $i++;
}
       echo json_encode($cusCollection);
  • It's product collection or ordered product collection? – Rohan Hapani Oct 23 '18 at 11:56
  • It is product collection. – Ajwad Syed Oct 23 '18 at 11:57
  • Please added here which class you inject in construct for $this->order. – Rohan Hapani Oct 23 '18 at 11:58
  • By this I am calling my block class function in controller. I hope it is understandable. – Ajwad Syed Oct 23 '18 at 12:00
  • It's not understandable. That's why I told to upload whole answer. So, it will helpful for other user also. – Rohan Hapani Oct 23 '18 at 12:01

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.