/
Validation.php
50 lines (46 loc) · 1.6 KB
/
Validation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule;
use \Magento\ConfigurableProduct\Model\Product\Type\Configurable;
/**
* Class Validation. Call validate method for configurable product instead simple product
*/
class Validation
{
/** @var Configurable */
private $configurable;
/**
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType
*/
public function __construct(Configurable $configurableType)
{
$this->configurable = $configurableType;
}
/**
* @param \Magento\CatalogRule\Model\Rule $rule
* @param \Closure $proceed
* @param \Magento\Framework\DataObject|\Magento\Catalog\Model\Product $product
* @return bool
*/
public function aroundValidate(
\Magento\CatalogRule\Model\Rule $rule,
\Closure $proceed,
\Magento\Framework\DataObject $product
) {
$validateResult = $proceed($product);
if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) {
foreach ($configurableProducts as $configurableProductId) {
$validateResult = $rule->getConditions()->validateByEntityId($configurableProductId);
// If any of configurable product is valid for current rule, then their sub-product must be valid too
if ($validateResult) {
break;
}
}
}
return $validateResult;
}
}