Skip to content

Commit e47cafa

Browse files
committed
Merge branch 'MAGETWO-63350' of github.com:magento-troll/magento2ce into MAGETWO-58456
2 parents 2055c25 + f366181 commit e47cafa

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

app/code/Magento/SalesRule/Model/ResourceModel/Rule.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Rule\Model\ResourceModel\AbstractResource;
1212
use Magento\Framework\EntityManager\EntityManager;
1313
use Magento\Framework\Serialize\Serializer\Json;
14+
use Magento\Framework\EntityManager\MetadataPool;
15+
use Magento\SalesRule\Api\Data\RuleInterface;
1416

1517
/**
1618
* Sales Rule resource model
@@ -51,29 +53,37 @@ class Rule extends AbstractResource
5153
*/
5254
protected $entityManager;
5355

56+
/**
57+
* @var MetadataPool
58+
*/
59+
private $metadataPool;
60+
5461
/**
5562
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5663
* @param \Magento\Framework\Stdlib\StringUtils $string
5764
* @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon
5865
* @param string $connectionName
5966
* @param \Magento\Framework\DataObject|null $associatedEntityMapInstance
6067
* @param Json $serializer Optional parameter for backward compatibility
68+
* @param MetadataPool $metadataPool Optional parameter for backward compatibility
6169
*/
6270
public function __construct(
6371
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6472
\Magento\Framework\Stdlib\StringUtils $string,
6573
\Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon,
6674
$connectionName = null,
6775
\Magento\Framework\DataObject $associatedEntityMapInstance = null,
68-
Json $serializer = null
76+
Json $serializer = null,
77+
MetadataPool $metadataPool = null
6978
) {
7079
$this->string = $string;
7180
$this->_resourceCoupon = $resourceCoupon;
7281
$associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get(
7382
\Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class
7483
);
7584
$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);
7787
parent::__construct($context, $connectionName);
7888
}
7989

@@ -305,7 +315,11 @@ public function getActiveAttributes()
305315
public function setActualProductAttributes($rule, $attributes)
306316
{
307317
$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+
);
309323

310324
//Getting attribute IDs for attribute codes
311325
$attributeIds = [];
@@ -327,7 +341,7 @@ public function setActualProductAttributes($rule, $attributes)
327341
foreach ($rule->getWebsiteIds() as $websiteId) {
328342
foreach ($attributeIds as $attribute) {
329343
$data[] = [
330-
'rule_id' => $rule->getId(),
344+
$metadata->getLinkField() => $rule->getData($metadata->getLinkField()),
331345
'website_id' => $websiteId,
332346
'customer_group_id' => $customerGroupId,
333347
'attribute_id' => $attribute,

app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ public function _initSelect()
282282
public function addAttributeInConditionFilter($attributeCode)
283283
{
284284
$match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
285+
/**
286+
* Information about conditions and actions stored in table as JSON encoded array
287+
* in fields conditions_serialized and actions_serialized.
288+
* If you want to find rules that contains some particular attribute, the easiest way to do so is serialize
289+
* attribute code in the same way as it stored in the serialized columns and execute SQL search
290+
* with like condition.
291+
* Table
292+
* +-------------------------------------------------------------------+
293+
* | conditions_serialized | actions_serialized |
294+
* +-------------------------------------------------------------------+
295+
* | {..."attribute":"attr_name"...} | {..."attribute":"attr_name"...} |
296+
* +---------------------------------|---------------------------------+
297+
* From attribute code "attr_code", will be generated such SQL:
298+
* `condition_serialized` LIKE '%"attribute":"attr_name"%'
299+
* OR `actions_serialized` LIKE '%"attribute":"attr_name"%'
300+
*/
285301
$field = $this->_getMappedField('conditions_serialized');
286302
$cCond = $this->_getConditionSql($field, ['like' => $match]);
287303
$field = $this->_getMappedField('actions_serialized');

0 commit comments

Comments
 (0)