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?
like
query, what you are searching by issku 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.