Skip to content

Commit c78c130

Browse files
authored
Merge pull request #4161 from magento-tsg/2.1.18-develop-pr71
[TSG] Backporting for 2.1 (pr71) (2.1.18-develop)
2 parents 5cba861 + 79825eb commit c78c130

File tree

15 files changed

+543
-114
lines changed

15 files changed

+543
-114
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Product;
8+
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
12+
/**
13+
* Class to check that product is saleable.
14+
*/
15+
class SalabilityChecker
16+
{
17+
/**
18+
* @var ProductRepositoryInterface
19+
*/
20+
private $productRepository;
21+
22+
/**
23+
* @var StoreManagerInterface
24+
*/
25+
private $storeManager;
26+
27+
/**
28+
* @param ProductRepositoryInterface $productRepository
29+
* @param StoreManagerInterface $storeManager
30+
*/
31+
public function __construct(
32+
ProductRepositoryInterface $productRepository,
33+
StoreManagerInterface $storeManager
34+
) {
35+
$this->productRepository = $productRepository;
36+
$this->storeManager = $storeManager;
37+
}
38+
39+
/**
40+
* Check if product is salable.
41+
*
42+
* @param int $productId
43+
* @param int|null $storeId
44+
* @return bool
45+
*/
46+
public function isSalable($productId, $storeId = null)
47+
{
48+
if ($storeId === null) {
49+
$storeId = $this->storeManager->getStore()->getId();
50+
}
51+
/** @var \Magento\Catalog\Model\Product $product */
52+
$product = $this->productRepository->getById($productId, false, $storeId);
53+
54+
return $product->isSalable();
55+
}
56+
}

app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public function __construct(
9191
}
9292

9393
/**
94+
* Return currency symbol.
95+
*
9496
* @return string
9597
*/
9698
public function getCurrencySymbol()
@@ -173,6 +175,7 @@ public function getEditProductUrl($id)
173175
* Retrieve attributes data
174176
*
175177
* @return array
178+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
176179
*/
177180
protected function getAttributes()
178181
{
@@ -263,6 +266,8 @@ public function getImageUploadUrl()
263266
}
264267

265268
/**
269+
* Return product qty.
270+
*
266271
* @param Product $product
267272
* @return float
268273
*/
@@ -272,6 +277,8 @@ public function getProductStockQty(Product $product)
272277
}
273278

274279
/**
280+
* Return variation wizard.
281+
*
275282
* @param array $initData
276283
* @return string
277284
*/
@@ -287,6 +294,8 @@ public function getVariationWizard($initData)
287294
}
288295

289296
/**
297+
* Return product configuration matrix.
298+
*
290299
* @return array|null
291300
*/
292301
public function getProductMatrix()
@@ -298,17 +307,22 @@ public function getProductMatrix()
298307
}
299308

300309
/**
310+
* Return product attributes.
311+
*
301312
* @return array|null
302313
*/
303314
public function getProductAttributes()
304315
{
305316
if ($this->productAttributes === null) {
306317
$this->prepareVariations();
307318
}
319+
308320
return $this->productAttributes;
309321
}
310322

311323
/**
324+
* Prepare product variations.
325+
*
312326
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
313327
* @return void
314328
* TODO: move to class
@@ -333,36 +347,13 @@ protected function prepareVariations()
333347
$price = $product->getPrice();
334348
$variationOptions = [];
335349
foreach ($usedProductAttributes as $attribute) {
336-
if (!isset($attributes[$attribute->getAttributeId()])) {
337-
$attributes[$attribute->getAttributeId()] = [
338-
'code' => $attribute->getAttributeCode(),
339-
'label' => $attribute->getStoreLabel(),
340-
'id' => $attribute->getAttributeId(),
341-
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
342-
'chosen' => [],
343-
];
344-
foreach ($attribute->getOptions() as $option) {
345-
if (!empty($option->getValue())) {
346-
$attributes[$attribute->getAttributeId()]['options'][] = [
347-
'attribute_code' => $attribute->getAttributeCode(),
348-
'attribute_label' => $attribute->getStoreLabel(0),
349-
'id' => $option->getValue(),
350-
'label' => $option->getLabel(),
351-
'value' => $option->getValue(),
352-
];
353-
}
354-
}
355-
}
356-
$optionId = $variation[$attribute->getId()]['value'];
357-
$variationOption = [
358-
'attribute_code' => $attribute->getAttributeCode(),
359-
'attribute_label' => $attribute->getStoreLabel(0),
360-
'id' => $optionId,
361-
'label' => $variation[$attribute->getId()]['label'],
362-
'value' => $optionId,
363-
];
364-
$variationOptions[] = $variationOption;
365-
$attributes[$attribute->getAttributeId()]['chosen'][] = $variationOption;
350+
list($attributes, $variationOptions) = $this->prepareAttributes(
351+
$attributes,
352+
$attribute,
353+
$configurableAttributes,
354+
$variation,
355+
$variationOptions
356+
);
366357
}
367358

368359
$productMatrix[] = [
@@ -376,12 +367,66 @@ protected function prepareVariations()
376367
'price' => $price,
377368
'options' => $variationOptions,
378369
'weight' => $product->getWeight(),
379-
'status' => $product->getStatus()
370+
'status' => $product->getStatus(),
371+
'__disableTmpl' => true,
380372
];
381373
}
382374
}
383375
}
384376
$this->productMatrix = $productMatrix;
385377
$this->productAttributes = array_values($attributes);
386378
}
379+
380+
/**
381+
* Prepare attributes.
382+
*
383+
* @param array $attributes
384+
* @param object $attribute
385+
* @param array $configurableAttributes
386+
* @param array $variation
387+
* @param array $variationOptions
388+
* @return array
389+
*/
390+
private function prepareAttributes(
391+
array $attributes,
392+
$attribute,
393+
array $configurableAttributes,
394+
array $variation,
395+
array $variationOptions
396+
) {
397+
if (!isset($attributes[$attribute->getAttributeId()])) {
398+
$attributes[$attribute->getAttributeId()] = [
399+
'code' => $attribute->getAttributeCode(),
400+
'label' => $attribute->getStoreLabel(),
401+
'id' => $attribute->getAttributeId(),
402+
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
403+
'chosen' => [],
404+
];
405+
foreach ($attribute->getOptions() as $option) {
406+
if (!empty($option->getValue())) {
407+
$attributes[$attribute->getAttributeId()]['options'][] = [
408+
'attribute_code' => $attribute->getAttributeCode(),
409+
'attribute_label' => $attribute->getStoreLabel(0),
410+
'id' => $option->getValue(),
411+
'label' => $option->getLabel(),
412+
'value' => $option->getValue(),
413+
'__disableTmpl' => true,
414+
];
415+
}
416+
}
417+
}
418+
$optionId = $variation[$attribute->getId()]['value'];
419+
$variationOption = [
420+
'attribute_code' => $attribute->getAttributeCode(),
421+
'attribute_label' => $attribute->getStoreLabel(0),
422+
'id' => $optionId,
423+
'label' => $variation[$attribute->getId()]['label'],
424+
'value' => $optionId,
425+
'__disableTmpl' => true,
426+
];
427+
$variationOptions[] = $variationOption;
428+
$attributes[$attribute->getAttributeId()]['chosen'][] = $variationOption;
429+
430+
return [$attributes, $variationOptions];
431+
}
387432
}

app/code/Magento/Downloadable/Controller/Download/LinkSample.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66
*/
77
namespace Magento\Downloadable\Controller\Download;
88

9+
use Magento\Catalog\Model\Product\SalabilityChecker;
910
use Magento\Downloadable\Helper\Download as DownloadHelper;
11+
use Magento\Framework\App\Action\Context;
1012
use Magento\Framework\App\ResponseInterface;
1113

14+
/**
15+
* Class LinkSample executes download sample link action.
16+
*/
1217
class LinkSample extends \Magento\Downloadable\Controller\Download
1318
{
1419
/**
15-
* Download link's sample action
20+
* @var SalabilityChecker
21+
*/
22+
private $salabilityChecker;
23+
24+
/**
25+
* @param Context $context
26+
* @param SalabilityChecker|null $salabilityChecker
27+
*/
28+
public function __construct(
29+
Context $context,
30+
SalabilityChecker $salabilityChecker = null
31+
) {
32+
parent::__construct($context);
33+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
34+
}
35+
36+
/**
37+
* Download link's sample action.
1638
*
1739
* @return ResponseInterface
1840
* @SuppressWarnings(PHPMD.ExitExpression)
@@ -21,16 +43,16 @@ public function execute()
2143
{
2244
$linkId = $this->getRequest()->getParam('link_id', 0);
2345
/** @var \Magento\Downloadable\Model\Link $link */
24-
$link = $this->_objectManager->create('Magento\Downloadable\Model\Link')->load($linkId);
25-
if ($link->getId()) {
46+
$link = $this->_objectManager->create(\Magento\Downloadable\Model\Link::class)->load($linkId);
47+
if ($link->getId() && $this->salabilityChecker->isSalable($link->getProductId())) {
2648
$resource = '';
2749
$resourceType = '';
2850
if ($link->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
2951
$resource = $link->getSampleUrl();
3052
$resourceType = DownloadHelper::LINK_TYPE_URL;
3153
} elseif ($link->getSampleType() == DownloadHelper::LINK_TYPE_FILE) {
3254
$resource = $this->_objectManager->get(
33-
'Magento\Downloadable\Helper\File'
55+
\Magento\Downloadable\Helper\File::class
3456
)->getFilePath(
3557
$this->_getLink()->getBaseSamplePath(),
3658
$link->getSampleFile()
@@ -46,6 +68,7 @@ public function execute()
4668
);
4769
}
4870
}
71+
4972
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
5073
}
5174
}

app/code/Magento/Downloadable/Controller/Download/Sample.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66
*/
77
namespace Magento\Downloadable\Controller\Download;
88

9+
use Magento\Catalog\Model\Product\SalabilityChecker;
910
use Magento\Downloadable\Helper\Download as DownloadHelper;
11+
use Magento\Framework\App\Action\Context;
1012
use Magento\Framework\App\ResponseInterface;
1113

14+
/**
15+
* Class Sample executes download sample action.
16+
*/
1217
class Sample extends \Magento\Downloadable\Controller\Download
1318
{
1419
/**
15-
* Download sample action
20+
* @var SalabilityChecker
21+
*/
22+
private $salabilityChecker;
23+
24+
/**
25+
* @param Context $context
26+
* @param SalabilityChecker|null $salabilityChecker
27+
*/
28+
public function __construct(
29+
Context $context,
30+
SalabilityChecker $salabilityChecker = null
31+
) {
32+
parent::__construct($context);
33+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
34+
}
35+
36+
/**
37+
* Download sample action.
1638
*
1739
* @return ResponseInterface
1840
* @SuppressWarnings(PHPMD.ExitExpression)
@@ -21,16 +43,16 @@ public function execute()
2143
{
2244
$sampleId = $this->getRequest()->getParam('sample_id', 0);
2345
/** @var \Magento\Downloadable\Model\Sample $sample */
24-
$sample = $this->_objectManager->create('Magento\Downloadable\Model\Sample')->load($sampleId);
25-
if ($sample->getId()) {
46+
$sample = $this->_objectManager->create(\Magento\Downloadable\Model\Sample::class)->load($sampleId);
47+
if ($sample->getId() && $this->salabilityChecker->isSalable($sample->getProductId())) {
2648
$resource = '';
2749
$resourceType = '';
2850
if ($sample->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
2951
$resource = $sample->getSampleUrl();
3052
$resourceType = DownloadHelper::LINK_TYPE_URL;
3153
} elseif ($sample->getSampleType() == DownloadHelper::LINK_TYPE_FILE) {
3254
/** @var \Magento\Downloadable\Helper\File $helper */
33-
$helper = $this->_objectManager->get('Magento\Downloadable\Helper\File');
55+
$helper = $this->_objectManager->get(\Magento\Downloadable\Helper\File::class);
3456
$resource = $helper->getFilePath($sample->getBasePath(), $sample->getSampleFile());
3557
$resourceType = DownloadHelper::LINK_TYPE_FILE;
3658
}
@@ -43,6 +65,7 @@ public function execute()
4365
);
4466
}
4567
}
68+
4669
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
4770
}
4871
}

0 commit comments

Comments
 (0)