Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
rule_custom_product_attribute.php
Go to the documentation of this file.
1 <?php
8 
9 $entityTypeId = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
10  ->loadByCode('catalog_category')
11  ->getId();
12 
14  'attribute_code' => 'attribute_for_sales_rule_1',
15  'entity_type_id' => $entityTypeId,
16  'backend_type' => 'varchar',
17  'is_required' => 1,
18  'is_user_defined' => 1,
19  'is_unique' => 0,
20  'is_used_for_promo_rules' => 1,
21 ];
22 
24 $attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
25 $attribute->setData($attributeData);
26 $attribute->save();
27 
30 $salesRule->setData(
31  [
32  'name' => '50% Off on some attribute',
33  'is_active' => 1,
34  'customer_group_ids' => [\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID],
35  'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON,
36  'simple_action' => 'by_percent',
37  'discount_amount' => 50,
38  'discount_step' => 0,
39  'stop_rules_processing' => 1,
40  'website_ids' => [
41  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
42  \Magento\Store\Model\StoreManagerInterface::class
43  )->getWebsite()->getId()
44  ]
45  ]
46 );
47 
48 $salesRule->getConditions()->loadArray([
49  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
50  'attribute' => null,
51  'operator' => null,
52  'value' => '1',
53  'is_value_processed' => null,
54  'aggregator' => 'all',
55  'conditions' => [
56  [
57  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
58  'attribute' => null,
59  'operator' => null,
60  'value' => '0',
61  'is_value_processed' => null,
62  'aggregator' => 'all',
63  'conditions' => [
64  [
65  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
66  'attribute' => 'attribute_for_sales_rule_1',
67  'operator' => '==',
68  'value' => '2',
69  'is_value_processed' => false,
70  ],
71  ],
72  ],
73  ],
74 ]);
75 
76 $salesRule->save();