Skip to content

Commit 4f25bdd

Browse files
author
Tang, Yu(ytang1)
committed
Merge pull request #279 from magento-fearless-kiwis/develop
[FearlessKiwis] Support for Different Product Types in Catalog Product Data Object
2 parents a7cb0c2 + 428d6f7 commit 4f25bdd

File tree

183 files changed

+7891
-2563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+7891
-2563
lines changed

app/code/Magento/Backend/Model/Session/Quote.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function getQuote()
150150
$this->_quote->setStoreId($this->getStoreId());
151151
}
152152

153-
if ($this->getCustomerId()) {
153+
if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
154154
$customer = $this->customerRepository->getById($this->getCustomerId());
155155
$this->_quote->assignCustomer($customer);
156156
}

app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,42 @@ protected function setUp()
225225
*
226226
* @return void
227227
*/
228-
public function testGetQuote()
228+
public function testGetQuoteWithoutQuoteId()
229229
{
230-
$storeId = 10;
231230
$quoteId = 22;
232-
$customerGroupId = 77;
231+
$storeId = 10;
233232
$customerId = 66;
233+
$customerGroupId = 77;
234+
235+
$this->quote->expects($this->any())
236+
->method('getQuoteId')
237+
->will($this->returnValue(null));
238+
$this->quote->expects($this->any())
239+
->method('setQuoteId')
240+
->with($quoteId);
241+
$this->quote->expects($this->any())
242+
->method('getStoreId')
243+
->will($this->returnValue($storeId));
244+
$this->quote->expects($this->any())
245+
->method('getCustomerId')
246+
->will($this->returnValue($customerId));
247+
248+
$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
249+
->getMock();
250+
$defaultGroup->expects($this->any())
251+
->method('getId')
252+
->will($this->returnValue($customerGroupId));
253+
$this->groupManagementMock->expects($this->any())
254+
->method('getDefaultGroup')
255+
->will($this->returnValue($defaultGroup));
256+
257+
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
258+
->disableOriginalConstructor()
259+
->getMock();
260+
$this->customerRepositoryMock->expects($this->once())
261+
->method('getById')
262+
->with($customerId)
263+
->willReturn($dataCustomerMock);
234264

235265
$quoteMock = $this->getMock(
236266
'Magento\Quote\Model\Quote',
@@ -248,28 +278,9 @@ public function testGetQuote()
248278
'',
249279
false
250280
);
251-
252-
$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
253-
->getMock();
254-
$defaultGroup->expects($this->any())
255-
->method('getId')
256-
->will($this->returnValue($customerGroupId));
257-
$this->groupManagementMock->expects($this->any())
258-
->method('getDefaultGroup')
259-
->will($this->returnValue($defaultGroup));
260-
261-
$this->quoteRepositoryMock->expects($this->once())
262-
->method('create')
263-
->will($this->returnValue($quoteMock));
264-
$this->quote->expects($this->any())
265-
->method('getStoreId')
266-
->will($this->returnValue($storeId));
267281
$quoteMock->expects($this->once())
268282
->method('setStoreId')
269283
->with($storeId);
270-
$this->quote->expects($this->any())
271-
->method('getQuoteId')
272-
->will($this->returnValue(null));
273284
$quoteMock->expects($this->once())
274285
->method('setCustomerGroupId')
275286
->with($customerGroupId)
@@ -278,25 +289,9 @@ public function testGetQuote()
278289
->method('setIsActive')
279290
->with(false)
280291
->will($this->returnSelf());
281-
$this->quoteRepositoryMock->expects($this->once())
282-
->method('save')
283-
->with($quoteMock);
284292
$quoteMock->expects($this->once())
285293
->method('getId')
286294
->will($this->returnValue($quoteId));
287-
$this->quote->expects($this->any())
288-
->method('setQuoteId')
289-
->with($quoteId);
290-
$this->quote->expects($this->any())
291-
->method('getCustomerId')
292-
->will($this->returnValue($customerId));
293-
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
294-
->disableOriginalConstructor()
295-
->getMock();
296-
$this->customerRepositoryMock->expects($this->once())
297-
->method('getById')
298-
->with($customerId)
299-
->willReturn($dataCustomerMock);
300295
$quoteMock->expects($this->once())
301296
->method('assignCustomer')
302297
->with($dataCustomerMock);
@@ -307,19 +302,47 @@ public function testGetQuote()
307302
->method('setIsSuperMode')
308303
->with(true);
309304

305+
$this->quoteRepositoryMock->expects($this->once())
306+
->method('create')
307+
->will($this->returnValue($quoteMock));
308+
$this->quoteRepositoryMock->expects($this->once())
309+
->method('save')
310+
->with($quoteMock);
311+
310312
$this->assertEquals($quoteMock, $this->quote->getQuote());
311313
}
312314

313315
/**
314316
* Run test getQuote method
315317
*
316318
* @return void
319+
* @dataProvider getQuoteDataProvider
317320
*/
318-
public function testGetQuoteGet()
321+
public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes)
319322
{
320-
$storeId = 10;
321323
$quoteId = 22;
322-
$customerId = 66;
324+
$storeId = 10;
325+
326+
$this->quote->expects($this->any())
327+
->method('getQuoteId')
328+
->will($this->returnValue($quoteId));
329+
$this->quote->expects($this->any())
330+
->method('setQuoteId')
331+
->with($quoteId);
332+
$this->quote->expects($this->any())
333+
->method('getStoreId')
334+
->will($this->returnValue($storeId));
335+
$this->quote->expects($this->any())
336+
->method('getCustomerId')
337+
->will($this->returnValue($customerId));
338+
339+
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
340+
->disableOriginalConstructor()
341+
->getMock();
342+
$this->customerRepositoryMock->expects($this->$expectedNumberOfInvokes())
343+
->method('getById')
344+
->with($customerId)
345+
->willReturn($dataCustomerMock);
323346

324347
$quoteMock = $this->getMock(
325348
'Magento\Quote\Model\Quote',
@@ -331,43 +354,17 @@ public function testGetQuoteGet()
331354
'assignCustomer',
332355
'setIgnoreOldQty',
333356
'setIsSuperMode',
357+
'getCustomerId',
334358
'__wakeup'
335359
],
336360
[],
337361
'',
338362
false
339363
);
340-
341-
$this->quoteRepositoryMock->expects($this->once())
342-
->method('create')
343-
->will($this->returnValue($quoteMock));
344-
$this->quote->expects($this->any())
345-
->method('getStoreId')
346-
->will($this->returnValue($storeId));
347364
$quoteMock->expects($this->once())
348365
->method('setStoreId')
349366
->with($storeId);
350-
$this->quote->expects($this->any())
351-
->method('getQuoteId')
352-
->will($this->returnValue($quoteId));
353-
$this->quoteRepositoryMock->expects($this->once())
354-
->method('get')
355-
->with($quoteId)
356-
->willReturn($quoteMock);
357-
$this->quote->expects($this->any())
358-
->method('setQuoteId')
359-
->with($quoteId);
360-
$this->quote->expects($this->any())
361-
->method('getCustomerId')
362-
->will($this->returnValue($customerId));
363-
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
364-
->disableOriginalConstructor()
365-
->getMock();
366-
$this->customerRepositoryMock->expects($this->once())
367-
->method('getById')
368-
->with($customerId)
369-
->willReturn($dataCustomerMock);
370-
$quoteMock->expects($this->once())
367+
$quoteMock->expects($this->$expectedNumberOfInvokes())
371368
->method('assignCustomer')
372369
->with($dataCustomerMock);
373370
$quoteMock->expects($this->once())
@@ -376,7 +373,29 @@ public function testGetQuoteGet()
376373
$quoteMock->expects($this->once())
377374
->method('setIsSuperMode')
378375
->with(true);
376+
$quoteMock->expects($this->once())
377+
->method('getCustomerId')
378+
->will($this->returnValue($quoteCustomerId));
379+
380+
$this->quoteRepositoryMock->expects($this->once())
381+
->method('create')
382+
->will($this->returnValue($quoteMock));
383+
$this->quoteRepositoryMock->expects($this->once())
384+
->method('get')
385+
->with($quoteId)
386+
->willReturn($quoteMock);
379387

380388
$this->assertEquals($quoteMock, $this->quote->getQuote());
381389
}
390+
391+
/**
392+
* @return array
393+
*/
394+
public function getQuoteDataProvider()
395+
{
396+
return [
397+
'customer ids different' => [66, null, 'once'],
398+
'customer ids same' => [66, 66, 'never'],
399+
];
400+
}
382401
}

app/code/Magento/Catalog/Block/Product/View/Options.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,30 @@ public function getJsonConfig()
206206
/* @var $option \Magento\Catalog\Model\Product\Option */
207207
$priceValue = 0;
208208
if ($option->getGroupByType() == \Magento\Catalog\Model\Product\Option::OPTION_GROUP_SELECT) {
209-
$_tmpPriceValues = [];
209+
$tmpPriceValues = [];
210210
foreach ($option->getValues() as $value) {
211211
/* @var $value \Magento\Catalog\Model\Product\Option\Value */
212212
$id = $value->getId();
213-
$_tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
213+
$tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
214214
}
215-
$priceValue = $_tmpPriceValues;
215+
$priceValue = $tmpPriceValues;
216216
} else {
217217
$priceValue = $this->_getPriceConfiguration($option);
218218
}
219219
$config[$option->getId()] = $priceValue;
220220
}
221221

222+
$configObj = new \Magento\Framework\Object(
223+
[
224+
'config' => $config,
225+
]
226+
);
227+
228+
//pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
229+
$this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);
230+
231+
$config=$configObj->getConfig();
232+
222233
return $this->_jsonEncoder->encode($config);
223234
}
224235

app/code/Magento/Catalog/Model/Product.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,25 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
250250
protected $metadataService;
251251

252252
/*
253-
* @param \Magento\Catalog\Model\ProductLink\ProductLinkManagementInterface
253+
* @param \Magento\Catalog\Model\ProductLink\CollectionProvider
254254
*/
255-
protected $linkManagement;
255+
protected $entityCollectionProvider;
256256

257257
/*
258-
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
258+
* @param \Magento\Catalog\Model\Product\LinkTypeProvider
259+
*/
260+
protected $linkProvider;
261+
262+
/*
263+
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory
259264
*/
260265
protected $productLinkFactory;
261266

267+
/*
268+
* @param \Magento\Catalog\Api\Data\ProductLinkExtensionFactory
269+
*/
270+
protected $productLinkExtensionFactory;
271+
262272
/**
263273
* @var \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory
264274
*/
@@ -318,8 +328,10 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
318328
* @param Indexer\Product\Eav\Processor $productEavIndexerProcessor
319329
* @param CategoryRepositoryInterface $categoryRepository
320330
* @param Product\Image\CacheFactory $imageCacheFactory
321-
* @param \Magento\Catalog\Model\ProductLink\Management $linkManagement
322-
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
331+
* @param \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider
332+
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
333+
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
334+
* @param \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory
323335
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
324336
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
325337
* @param array $data
@@ -354,8 +366,10 @@ public function __construct(
354366
\Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
355367
CategoryRepositoryInterface $categoryRepository,
356368
Product\Image\CacheFactory $imageCacheFactory,
357-
\Magento\Catalog\Model\ProductLink\Management $linkManagement,
369+
\Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider,
370+
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
358371
\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
372+
\Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory,
359373
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory,
360374
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
361375
array $data = []
@@ -380,8 +394,10 @@ public function __construct(
380394
$this->_productEavIndexerProcessor = $productEavIndexerProcessor;
381395
$this->categoryRepository = $categoryRepository;
382396
$this->imageCacheFactory = $imageCacheFactory;
383-
$this->linkManagement = $linkManagement;
397+
$this->entityCollectionProvider = $entityCollectionProvider;
398+
$this->linkTypeProvider = $linkTypeProvider;
384399
$this->productLinkFactory = $productLinkFactory;
400+
$this->productLinkExtensionFactory = $productLinkExtensionFactory;
385401
$this->mediaGalleryEntryFactory = $mediaGalleryEntryFactory;
386402
$this->dataObjectHelper = $dataObjectHelper;
387403
parent::__construct(
@@ -1352,23 +1368,34 @@ public function getCrossSellLinkCollection()
13521368
public function getProductLinks()
13531369
{
13541370
if (empty($this->_links)) {
1355-
$productLinks = [];
1356-
1357-
$productLinks['related'] = $this->getRelatedProducts();
1358-
$productLinks['upsell'] = $this->getUpSellProducts();
1359-
$productLinks['crosssell'] = $this->getCrossSellProducts();
1360-
13611371
$output = [];
1362-
foreach ($productLinks as $type => $linkTypeArray) {
1363-
foreach ($linkTypeArray as $link) {
1372+
$linkTypes = $this->linkTypeProvider->getLinkTypes();
1373+
foreach (array_keys($linkTypes) as $linkTypeName) {
1374+
$collection = $this->entityCollectionProvider->getCollection($this, $linkTypeName);
1375+
foreach ($collection as $item) {
13641376
/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
13651377
$productLink = $this->productLinkFactory->create();
13661378
$productLink->setProductSku($this->getSku())
1367-
->setLinkType($type)
1368-
->setLinkedProductSku($link['sku'])
1369-
->setLinkedProductType($link['type_id'])
1370-
->setPosition($link['position']);
1371-
1379+
->setLinkType($linkTypeName)
1380+
->setLinkedProductSku($item['sku'])
1381+
->setLinkedProductType($item['type'])
1382+
->setPosition($item['position']);
1383+
if (isset($item['custom_attributes'])) {
1384+
$productLinkExtension = $productLink->getExtensionAttributes();
1385+
if ($productLinkExtension === null) {
1386+
$productLinkExtension = $this->productLinkExtensionFactory->create();
1387+
}
1388+
foreach ($item['custom_attributes'] as $option) {
1389+
$name = $option['attribute_code'];
1390+
$value = $option['value'];
1391+
$setterName = 'set' . ucfirst($name);
1392+
// Check if setter exists
1393+
if (method_exists($productLinkExtension, $setterName)) {
1394+
call_user_func([$productLinkExtension, $setterName], $value);
1395+
}
1396+
}
1397+
$productLink->setExtensionAttributes($productLinkExtension);
1398+
}
13721399
$output[] = $productLink;
13731400
}
13741401
}

0 commit comments

Comments
 (0)