Skip to content

Commit

Permalink
Added store filtering in ajax store action and fix filter to use FIND…
Browse files Browse the repository at this point in the history
…_IN_SET instead NI
  • Loading branch information
vmalyk committed Mar 26, 2018
1 parent 35b36d7 commit d097127
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/Controller/Ajax/Stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Limesharp\Stockists\Model\ResourceModel\Stores\CollectionFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Responsible for loading page content.
Expand All @@ -41,14 +42,21 @@ class Stores extends \Magento\Framework\App\Action\Action
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @var StoreManagerInterface
*/
public $storeManager;

public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
CollectionFactory $collectionFactory
CollectionFactory $collectionFactory,
StoreManagerInterface $storeManager
) {
$this->collectionFactory = $collectionFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->storeManager = $storeManager;
parent::__construct($context);
}

Expand All @@ -59,11 +67,13 @@ public function __construct(
*/
public function execute()
{
$collection = $this->collectionFactory->create()->getData();
$collection = $this->collectionFactory->create()
->addStoreFilter($this->storeManager->getStore()->getId())
->getData();
$json = [];
foreach ($collection as $stockist) {
$json[] = $stockist;
}
return $this->resultJsonFactory->create()->setData($json);
}
}
}
8 changes: 7 additions & 1 deletion src/Model/ResourceModel/Stores/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ public function addStoreFilter($store, bool $withAdmin = true)
$store[] = Store::DEFAULT_STORE_ID;
}

$this->addFilter('store_id', ['in' => $store], 'public');
$filterParams = [];

foreach ($store as $storeId) {
$filterParams[]['finset'] = $storeId;
}

$this->addFilter('store_id', $filterParams, 'public');
}
return $this;
}
Expand Down

0 comments on commit d097127

Please sign in to comment.