Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
coupon_code_with_wildcard.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' => '5$ 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  'simple_action' => Rule::CART_FIXED_ACTION,
27  'discount_amount' => 5,
28  'discount_step' => 0,
29  'stop_rules_processing' => 1,
30  'website_ids' => [
31  $objectManager->get(StoreManagerInterface::class)->getWebsite()->getId(),
32  ],
33  ]
34 );
35 $objectManager->get(\Magento\SalesRule\Model\ResourceModel\Rule::class)->save($salesRule);
36 
37 // Create coupon and assign "15$ fixed discount" rule to this coupon.
38 $coupon = $objectManager->create(Coupon::class);
39 $coupon->setRuleId($salesRule->getId())
40  ->setCode('2?ds5!2d')
41  ->setType(0);
42 
44 $couponRepository = $objectManager->get(CouponRepositoryInterface::class);