in a magento 2.2 based site we need to programmatically create a coupon code for . However was only able to find one code sample and it does not seem to work. Meaning no coupon code is created and when I look in the exception.log file I see the below error message

> 
> main.CRITICAL: Notice: Undefined property: Smartwave\Porto\Block\Template::$objectManager in /var/www/html/\*\*\*\*.com/vendor/magento/framework/View/TemplateEngine/Php.php on line 110 {"exception":"[object] (Exception(code: 0): 
> 
> 
> 

Below is the code I am trying to use. Also as a side note the coupon code is created in the admin panel as a cart price rule.

```
//lets create a coupon code
$coupon['name'] = 'vip_signup';
$coupon['desc'] = 'Discount for vip signup.';
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = '1';
$coupon['discount_type'] ='by_percent';
$coupon['discount_amount'] ='15';
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = '1';
$coupon['code'] ='NL01-1234'; //this code will normally be autogenetated but i am hard coding for testing purposes

$shoppingCartPriceRule = $this->objectManager->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);

return $shoppingCartPriceRule->save();
       $shoppingCartPriceRule->unsetData(); 

```