Skip to content

Commit 81d6abd

Browse files
author
Sergey Shvets
committed
MAGETWO-54648: [GitHub] No Swatch Input for Admin Scope. No Fallback to Admin Scope #5143 #5142
1 parent 71c58ad commit 81d6abd

File tree

6 files changed

+148
-15
lines changed

6 files changed

+148
-15
lines changed

app/code/Magento/Swatches/Model/Plugin/EavAttribute.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ protected function processTextualSwatch(Attribute $attribute)
277277
//option was deleted by button with basket
278278
continue;
279279
}
280+
$defaultSwatchValue = reset($storeValues);
280281
foreach ($storeValues as $storeId => $value) {
282+
if (!$value) {
283+
$value = $defaultSwatchValue;
284+
}
281285
$swatch = $this->loadSwatchIfExists($optionId, $storeId);
282286
$swatch->isDeleted($isOptionForDelete);
283287
$this->saveSwatchData(

app/code/Magento/Swatches/Setup/UpgradeData.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use Magento\Framework\Setup\ModuleDataSetupInterface;
1111
use Magento\Framework\Setup\ModuleContextInterface;
1212
use Magento\Catalog\Model\Product;
13+
use Magento\Store\Model\Store;
14+
use Magento\Swatches\Model\Swatch;
15+
use Zend_Db;
16+
use Zend_Db_Expr;
1317

1418
/**
1519
* Upgrade Data script
@@ -54,6 +58,53 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
5458
$eavSetup->addAttributeToGroup(Product::ENTITY, $attributeSetId, $groupId, 'swatch_image');
5559
}
5660

61+
if (version_compare($context->getVersion(), '2.0.2', '<')) {
62+
$this->updateAdminTextSwatchValues($setup);
63+
}
64+
5765
$setup->endSetup();
5866
}
67+
68+
private function updateAdminTextSwatchValues(ModuleDataSetupInterface $setup)
69+
{
70+
$storeData = $setup->getConnection()
71+
->select()
72+
->from($setup->getTable('store'))
73+
->where(Store::STORE_ID . "<> ? ", Store::DEFAULT_STORE_ID)
74+
->order("sort_order desc")
75+
->limit(1)
76+
->query(Zend_Db::FETCH_ASSOC)
77+
->fetch();
78+
79+
if (is_array($storeData)) {
80+
81+
/**
82+
* update eav_attribute_option_swatch as s
83+
* left join eav_attribute_option_swatch as ls on ls.option_id = s.option_id and ls.store_id = 1
84+
* set
85+
*
86+
* s.value = ls.value
87+
* where s.store_id = 0 and s.`type` = 0 and s.value = ""
88+
*/
89+
90+
/** @var \Magento\Framework\DB\Select $select */
91+
$select = $setup->getConnection()
92+
->select()
93+
->joinLeft(
94+
["ls" => $setup->getTable('eav_attribute_option_swatch')],
95+
new Zend_Db_Expr("ls.option_id = s.option_id AND ls.store_id = " . $storeData[Store::STORE_ID]),
96+
["value"]
97+
)
98+
->where("s.store_id = ? ", Store::DEFAULT_STORE_ID)
99+
->where("s.type = ? ", Swatch::SWATCH_TYPE_TEXTUAL)
100+
->where("s.value = ? or s.value is null", "");
101+
102+
$setup->getConnection()->query(
103+
$setup->getConnection()->updateFromSelect(
104+
$select,
105+
["s" => $setup->getTable('eav_attribute_option_swatch')]
106+
)
107+
);
108+
}
109+
}
59110
}

app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function testBeforeSaveWithDeletedOption()
262262
false
263263
)
264264
);
265-
$this->eavAttribute->beforeSave($this->attribute);
265+
$this->eavAttribute->beforeSave($this->attribute);
266266
}
267267

268268
public function testBeforeSaveNotSwatch()
@@ -378,6 +378,66 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue)
378378
$this->eavAttribute->afterAfterSave($this->attribute);
379379
}
380380

381+
public function testDefaultTextualSwatchAfterSave()
382+
{
383+
$this->abstractSource->expects($this->once())->method('getAllOptions')
384+
->willReturn($this->allOptions);
385+
386+
$this->swatch->expects($this->any())->method('getId')
387+
->willReturn(EavAttribute::DEFAULT_STORE_ID);
388+
$this->swatch->expects($this->any())->method('save');
389+
$this->swatch->expects($this->any())->method('isDeleted')
390+
->with(false);
391+
392+
$this->collection->expects($this->any())->method('addFieldToFilter')
393+
->willReturnSelf();
394+
$this->collection->expects($this->any())->method('getFirstItem')
395+
->willReturn($this->swatch);
396+
$this->collectionFactory->expects($this->any())->method('create')
397+
->willReturn($this->collection);
398+
399+
$this->attribute->expects($this->at(0))->method('getData')
400+
->willReturn($this->optionIds);
401+
$this->attribute->expects($this->at(1))->method('getSource')
402+
->willReturn($this->abstractSource);
403+
$this->attribute->expects($this->at(2))->method('getData')
404+
->with('default/0')
405+
->willReturn(null);
406+
407+
$this->attribute->expects($this->at(3))->method('getData')
408+
->with('swatch/value')
409+
->willReturn(
410+
[
411+
self::STORE_ID => [
412+
1 => "test",
413+
2 => false,
414+
3 => null,
415+
4 => "",
416+
]
417+
]
418+
);
419+
420+
$this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
421+
->with($this->attribute)
422+
->willReturn(true);
423+
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
424+
->with($this->attribute)
425+
->willReturn(false);
426+
$this->swatchHelper->expects($this->once())->method('isTextSwatch')
427+
->with($this->attribute)
428+
->willReturn(true);
429+
430+
$this->swatch->expects($this->any())->method('setData')
431+
->withConsecutive(
432+
['option_id', self::OPTION_ID],
433+
['store_id', 1],
434+
['type', Swatch::SWATCH_TYPE_TEXTUAL],
435+
['value', "test"]
436+
);
437+
438+
$this->eavAttribute->afterAfterSave($this->attribute);
439+
}
440+
381441
public function testAfterAfterSaveTextualSwatch()
382442
{
383443
$this->abstractSource->expects($this->once())->method('getAllOptions')

app/code/Magento/Swatches/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_Swatches" setup_version="2.0.1">
9+
<module name="Magento_Swatches" setup_version="2.0.2">
1010
<sequence>
1111
<module name="Magento_Catalog"/>
1212
<module name="Magento_ConfigurableProduct"/>

app/code/Magento/Swatches/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ Image,Image
3333
"Image Size","Image Size"
3434
"Example format: 200x300.","Example format: 200x300."
3535
"Image Position","Image Position"
36+
Description,Description

app/code/Magento/Swatches/view/adminhtml/templates/catalog/product/attribute/text.phtml

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
$stores = $block->getStoresSortedBySortOrder();
1212
?>
1313
<fieldset class="fieldset">
14-
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Manage Swatch (Values of Your Attribute)') ?></span></legend>
14+
<legend class="legend">
15+
<span><?php /* @escapeNotVerified */ echo __('Manage Swatch (Values of Your Attribute)') ?></span>
16+
</legend>
1517
<div id="swatch-text-options-panel">
1618
<table class="data-table clearfix" cellspacing="0">
1719
<thead>
1820
<tr id="swatch-text-options-table">
1921
<th class="col-draggable"></th>
2022
<th class="col-default"><span><?php /* @escapeNotVerified */ echo __('Is Default') ?></span></th>
2123
<?php foreach ($stores as $_store): ?>
22-
<?php if ($_store->getId() != \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?>
23-
<th class="col-swatch col-<%- data.id %>"><span><?php /* @escapeNotVerified */ echo __('Swatch'); ?></span></th>
24-
<?php endif; ?>
25-
<th<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> class="_required"<?php endif; ?>>
24+
<th class="col-swatch col-<%- data.id %>
25+
<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> _required<?php endif; ?>"
26+
colspan="2">
2627
<span><?php /* @escapeNotVerified */ echo $_store->getName() ?></span>
2728
</th>
2829
<?php endforeach; ?>
@@ -40,7 +41,8 @@ $stores = $block->getStoresSortedBySortOrder();
4041
<tr>
4142
<th colspan="<?php /* @escapeNotVerified */ echo $colTotal; ?>" class="col-actions-add">
4243
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()):?>
43-
<button id="add_new_swatch_text_option_button" title="<?php /* @escapeNotVerified */ echo __('Add Swatch'); ?>"
44+
<button id="add_new_swatch_text_option_button" title="
45+
<?php /* @escapeNotVerified */ echo __('Add Swatch'); ?>"
4446
type="button" class="action- scalable add">
4547
<span><?php /* @escapeNotVerified */ echo __('Add Swatch'); ?></span>
4648
</button>
@@ -55,21 +57,36 @@ $stores = $block->getStoresSortedBySortOrder();
5557
<tr>
5658
<td class="col-draggable">
5759
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()): ?>
58-
<div data-role="draggable-handle" class="draggable-handle" title="<?php /* @escapeNotVerified */ echo __('Sort Option'); ?>"></div>
60+
<div data-role="draggable-handle"
61+
class="draggable-handle"
62+
title="<?php /* @escapeNotVerified */ echo __('Sort Option'); ?>"></div>
5963
<?php endif; ?>
60-
<input data-role="order" type="hidden" name="optiontext[order][<%- data.id %>]" value="<%- data.sort_order %>" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()): ?> disabled="disabled"<?php endif; ?>/>
64+
<input data-role="order" type="hidden" name="optiontext[order][<%- data.id %>]"
65+
value="<%- data.sort_order %>"
66+
<?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()): ?> disabled="disabled"<?php endif; ?>
67+
/>
6168
</td>
6269
<td class="col-default">
63-
<input class="input-radio" type="<%- data.intype %>" name="defaulttext[]" value="<%- data.id %>" <%- data.checked %><?php if ($block->getReadOnly()):?>disabled="disabled"<?php endif;?>/>
70+
<input class="input-radio"
71+
type="<%- data.intype %>"
72+
name="defaulttext[]"
73+
value="<%- data.id %>" <%- data.checked %><?php if ($block->getReadOnly()):?>disabled="disabled"<?php endif;?>/>
6474
</td>
6575
<?php foreach ($stores as $_store): ?>
66-
<?php if ($_store->getId() != \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?>
6776
<td class="col-swatch col-<%- data.id %>">
68-
<input class="input-text swatch-text-field-<?php /* @escapeNotVerified */ echo $_store->getId() ?> <?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?>required-option<?php endif; ?>" name="swatchtext[value][<%- data.id %>][<?php /* @escapeNotVerified */ echo $_store->getId() ?>]" type="text" value="<%- data.swatch<?php /* @escapeNotVerified */ echo $_store->getId() ?> %>" />
77+
<input class="input-text
78+
swatch-text-field-<?php /* @escapeNotVerified */ echo $_store->getId() ?>
79+
<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> required-option<?php endif; ?>"
80+
name="swatchtext[value][<%- data.id %>][<?php /* @escapeNotVerified */ echo $_store->getId() ?>]"
81+
type="text" value="<%- data.swatch<?php /* @escapeNotVerified */ echo $_store->getId() ?> %>"
82+
placeholder="<?php echo __("Swatch"); ?>" />
6983
</td>
70-
<?php endif; ?>
7184
<td class="swatch-col-<%- data.id %>">
72-
<input name="optiontext[value][<%- data.id %>][<?php /* @escapeNotVerified */ echo $_store->getId() ?>]" value="<%- data.store<?php /* @escapeNotVerified */ echo $_store->getId() ?> %>" class="input-text<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> required-option<?php endif; ?>" type="text" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()):?> disabled="disabled"<?php endif;?>/>
85+
<input name="optiontext[value][<%- data.id %>][<?php /* @escapeNotVerified */ echo $_store->getId() ?>]"
86+
value="<%- data.store<?php /* @escapeNotVerified */ echo $_store->getId() ?> %>"
87+
class="input-text<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> required-option<?php endif; ?>"
88+
type="text" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()):?> disabled="disabled"<?php endif;?>
89+
placeholder="<?php echo __("Description"); ?>" />
7390
</td>
7491
<?php endforeach; ?>
7592
<td id="delete_button_swatch_container_<%- data.id %>" class="col-delete">

0 commit comments

Comments
 (0)