SuggestedAttributeList.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * List of suggested attributes
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\ConfigurableProduct\Model;
  9. class SuggestedAttributeList
  10. {
  11. /**
  12. * @var ConfigurableAttributeHandler
  13. */
  14. protected $configurableAttributeHandler;
  15. /**
  16. * Catalog resource helper
  17. *
  18. * @var \Magento\Catalog\Model\ResourceModel\Helper
  19. */
  20. protected $_resourceHelper;
  21. /**
  22. * @param ConfigurableAttributeHandler $configurableAttributeHandler
  23. * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
  24. */
  25. public function __construct(
  26. \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler $configurableAttributeHandler,
  27. \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
  28. ) {
  29. $this->configurableAttributeHandler = $configurableAttributeHandler;
  30. $this->_resourceHelper = $resourceHelper;
  31. }
  32. /**
  33. * Retrieve list of attributes with admin store label containing $labelPart
  34. *
  35. * @param string $labelPart
  36. * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
  37. */
  38. public function getSuggestedAttributes($labelPart)
  39. {
  40. $escapedLabelPart = $this->_resourceHelper->addLikeEscape($labelPart, ['position' => 'any']);
  41. /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
  42. $collection = $this->configurableAttributeHandler->getApplicableAttributes()->addFieldToFilter(
  43. 'frontend_label',
  44. ['like' => $escapedLabelPart]
  45. );
  46. $result = [];
  47. foreach ($collection->getItems() as $id => $attribute) {
  48. /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
  49. if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
  50. $result[$id] = [
  51. 'id' => $attribute->getId(),
  52. 'label' => $attribute->getFrontendLabel(),
  53. 'code' => $attribute->getAttributeCode(),
  54. 'options' => $attribute->getSource()->getAllOptions(false),
  55. ];
  56. }
  57. }
  58. return $result;
  59. }
  60. }