0

Function to generate cart price rule

public function generateCoupon($value='')
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$coupon['name']     =   'PROMO1';
$coupon['desc']     =   'Promotion one';
$coupon['start']    =   date('Y-m-01');
$coupon['end']      =   date('Y-m-t');
$coupon['max_redemptions']  =   1;
$coupon['discount_type']    =   'by_percent';
$coupon['discount_amount']  =   30;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions']      =   1;
$coupon['code']             =   "PROMOPROMO2";
$coupon['store_labels']     =   '3k-4.9k 30% discount'; //this code will normally be autogenetated but i am hard coding for testing purposes

$shoppingCartPriceRule = $objectManager->create('Magento\SalesRule\Model\Rule');
$shoppingCartPrice = $shoppingCartPriceRule->setName($coupon['name'])
        ->setDescription($coupon['desc'])
        ->setFromDate($coupon['start'])
        ->setToDate($coupon['end'])
        ->setUsesPerCustomer($coupon['max_redemptions'])
        ->setCustomerGroupIds(array('1',))
        ->setIsActive(1)
        ->setSimpleAction($coupon['discount_type'])
        ->setDiscountAmount($coupon['discount_amount'])
        ->setDiscountQty(1)
        ->setApplyToShipping($coupon['flag_is_free_shipping'])
        ->setTimesUsed($coupon['redemptions'])
        ->setStoreLabels(array('40% Discount For First Order','40% Discount For First Order'))
        ->setWebsiteIds(array('1'))
        ->setCouponType(2)
        ->setCouponCode($coupon['code'])
        ->setUsesPerCoupon(NULL);
        $shoppingCartPriceRule->save();}

The above function create coupon code successfully. But there is a condition, I have to add a condition that order amount is greater than 5000. Then only coupon is accessible. How can i add this condition?

1 Answer 1

2

Try following way:


/**
 * @var \Magento\SalesRule\Model\RuleFactory
 */
protected $ruleFactory;
/**
 * @var \Magento\SalesRule\Model\Rule\Condition\AddressFactory
 */
protected $addressFactory;

public function __construct(
    \Magento\SalesRule\Model\RuleFactory $ruleFactory,
    \Magento\SalesRule\Model\Rule\Condition\AddressFactory $addressFactory
) {
    $this->ruleFactory = $ruleFactory;
    $this->addressFactory = $addressFactory;
}

And now


public function addSalesRule()
{
    $sku = '24-WG085';
    $shoppingCartPriceRule = $this->ruleFactory->create();

    $shoppingCartPriceRule->setName('Add a second for $1 - ' . $sku)
        ->setDescription('Buy one item at regular price, and receive a second item for just $1.00 more!')
        ->setFromDate('2000-01-01')
        ->setToDate(NULL)
        ->setUsesPerCustomer('0')
        ->setCustomerGroupIds(array('0','1','2','3',))
        ->setIsActive('1')
        ->setStopRulesProcessing('0')
        ->setIsAdvanced('1')
        ->setProductIds(NULL)
        ->setSortOrder('1')
        ->setSimpleAction('by_percent')
        ->setDiscountAmount(30)
        ->setDiscountQty(NULL)
        ->setDiscountStep('0')
        ->setApplyToShipping('0')
        ->setTimesUsed('0')
        ->setIsRss('0')
        ->setWebsiteIds(array('1'))
        ->setCouponType('1')
        ->setCouponCode(NULL)
        ->setUsesPerCoupon(NULL);

    // Add Subtotal Condition
    $skuCond = $this->addressFactory->create()
        ->setType('Magento\SalesRule\Model\Rule\Condition\Address')
        ->setAttribute('base_subtotal')
        ->setOperator('>=')
        ->setValue(5000);
    $shoppingCartPriceRule->getConditions()->addCondition($skuCond);
    $shoppingCartPriceRule->save();
}
3
  • is it possible to add a customer session to condition, Because I have two type cusotmer. The coupon is eligible for one type of users.
    – Midlaj
    Commented Feb 26, 2018 at 4:50
  • Can you please answer this question link
    – Midlaj
    Commented Apr 24, 2018 at 4:46
  • @Sohel, can you please check this code, I am trying to same condition applying on this. but not working. magento.stackexchange.com/questions/357162/… Commented Jun 24, 2022 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.