Configurable.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Model\Plugin;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProductType;
  9. use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection;
  10. class Configurable
  11. {
  12. /**
  13. * @var \Magento\Eav\Model\Config|\Magento\Swatches\Model\SwatchFactory
  14. */
  15. private $eavConfig;
  16. /**
  17. * @var \Magento\Swatches\Helper\Data
  18. */
  19. private $swatchHelper;
  20. /**
  21. * @param \Magento\Swatches\Model\SwatchFactory $eavConfig
  22. * @param \Magento\Swatches\Helper\Data $swatchHelper
  23. */
  24. public function __construct(
  25. \Magento\Eav\Model\Config $eavConfig,
  26. \Magento\Swatches\Helper\Data $swatchHelper
  27. ) {
  28. $this->eavConfig = $eavConfig;
  29. $this->swatchHelper = $swatchHelper;
  30. }
  31. /**
  32. * Add swatch attributes to Configurable Products Collection
  33. *
  34. * @param ConfigurableProductType $subject
  35. * @param Collection $result
  36. * @param ProductInterface $product
  37. * @return Collection
  38. */
  39. public function afterGetUsedProductCollection(
  40. ConfigurableProductType $subject,
  41. Collection $result,
  42. ProductInterface $product
  43. ) {
  44. $swatchAttributes = ['image'];
  45. foreach ($subject->getUsedProductAttributes($product) as $code => $attribute) {
  46. if ($attribute->getData('additional_data')
  47. && (
  48. $this->swatchHelper->isVisualSwatch($attribute) || $this->swatchHelper->isTextSwatch($attribute)
  49. )
  50. ) {
  51. $swatchAttributes[] = $code;
  52. }
  53. }
  54. $result->addAttributeToSelect($swatchAttributes);
  55. return $result;
  56. }
  57. }