Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a additional attributes parameters into autocomplete product c… #302

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,40 @@ class DataProvider implements DataProviderInterface
*/
private $productCollection;

/**
* Additional product attributes required.
*
* @var array
*/
private $additionalAttributes;

/**
* Constructor.
*
* @param ItemFactory $itemFactory Suggest item factory.
* @param QueryFactory $queryFactory Search query factory.
* @param TermDataProvider $termDataProvider Search terms suggester.
* @param ProductCollection $productCollection Product collection.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param string $type Autocomplete provider type.
* @param ItemFactory $itemFactory Suggest item factory.
* @param QueryFactory $queryFactory Search query factory.
* @param TermDataProvider $termDataProvider Search terms suggester.
* @param ProductCollection $productCollection Product collection.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param string $type Autocomplete provider type.
* @param array $additionalAttributes Additional product attributes required.
*/
public function __construct(
ItemFactory $itemFactory,
QueryFactory $queryFactory,
TermDataProvider $termDataProvider,
ProductCollection $productCollection,
ConfigurationHelper $configurationHelper,
$type = self::AUTOCOMPLETE_TYPE
$type = self::AUTOCOMPLETE_TYPE,
array $additionalAttributes = []
) {
$this->itemFactory = $itemFactory;
$this->queryFactory = $queryFactory;
$this->termDataProvider = $termDataProvider;
$this->productCollection = $productCollection;
$this->configurationHelper = $configurationHelper;
$this->type = $type;
$this->additionalAttributes = $additionalAttributes;

$this->prepareProductCollection();
}
Expand All @@ -112,7 +122,11 @@ public function getItems()
$result = [];

foreach ($this->productCollection as $product) {
$result[] = $this->itemFactory->create(['product' => $product, 'type' => $this->getType()]);
$result[] = $this->itemFactory->create([
'product' => $product,
'type' => $this->getType(),
'additional_attributes' => $this->additionalAttributes,
]);
}

return $result;
Expand All @@ -134,6 +148,10 @@ private function prepareProductCollection()
->setVisibility([Visibility::VISIBILITY_IN_SEARCH, Visibility::VISIBILITY_BOTH])
->addPriceData();

if ($this->additionalAttributes) {
$this->productCollection->addAttributeToSelect($this->additionalAttributes);
}

if (!$this->configurationHelper->isShowOutOfStock()) {
$this->productCollection->addIsInStockFilter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(ObjectManagerInterface $objectManager, ImageHelper $
public function create(array $data)
{
$data = $this->addProductData($data);
unset($data['product']);
unset($data['product'], $data['additional_attributes']);

return parent::create($data);
}
Expand All @@ -92,6 +92,10 @@ private function addProductData($data)
'url' => $product->getProductUrl(),
'price' => $this->renderProductPrice($product, \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE),
];
$additionalAttributes = $data['additional_attributes'];
foreach ($additionalAttributes as $additionalAttributeKey => $additionalAttribute) {
$productData[$additionalAttributeKey] = $product->getData($additionalAttribute);
}

$data = array_merge($data, $productData);

Expand Down