Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
coupon_cart_fixed_discount.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
14 
15 $objectManager = Bootstrap::getObjectManager();
16 
18 $salesRule = $objectManager->create(Rule::class);
19 $salesRule->setData(
20  [
21  'name' => '15$ fixed discount on whole cart',
22  'is_active' => 1,
23  'customer_group_ids' => [GroupManagement::NOT_LOGGED_IN_ID],
24  'coupon_type' => Rule::COUPON_TYPE_SPECIFIC,
25  'conditions' => [
26  [
27  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
28  'attribute' => 'base_subtotal',
29  'operator' => '>',
30  'value' => 45,
31  ],
32  ],
33  'simple_action' => Rule::CART_FIXED_ACTION,
34  'discount_amount' => 15,
35  'discount_step' => 0,
36  'stop_rules_processing' => 1,
37  'website_ids' => [
38  $objectManager->get(StoreManagerInterface::class)->getWebsite()->getId(),
39  ],
40  ]
41 );
42 $objectManager->get(\Magento\SalesRule\Model\ResourceModel\Rule::class)->save($salesRule);
43 
44 // Create coupon and assign "15$ fixed discount" rule to this coupon.
45 $coupon = $objectManager->create(Coupon::class);
46 $coupon->setRuleId($salesRule->getId())
47  ->setCode('CART_FIXED_DISCOUNT_15')
48  ->setType(0);
49 $objectManager->get(CouponRepositoryInterface::class)->save($coupon);