Combine.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogWidget\Model\Rule\Condition;
  7. /**
  8. * Combination of product conditions
  9. */
  10. class Combine extends \Magento\Rule\Model\Condition\Combine
  11. {
  12. /**
  13. * @var \Magento\CatalogWidget\Model\Rule\Condition\ProductFactory
  14. */
  15. protected $productFactory;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected $elementName = 'parameters';
  20. /**
  21. * @var array
  22. */
  23. private $excludedAttributes;
  24. /**
  25. * @param \Magento\Rule\Model\Condition\Context $context
  26. * @param \Magento\CatalogWidget\Model\Rule\Condition\ProductFactory $conditionFactory
  27. * @param array $data
  28. * @param array $excludedAttributes
  29. */
  30. public function __construct(
  31. \Magento\Rule\Model\Condition\Context $context,
  32. \Magento\CatalogWidget\Model\Rule\Condition\ProductFactory $conditionFactory,
  33. array $data = [],
  34. array $excludedAttributes = []
  35. ) {
  36. $this->productFactory = $conditionFactory;
  37. parent::__construct($context, $data);
  38. $this->setType(\Magento\CatalogWidget\Model\Rule\Condition\Combine::class);
  39. $this->excludedAttributes = $excludedAttributes;
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function getNewChildSelectOptions()
  45. {
  46. $productAttributes = $this->productFactory->create()->loadAttributeOptions()->getAttributeOption();
  47. $attributes = [];
  48. foreach ($productAttributes as $code => $label) {
  49. if (!in_array($code, $this->excludedAttributes)) {
  50. $attributes[] = [
  51. 'value' => Product::class . '|' . $code,
  52. 'label' => $label,
  53. ];
  54. }
  55. }
  56. $conditions = parent::getNewChildSelectOptions();
  57. $conditions = array_merge_recursive(
  58. $conditions,
  59. [
  60. [
  61. 'value' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
  62. 'label' => __('Conditions Combination'),
  63. ],
  64. ['label' => __('Product Attribute'), 'value' => $attributes]
  65. ]
  66. );
  67. return $conditions;
  68. }
  69. /**
  70. * Collect validated attributes for Product Collection
  71. *
  72. * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
  73. * @return $this
  74. */
  75. public function collectValidatedAttributes($productCollection)
  76. {
  77. foreach ($this->getConditions() as $condition) {
  78. $condition->collectValidatedAttributes($productCollection);
  79. }
  80. return $this;
  81. }
  82. }