3
    <?php
namespace Queueapp\Queueapp\Controller\Query;
// Mage::app();

class Products extends \Magento\Framework\App\Action\Action {
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;
    /**      * @param \Magento\Framework\App\Action\Context $context      */
    public function __construct(\Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory)     {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Blog Index, shows a list of recent blog posts.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
      //How do I access the products/categories information here and send it to my frontend as json
      $resultPage = $this->resultPageFactory->create();
      $resultPage->getConfig()->getTitle()->prepend(__('Products'));
      echo json_encode("me");
      return $resultPage;

    }
}
3

try it like this:

<?php
namespace Queueapp\Queueapp\Controller\Query;


class Products extends \Magento\Framework\App\Action\Action 
{
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;
    protected $productCollectionFactory;
    protected $categoryCollectionFactory;
    /**      * @param \Magento\Framework\App\Action\Context $context      */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,

    ) {

        $this->resultPageFactory = $resultPageFactory;
        $this->productCollectionFactory = $productCollectionFactory;
        $this->categoryCollectionFactory = $categoryCollectionFactory;
        parent::__construct($context);
    }

    /**
     * Blog Index, shows a list of recent blog posts.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
      //How do I access the products/categories information here and send it to my frontend as json
      $resultPage = $this->resultPageFactory->create();
      $resultPage->getConfig()->getTitle()->prepend(__('Products'));
      $categories = $this->getCategories();
      $products = $this->getProducts();
      //do what you need with $products and $categories.
      return $resultPage;

    }
    public function getProducts()
    {
         $products = $productCollectionFactory->create();
         //now add filters as you need similar to Magento 1
         $products->addAttributeToSelect('*');
         $products->addAttributeToFilter(....);
         //loop through the products and build your array
         $values = [];
         foreach ($products as $product) {
             $values[] = ['name' => $product->getName(), ...];
         }
         return json_encode($values);
    }
    public function getCategories()
    {
         $categories = $categoryCollectionFactory->create();
         //now add filters as you need similar to Magento 1
         $categories->addAttributeToSelect('*');
         $categories->addAttributeToFilter(....);
         //loop through the categories and build your array
         $values = [];
         foreach ($categories as $category) {
             $values[] = ['name' => $category->getName(), ...];
         }
         return json_encode($values);
    }
}
  • Hi @marius I'm quite new to magento. kindly help me complete the code above. I want all products and all information about products. Also I'm guessing instead of return $resultPage; I should do return $products instead. In order to return json to my frontend – acquayefrank Jan 25 '16 at 10:55
1

Try this:

<?php
namespace Queueapp\Queueapp\Controller\Query;

use Magento\Framework\App\State;
use Magento\Framework\App\ObjectManager\ConfigLoader;

class Products extends \Magento\Framework\App\Action\Action {
    /** @var  \Magento\Framework\View\Result\Page */
    /**
     * Queue collection factory
     *
     * @var \Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory
     */
    protected $_productFactory;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    protected $_productRepository;
    protected $product;
    protected $state;
    protected $configLoader;
    protected $objectManager;


    /**
     * Construct
     *
     */
    public function __construct(
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Catalog\Api\ProductRepositoryInterface $_productRepository,
            \Magento\Catalog\Api\Data\ProductInterface $product,

            \Magento\Framework\Registry $registry,                 
            State $state,
            \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_productFactory = $productFactory;
        $this->_productRepository =$_productRepository;
        $this->product  =   $product;
        $this->registry = $registry;       
        $this->state = $state;
        $this->objectManager = $objectManager;      
    }

    /**
     * Blog Index, shows a list of recent blog posts.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
      $products = $this->_productFactory->create()->getCollection()

      $resultPage->getConfig()->getTitle()->prepend(__('Products'));
      echo json_encode("me");
      return $resultPage;

    }
}
  • don't use object manager in classes that are not factories. – Marius Jan 25 '16 at 9:43

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.