Skip to content

Commit b25a34f

Browse files
committed
WIP: Fix for issue 12584 - save scoped bundle prices
1 parent cac512f commit b25a34f

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

app/code/Magento/Bundle/Model/LinkManagement.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ protected function mapProductLinkToSelectionModel(
209209
if ($productLink->getIsDefault() !== null) {
210210
$selectionModel->setIsDefault($productLink->getIsDefault());
211211
}
212+
if ($productLink->getWebsiteId() !== null) {
213+
$selectionModel->setWebsiteId($productLink->getWebsiteId());
214+
}
212215

213216
return $selectionModel;
214217
}

app/code/Magento/Bundle/Model/Option/SaveAction.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Exception\CouldNotSaveException;
1515
use Magento\Bundle\Model\Product\Type;
1616
use Magento\Bundle\Api\ProductLinkManagementInterface;
17+
use Magento\Store\Model\StoreManagerInterface;
1718

1819
/**
1920
* Encapsulates logic for saving a bundle option, including coalescing the parent product's data.
@@ -40,17 +41,24 @@ class SaveAction
4041
*/
4142
private $linkManagement;
4243

44+
/**
45+
* @var StoreManagerInterface
46+
*/
47+
private $storeManager;
48+
4349
/**
4450
* @param Option $optionResource
4551
* @param MetadataPool $metadataPool
4652
* @param Type $type
4753
* @param ProductLinkManagementInterface $linkManagement
54+
* @param StoreManagerInterface $storeManager
4855
*/
4956
public function __construct(
5057
Option $optionResource,
5158
MetadataPool $metadataPool,
5259
Type $type,
53-
ProductLinkManagementInterface $linkManagement
60+
ProductLinkManagementInterface $linkManagement,
61+
StoreManagerInterface $storeManager
5462
) {
5563
$this->optionResource = $optionResource;
5664
$this->metadataPool = $metadataPool;
@@ -135,6 +143,7 @@ private function updateOptionSelection(ProductInterface $product, OptionInterfac
135143
if (is_array($option->getProductLinks())) {
136144
$productLinks = $option->getProductLinks();
137145
foreach ($productLinks as $productLink) {
146+
$productLink->setWebsiteId($this->storeManager->getStore($product->getStoreId())->getWebsiteId());
138147
if (!$productLink->getId() && !$productLink->getSelectionId()) {
139148
$linksToAdd[] = $productLink;
140149
} else {

app/code/Magento/Bundle/Model/Selection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* @method \Magento\Bundle\Model\Selection setPosition(int $value)
2121
* @method int getIsDefault()
2222
* @method \Magento\Bundle\Model\Selection setIsDefault(int $value)
23+
* @method int getWebsiteId()
24+
* @method \Magento\Bundle\Model\Selection setWebsiteId(int $value)
2325
* @method int getSelectionPriceType()
2426
* @method \Magento\Bundle\Model\Selection setSelectionPriceType(int $value)
2527
* @method float getSelectionPriceValue()
@@ -71,6 +73,15 @@ protected function _construct()
7173
parent::_construct();
7274
}
7375

76+
public function beforeSave()
77+
{
78+
if (!$this->_catalogData->isPriceGlobal() && $this->getWebsiteId()) {
79+
$this->setData('website_selection_price_value', $this->getSelectionPriceValue());
80+
$this->setSelectionPriceValue($this->getOrigData('selection_price_value'));
81+
}
82+
parent::beforeSave();
83+
}
84+
7485
/**
7586
* Processing object before save data
7687
*
@@ -79,6 +90,9 @@ protected function _construct()
7990
public function afterSave()
8091
{
8192
if (!$this->_catalogData->isPriceGlobal() && $this->getWebsiteId()) {
93+
if (null !== $this->getData('website_selection_price_value')) {
94+
$this->setSelectionPriceValue($this->getData('website_selection_price_value'));
95+
}
8296
$this->getResource()->saveSelectionPrice($this);
8397

8498
if (!$this->getDefaultPriceScope()) {

app/code/Magento/Bundle/Test/Unit/Model/LinkManagementTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,15 @@ public function testAddChildProductAlreadyExistsInOption()
468468
*/
469469
public function testAddChildCouldNotSave()
470470
{
471+
$websiteId = 100;
471472
$productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
472-
->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
473+
->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getWebsiteId'])
473474
->disableOriginalConstructor()
474475
->getMockForAbstractClass();
475476
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
476477
$productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
477478
$productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
479+
$productLink->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
478480

479481
$this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
480482
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
@@ -541,13 +543,15 @@ function () {
541543

542544
public function testAddChild()
543545
{
546+
$websiteId = 100;
544547
$productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
545-
->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
548+
->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getWebsiteId'])
546549
->disableOriginalConstructor()
547550
->getMockForAbstractClass();
548551
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
549552
$productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
550553
$productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
554+
$productLink->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
551555

552556
$this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
553557
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
@@ -620,9 +624,10 @@ public function testSaveChild()
620624
$linkProductId = 45;
621625
$parentProductId = 32;
622626
$bundleProductSku = 'bundleProductSku';
627+
$websiteId = 100;
623628

624629
$productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
625-
->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
630+
->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getWebsiteId'])
626631
->disableOriginalConstructor()
627632
->getMockForAbstractClass();
628633
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
@@ -636,6 +641,7 @@ public function testSaveChild()
636641
->will($this->returnValue($canChangeQuantity));
637642
$productLink->expects($this->any())->method('getIsDefault')->will($this->returnValue($isDefault));
638643
$productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue($optionId));
644+
$productLink->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
639645

640646
$this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
641647
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
@@ -677,7 +683,8 @@ public function testSaveChild()
677683
'setSelectionPriceType',
678684
'setSelectionPriceValue',
679685
'setSelectionCanChangeQty',
680-
'setIsDefault'
686+
'setIsDefault',
687+
'setWebsiteId'
681688
]);
682689
$selection->expects($this->once())->method('save');
683690
$selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
@@ -691,6 +698,7 @@ public function testSaveChild()
691698
$selection->expects($this->once())->method('setSelectionPriceValue')->with($price);
692699
$selection->expects($this->once())->method('setSelectionCanChangeQty')->with($canChangeQuantity);
693700
$selection->expects($this->once())->method('setIsDefault')->with($isDefault);
701+
$selection->expects($this->once())->method('setWebsiteId')->with($websiteId);
694702

695703
$this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
696704
$this->assertTrue($this->model->saveChild($bundleProductSku, $productLink));
@@ -704,14 +712,16 @@ public function testSaveChildFailedToSave()
704712
$id = 12;
705713
$linkProductId = 45;
706714
$parentProductId = 32;
715+
$websiteId = 200;
707716

708717
$productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
709-
->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
718+
->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getWebsiteId'])
710719
->disableOriginalConstructor()
711720
->getMockForAbstractClass();
712721
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
713722
$productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
714723
$productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
724+
$productLink->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
715725
$bundleProductSku = 'bundleProductSku';
716726

717727
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
@@ -751,13 +761,15 @@ public function testSaveChildFailedToSave()
751761
'setSelectionPriceType',
752762
'setSelectionPriceValue',
753763
'setSelectionCanChangeQty',
754-
'setIsDefault'
764+
'setIsDefault',
765+
'setWebsiteId'
755766
]);
756767
$mockException = $this->createMock(\Exception::class);
757768
$selection->expects($this->once())->method('save')->will($this->throwException($mockException));
758769
$selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
759770
$selection->expects($this->any())->method('getId')->will($this->returnValue($id));
760771
$selection->expects($this->once())->method('setProductId')->with($linkProductId);
772+
$selection->expects($this->once())->method('setWebsiteId')->with($websiteId);
761773

762774
$this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
763775
$this->model->saveChild($bundleProductSku, $productLink);

app/code/Magento/Bundle/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
<event name="magento_bundle_model_selection_save_after">
1919
<observer name="legacy_model_save" instance="Magento\Framework\EntityManager\Observer\AfterEntitySave" />
2020
</event>
21+
<event name="magento_bundle_model_selection_save_before">
22+
<observer name="legacy_model_save" instance="Magento\Framework\EntityManager\Observer\BeforeEntitySave" />
23+
</event>
2124
</config>

0 commit comments

Comments
 (0)