系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中以编程方式创建优惠券代码的步骤:
步骤1: 您需要在Magento根目录上创建一个根脚本文件。
文件路径:Root Directory\pub\Customcoupon.php
现在添加以下代码
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); $coupon['name'] = 'custom_coupon'; $coupon['desc'] = ' 10% Off Discount coupon.'; $coupon['start'] = date('Y-m-d'); $coupon['end'] = ''; $coupon['max_redemptions'] = 1; $coupon['discount_type'] ='by_percent'; $coupon['discount_amount'] = 10; $coupon['flag_is_free_shipping'] = 'no'; $coupon['redemptions'] = 1; $coupon['code'] ='10%OFF'; $shoppingCartPriceRule = $obj->create('Magento\SalesRule\Model\Rule'); $shoppingCartPriceRule->setName($coupon['name']) ->setDescription($coupon['desc']) ->setFromDate($coupon['start']) ->setToDate($coupon['end']) ->setUsesPerCustomer($coupon['max_redemptions']) ->setCustomerGroupIds(array('0','1','2','3',)) ->setIsActive(1) ->setSimpleAction($coupon['discount_type']) ->setDiscountAmount($coupon['discount_amount']) ->setDiscountQty(1) ->setApplyToShipping($coupon['flag_is_free_shipping']) ->setTimesUsed($coupon['redemptions']) ->setWebsiteIds(array('1')) ->setCouponType(2) ->setCouponCode($coupon['code']) ->setUsesPerCoupon(NULL); $shoppingCartPriceRule->save();
现在,从Magento 2管理面板中检查优惠券代码。
优惠券代码
结论:
这样,您可以轻松地在Magento 2中以编程方式创建优惠券代码。