Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
cart_rule_10_percent_off.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
12 
13 $objectManager = Bootstrap::getObjectManager();
14 $websiteId = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)
15  ->getWebsite()
16  ->getId();
17 
19 $salesRule = $objectManager->create(Rule::class);
20 $salesRule->setData(
21  [
22  'name' => '10% Off on orders with two items',
23  'is_active' => 1,
24  'customer_group_ids' => [GroupManagement::NOT_LOGGED_IN_ID],
25  'coupon_type' => Rule::COUPON_TYPE_NO_COUPON,
26  'simple_action' => 'by_percent',
27  'discount_amount' => 10,
28  'discount_step' => 0,
29  'stop_rules_processing' => 1,
30  'website_ids' => [$websiteId]
31  ]
32 );
33 
34 $salesRule->getConditions()->loadArray([
35  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
36  'attribute' => null,
37  'operator' => null,
38  'value' => '1',
39  'is_value_processed' => null,
40  'aggregator' => 'all',
41  'conditions' =>
42  [
43  [
44  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
45  'attribute' => 'qty',
46  'operator' => '==',
47  'value' => '2',
48  'is_value_processed' => null,
49  'aggregator' => 'all',
50  'conditions' =>
51  [
52  [
53  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
54  'attribute' => 'attribute_set_id',
55  'operator' => '==',
56  'value' => '4',
57  'is_value_processed' => false,
58  ],
59  ],
60  ],
61  ],
62 ]);
63 
64 $salesRule->save();