0

I tried to get all products sku, that started at $params['sku'] My controller:

<?php

namespace Aty\TestTask\Controller\Index;

use Magento\Framework\App\Action\Context;

class GetProductAjax extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Catalog\Model\Product
     */
    private $product;

    public function __construct(
        Context $context,
        \Magento\Catalog\Model\Product $product
    ) {
        $this->product = $product;
        parent::__construct($context);
    }

    public function execute()
    {
        $params = $this->getRequest()->getParams();

        if (!empty($params['sku'])) {
            $collection = $this->product->getCollection();
            $skus = $collection->addFieldToFilter('sku', array(
                'like' => $params['sku'].' %'
                ))
                ->getItems();
            return $skus;
        }
        return false;
    }
}

But it return null. Where is my fault?

1
  • First you have a space in your like query, what you are searching by is sku like "abcd %" so if you do not have sku with a space then nothing will be found. Second, you can inspect actual query that was executed by doing $query = $collection->load()->getSelect()->__toString();, this is very helpful during development.
    – Zefiryn
    Oct 17, 2018 at 21:44

1 Answer 1

0

try to use below code-

protected $_productCollectionFactory;

public function __construct(
    Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
    $this->_productCollectionFactory = $productCollectionFactory; 
    parent::__construct($context);
}

public function execute()
{
    $params = $this->getRequest()->getParams();

    if (!empty($params['sku'])) {
        $collection =  $this->_productCollectionFactory->create();
        $collection = $collection->addFieldToFilter('sku', array(
            'like' => $params['sku'].' %'
            ));
        // if you want skus only
        $skus = array();
        foreach($collection as $product)
        {
          $skus[] = $product->getSku(); 
        }
        return $skus;
    }
    return false;
}
4
  • It's ok. but it get a product. May be u know how. I can get just array of sku?Can u explain, why I need to create it, pls?
    – Pasha
    Oct 17, 2018 at 19:37
  • @Pasha, please check my updated answer please. Oct 17, 2018 at 19:44
  • Ty, so mush. May be u can recommend some article, that explain about collection, and why I must create productCollection. And where I can know more about collection method like a addFieldToFilter.
    – Pasha
    Oct 17, 2018 at 19:47
  • to know how to use addFieldToFilter function you can check this link Oct 17, 2018 at 19:55

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.