11
11
use Magento \Rule \Model \ResourceModel \AbstractResource ;
12
12
use Magento \Framework \EntityManager \EntityManager ;
13
13
use Magento \Framework \Serialize \Serializer \Json ;
14
+ use Magento \Framework \EntityManager \MetadataPool ;
15
+ use Magento \SalesRule \Api \Data \RuleInterface ;
14
16
15
17
/**
16
18
* Sales Rule resource model
@@ -51,29 +53,37 @@ class Rule extends AbstractResource
51
53
*/
52
54
protected $ entityManager ;
53
55
56
+ /**
57
+ * @var MetadataPool
58
+ */
59
+ private $ metadataPool ;
60
+
54
61
/**
55
62
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
56
63
* @param \Magento\Framework\Stdlib\StringUtils $string
57
64
* @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon
58
65
* @param string $connectionName
59
66
* @param \Magento\Framework\DataObject|null $associatedEntityMapInstance
60
67
* @param Json $serializer Optional parameter for backward compatibility
68
+ * @param MetadataPool $metadataPool Optional parameter for backward compatibility
61
69
*/
62
70
public function __construct (
63
71
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
64
72
\Magento \Framework \Stdlib \StringUtils $ string ,
65
73
\Magento \SalesRule \Model \ResourceModel \Coupon $ resourceCoupon ,
66
74
$ connectionName = null ,
67
75
\Magento \Framework \DataObject $ associatedEntityMapInstance = null ,
68
- Json $ serializer = null
76
+ Json $ serializer = null ,
77
+ MetadataPool $ metadataPool = null
69
78
) {
70
79
$ this ->string = $ string ;
71
80
$ this ->_resourceCoupon = $ resourceCoupon ;
72
81
$ associatedEntitiesMapInstance = $ associatedEntityMapInstance ?: ObjectManager::getInstance ()->get (
73
82
\Magento \SalesRule \Model \ResourceModel \Rule \AssociatedEntityMap::class
74
83
);
75
84
$ this ->_associatedEntitiesMap = $ associatedEntitiesMapInstance ->getData ();
76
- $ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Json::class);
85
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
86
+ $ this ->metadataPool = $ metadataPool ?: ObjectManager::getInstance ()->get (MetadataPool::class);
77
87
parent ::__construct ($ context , $ connectionName );
78
88
}
79
89
@@ -305,7 +315,11 @@ public function getActiveAttributes()
305
315
public function setActualProductAttributes ($ rule , $ attributes )
306
316
{
307
317
$ connection = $ this ->getConnection ();
308
- $ connection ->delete ($ this ->getTable ('salesrule_product_attribute ' ), ['rule_id=? ' => $ rule ->getId ()]);
318
+ $ metadata = $ this ->metadataPool ->getMetadata (RuleInterface::class);
319
+ $ connection ->delete (
320
+ $ this ->getTable ('salesrule_product_attribute ' ),
321
+ [$ metadata ->getLinkField () . '=? ' => $ rule ->getData ($ metadata ->getLinkField ())]
322
+ );
309
323
310
324
//Getting attribute IDs for attribute codes
311
325
$ attributeIds = [];
@@ -327,7 +341,7 @@ public function setActualProductAttributes($rule, $attributes)
327
341
foreach ($ rule ->getWebsiteIds () as $ websiteId ) {
328
342
foreach ($ attributeIds as $ attribute ) {
329
343
$ data [] = [
330
- ' rule_id ' => $ rule ->getId ( ),
344
+ $ metadata -> getLinkField () => $ rule ->getData ( $ metadata -> getLinkField () ),
331
345
'website_id ' => $ websiteId ,
332
346
'customer_group_id ' => $ customerGroupId ,
333
347
'attribute_id ' => $ attribute ,
0 commit comments