discount_10percent.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /**
  3. * SalesRule 10% discount coupon
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  9. /** @var \Magento\SalesRule\Model\RuleFactory $salesRule */
  10. $salesRuleFactory = $objectManager->get(\Magento\SalesRule\Model\RuleFactory::class);
  11. /** @var \Magento\SalesRule\Model\Rule $salesRule */
  12. $salesRule = $salesRuleFactory->create();
  13. $data = [
  14. 'name' => 'Test Coupon',
  15. 'is_active' => true,
  16. 'website_ids' => [
  17. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  18. \Magento\Store\Model\StoreManagerInterface::class
  19. )->getStore()->getWebsiteId()
  20. ],
  21. 'customer_group_ids' => [\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID],
  22. 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC,
  23. 'coupon_code' => uniqid(),
  24. 'simple_action' => \Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION,
  25. 'discount_amount' => 10,
  26. 'discount_step' => 1
  27. ];
  28. $salesRule->loadPost($data)->setUseAutoGeneration(false)->save();
  29. $objectManager->get(\Magento\Framework\Registry::class)->unregister('Magento/Checkout/_file/discount_10percent');
  30. $objectManager->get(\Magento\Framework\Registry::class)
  31. ->register('Magento/Checkout/_file/discount_10percent', $salesRule->getRuleId());