I create the coupon function in Helper which is
public function setCoupon()
{
// $this->_state->setAreaCode('adminhtml');
// $this->_state->setAreaCode('frontend');
$coupon['name'] = 'Offer';
$coupon['desc'] = 'Discount for vip signup coupon.';
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = 1;
$coupon['discount_type'] ='by_fixed';
$coupon['discount_amount'] = 15;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = 1;
$coupon['code'] ='NL02-1234'; //this code will normally be autogenetated but i am hard coding for testing purposes
$this->_rule->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)
// ->setProductIds(array(1,2,3));
// ->setProductIds(array('1','2','3'))
->setProductIds(1)
->setCouponCode($coupon['code'])
->setUsesPerCoupon(NULL);
$this->_rule->save();
}
where $this->_rule is object of Magento\SalesRule\Model\Rule class it works perfectly but you guys can see that I tried setProductIds to set it for specific products but when it was in array it show me an error Array to String and when I tried setProductIds only for one product Id then it not gives me any error but the coupon works on the every product. How can I set this???