|
5 | 5 | */
|
6 | 6 | namespace Magento\ConfigurableProduct\Setup;
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Model\Product; |
8 | 9 | use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
|
9 | 10 | use Magento\Framework\Setup\UpgradeDataInterface;
|
10 | 11 | use Magento\Framework\Setup\ModuleContextInterface;
|
@@ -41,25 +42,59 @@ public function __construct(EavSetupFactory $eavSetupFactory)
|
41 | 42 | public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
|
42 | 43 | {
|
43 | 44 | $setup->startSetup();
|
| 45 | + /** @var EavSetup $eavSetup */ |
| 46 | + $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); |
| 47 | + |
44 | 48 | if (version_compare($context->getVersion(), '2.2.0') < 0) {
|
45 |
| - /** @var EavSetup $eavSetup */ |
46 |
| - $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); |
47 |
| - $relatedProductTypes = explode( |
48 |
| - ',', |
49 |
| - $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'tier_price', 'apply_to') |
50 |
| - ); |
| 49 | + $relatedProductTypes = $this->getRelatedProductTypes('tier_price', $eavSetup); |
51 | 50 | $key = array_search(Configurable::TYPE_CODE, $relatedProductTypes);
|
52 | 51 | if ($key !== false) {
|
53 | 52 | unset($relatedProductTypes[$key]);
|
54 |
| - $eavSetup->updateAttribute( |
55 |
| - \Magento\Catalog\Model\Product::ENTITY, |
56 |
| - 'tier_price', |
57 |
| - 'apply_to', |
58 |
| - implode(',', $relatedProductTypes) |
59 |
| - ); |
| 53 | + $this->updateRelatedProductTypes('tier_price', $relatedProductTypes, $eavSetup); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (version_compare($context->getVersion(), '2.2.1') < 0) { |
| 58 | + $relatedProductTypes = $this->getRelatedProductTypes('manufacturer', $eavSetup); |
| 59 | + if (!in_array(Configurable::TYPE_CODE, $relatedProductTypes)) { |
| 60 | + $relatedProductTypes[] = Configurable::TYPE_CODE; |
| 61 | + $this->updateRelatedProductTypes('manufacturer', $relatedProductTypes, $eavSetup); |
60 | 62 | }
|
61 | 63 | }
|
62 | 64 |
|
63 | 65 | $setup->endSetup();
|
64 | 66 | }
|
| 67 | + |
| 68 | + /** |
| 69 | + * Get related product types for attribute. |
| 70 | + * |
| 71 | + * @param string $attributeId |
| 72 | + * @param EavSetup $eavSetup |
| 73 | + * @return array |
| 74 | + */ |
| 75 | + private function getRelatedProductTypes(string $attributeId, EavSetup $eavSetup) |
| 76 | + { |
| 77 | + return explode( |
| 78 | + ',', |
| 79 | + $eavSetup->getAttribute(Product::ENTITY, $attributeId, 'apply_to') |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Update related product types for attribute. |
| 85 | + * |
| 86 | + * @param string $attributeId |
| 87 | + * @param array $relatedProductTypes |
| 88 | + * @param EavSetup $eavSetup |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + private function updateRelatedProductTypes(string $attributeId, array $relatedProductTypes, EavSetup $eavSetup) |
| 92 | + { |
| 93 | + $eavSetup->updateAttribute( |
| 94 | + Product::ENTITY, |
| 95 | + $attributeId, |
| 96 | + 'apply_to', |
| 97 | + implode(',', $relatedProductTypes) |
| 98 | + ); |
| 99 | + } |
65 | 100 | }
|
0 commit comments