5
5
*/
6
6
namespace Magento \SalesRule \Model \Rule \Condition ;
7
7
8
+ use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
9
+
8
10
/**
9
11
* Product rule condition data model
10
12
*
11
13
* @author Magento Core Team <[email protected] >
14
+ *
15
+ * @method string getAttribute()
12
16
*/
13
17
class Product extends \Magento \Rule \Model \Condition \Product \AbstractProduct
14
18
{
@@ -26,20 +30,47 @@ protected function _addSpecialAttributes(array &$attributes)
26
30
$ attributes ['quote_item_row_total ' ] = __ ('Row total in cart ' );
27
31
}
28
32
33
+ /**
34
+ * @param \Magento\Framework\Model\AbstractModel $model
35
+ *
36
+ * @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product
37
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
38
+ */
39
+ protected function getProductToValidate (\Magento \Framework \Model \AbstractModel $ model )
40
+ {
41
+ /** @var \Magento\Catalog\Model\Product $product */
42
+ $ product = $ model ->getProduct ();
43
+ if (!$ product instanceof \Magento \Catalog \Model \Product) {
44
+ $ product = $ this ->productRepository ->getById ($ model ->getProductId ());
45
+ }
46
+
47
+ $ attrCode = $ this ->getAttribute ();
48
+
49
+ /* Check for attributes which are not available for configurable products */
50
+ if ($ product ->getTypeId () == Configurable::TYPE_CODE && !$ product ->hasData ($ attrCode )) {
51
+ /** @var \Magento\Catalog\Api\Data\ProductInterface $childProduct */
52
+ $ childProduct = current ($ model ->getChildren ())->getProduct ();
53
+ if ($ childProduct ->hasData ($ attrCode )) {
54
+ $ product = $ childProduct ;
55
+ }
56
+ }
57
+
58
+ return $ product ;
59
+ }
60
+
29
61
/**
30
62
* Validate Product Rule Condition
31
63
*
32
64
* @param \Magento\Framework\Model\AbstractModel $model
65
+ *
33
66
* @return bool
67
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
34
68
*/
35
69
public function validate (\Magento \Framework \Model \AbstractModel $ model )
36
70
{
37
71
//@todo reimplement this method when is fixed MAGETWO-5713
38
72
/** @var \Magento\Catalog\Model\Product $product */
39
- $ product = $ model ->getProduct ();
40
- if (!$ product instanceof \Magento \Catalog \Model \Product) {
41
- $ product = $ this ->productRepository ->getById ($ model ->getProductId ());
42
- }
73
+ $ product = $ this ->getProductToValidate ($ model );
43
74
44
75
$ product ->setQuoteItemQty (
45
76
$ model ->getQty ()
@@ -49,9 +80,8 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
49
80
$ model ->getBaseRowTotal ()
50
81
);
51
82
52
- $ attrCode = $ this ->getAttribute ();
53
83
54
- if ('category_ids ' == $ attrCode ) {
84
+ if ('category_ids ' == $ this -> getAttribute () ) {
55
85
return $ this ->validateAttribute ($ this ->_getAvailableInCategories ($ product ->getId ()));
56
86
}
57
87
0 commit comments