Skip to content

Commit 9512674

Browse files
author
Sergey Shvets
committed
Merge remote-tracking branch 'origin/MAGETWO-54648' into MAGETWO-55814
2 parents 0a2385d + 1970b92 commit 9512674

File tree

6 files changed

+157
-24
lines changed

6 files changed

+157
-24
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
@@ -34,3 +34,4 @@ Image,Image
3434
"Example format: 200x300.","Example format: 200x300."
3535
"Image Position","Image Position"
3636
"The value of Admin must be unique.","The value of Admin must be unique."
37+
Description,Description

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010

1111
$stores = $block->getStoresSortedBySortOrder();
1212
?>
13-
<fieldset class="fieldset" >
14-
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Manage Swatch (Values of Your Attribute)') ?></span></legend>
13+
<fieldset class="fieldset">
14+
<legend class="legend">
15+
<span><?php echo $block->escapeHtml(__('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>
20-
<th class="col-default"><span><?php /* @escapeNotVerified */ echo __('Is Default') ?></span></th>
22+
<th class="col-default"><span><?php echo $block->escapeHtml(__('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; ?>>
26-
<span><?php /* @escapeNotVerified */ echo $_store->getName() ?></span>
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">
27+
<span><?php echo $block->escapeHtml($_store->getName()) ?></span>
2728
</th>
2829
<?php endforeach; ?>
2930
<?php $colTotal = count($stores) * 2 + 3; ?>
@@ -39,11 +40,12 @@ $stores = $block->getStoresSortedBySortOrder();
3940
</th>
4041
</tr>
4142
<tr>
42-
<th colspan="<?php /* @escapeNotVerified */ echo $colTotal; ?>" class="col-actions-add">
43+
<th colspan="<?php echo (int)$colTotal; ?>" class="col-actions-add">
4344
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()):?>
44-
<button id="add_new_swatch_text_option_button" title="<?php /* @escapeNotVerified */ echo __('Add Swatch'); ?>"
45+
<button id="add_new_swatch_text_option_button"
46+
title="<?php echo $block->escapeHtml(__('Add Swatch')); ?>"
4547
type="button" class="action- scalable add">
46-
<span><?php /* @escapeNotVerified */ echo __('Add Swatch'); ?></span>
48+
<span><?php echo $block->escapeHtml(__('Add Swatch')); ?></span>
4749
</button>
4850
<?php endif; ?>
4951
</th>
@@ -56,30 +58,45 @@ $stores = $block->getStoresSortedBySortOrder();
5658
<tr>
5759
<td class="col-draggable">
5860
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()): ?>
59-
<div data-role="draggable-handle" class="draggable-handle" title="<?php /* @escapeNotVerified */ echo __('Sort Option'); ?>"></div>
61+
<div data-role="draggable-handle"
62+
class="draggable-handle"
63+
title="<?php echo $block->escapeHtml(__('Sort Option')); ?>"></div>
6064
<?php endif; ?>
61-
<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; ?>/>
65+
<input data-role="order" type="hidden" name="optiontext[order][<%- data.id %>]"
66+
value="<%- data.sort_order %>"
67+
<?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()): ?> disabled="disabled"<?php endif; ?>
68+
/>
6269
</td>
6370
<td class="col-default">
64-
<input class="input-radio" type="<%- data.intype %>" name="defaulttext[]" value="<%- data.id %>" <%- data.checked %><?php if ($block->getReadOnly()):?>disabled="disabled"<?php endif;?>/>
71+
<input class="input-radio"
72+
type="<%- data.intype %>"
73+
name="defaulttext[]"
74+
value="<%- data.id %>" <%- data.checked %><?php if ($block->getReadOnly()):?>disabled="disabled"<?php endif;?>/>
6575
</td>
6676
<?php foreach ($stores as $_store): ?>
67-
<?php if ($_store->getId() != \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?>
77+
<?php $storeId = (int)$_store->getId(); ?>
6878
<td class="col-swatch col-<%- data.id %>">
69-
<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() ?> %>" />
79+
<input class="input-text
80+
swatch-text-field-<?php /* @noEscape */ echo $storeId; ?>
81+
<?php if ($storeId == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> required-option required-unique<?php endif; ?>"
82+
name="swatchtext[value][<%- data.id %>][<?php /* @noEscape */ echo $storeId; ?>]"
83+
type="text" value="<%- data.swatch<?php /* @noEscape */ echo $storeId; ?> %>"
84+
placeholder="<?php echo $block->escapeHtml(__("Swatch")); ?>"/>
7085
</td>
71-
<?php endif; ?>
7286
<td class="swatch-col-<%- data.id %>">
73-
<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 required-unique<?php endif; ?>" type="text" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()):?> disabled="disabled"<?php endif;?>/>
87+
<input name="optiontext[value][<%- data.id %>][<?php /* @noEscape */ echo $storeId; ?>]"
88+
value="<%- data.store<?php /* @noEscape */ echo $storeId; ?> %>"
89+
class="input-text<?php if ($storeId == \Magento\Store\Model\Store::DEFAULT_STORE_ID): ?> required-option<?php endif; ?>"
90+
type="text" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()):?> disabled="disabled"<?php endif;?>
91+
placeholder="<?php echo __("Description"); ?>" />
7492
</td>
7593
<?php endforeach; ?>
7694
<td id="delete_button_swatch_container_<%- data.id %>" class="col-delete">
7795
<input type="hidden" class="delete-flag" name="optiontext[delete][<%- data.id %>]" value="" />
7896
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()):?>
79-
<button title="<?php /* @escapeNotVerified */ echo __('Delete') ?>" type="button"
80-
class="action- scalable delete delete-option"
81-
>
82-
<span><?php /* @escapeNotVerified */ echo __('Delete') ?></span>
97+
<button title="<?php echo $block->escapeHtml(__('Delete')); ?>" type="button"
98+
class="action- scalable delete delete-option">
99+
<span><?php echo $block->escapeHtml(__('Delete')); ?></span>
83100
</button>
84101
<?php endif;?>
85102
</td>

0 commit comments

Comments
 (0)