7

I have used the below code to create catalog price rule programmatically. It creates the rule,but the conditions are not saved.

$catalogPriceRule = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
$catalogPriceRule
    ->setName('name')
    ->setDescription('description')
    ->setIsActive(1)
    ->setCustomerGroupIds(array(1))
    ->setWebsiteIds(array(1))
    ->setFromDate('')
    ->setToDate('')
    ->setSimpleAction('by_fixed')
    ->setDiscountAmount(10)
    ->setStopRulesProcessing(0);

$conditions = array();
$conditions[1] = array(
    'type' => 'catalogrule/rule_condition_combine',
    'aggregator' => 'any',
    'value' => "1",
    'new_child' => ''
);

$i = 1;
$conditions['1--1'] = array(
    'type' => 'catalogrule/rule_condition_product_found',
    'value' => 1,
    'aggregator' => 'all',
    'new_child' => '',
);

$conditions['1--1--1'] = array(
    'type' => 'catalogrule/rule_condition_product',
    'attribute' => 'sku',
    'operator' => '==',
    'value' => '24-UB02',
);
$catalogPriceRule->setData('conditions',$conditions);
$catalogPriceRule->loadPost($catalogPriceRule->getData());
$catalogPriceRule->save();
$catalogPriceRule->applyAll();
3
  • Can u explain what condition are you try to build?
    – Bhavik
    Jan 24, 2017 at 7:55
  • Want to add some skus in the condition. Jan 24, 2017 at 7:56
  • How can i create cart rule with condition?
    – senthil
    Mar 12, 2019 at 7:38

1 Answer 1

3

check below Code.

Reference: \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog\Save

Before saving rule validate data It's helping for saving Correct value

$model = $objectManager->create('Magento\CatalogRule\Model\Rule');
$model->setName('name') 
    ->setDescription('description') 
    ->setIsActive(1) 
    ->setCustomerGroupIds(array(1))
    ->setWebsiteIds(array(1)) 
    ->setFromDate('') 
    ->setToDate('') 
    ->setSimpleAction('by_fixed') 
    ->setDiscountAmount(10) 
    ->setStopRulesProcessing(0);  

$conditions = array();
$conditions["1"] = array
        (
            "type" => "Magento\CatalogRule\Model\Rule\Condition\Combine",
            "aggregator" => "all",
            "value" => 1,
            "new_child" => ""
        );
$conditions["1--1"] = array
        (
            "type" => "Magento\CatalogRule\Model\Rule\Condition\Product",
            "attribute" => "sku",
            "operator" => "==",
            "value" => "24-UB02"
        );

$model->setData('conditions',$conditions);

// Validating rule data before Saving
$validateResult = $model->validateData(new \Magento\Framework\DataObject($model->getData()));
if ($validateResult !== true) {
    foreach ($validateResult as $errorMessage) {
        echo $errorMessage;
    }                                       
    return;
}

try {                   
    $model->loadPost($model->getData());
    $model->save(); 

    $ruleJob = $objectManager->get('Magento\CatalogRule\Model\Rule\Job');
    $ruleJob->applyAll();
    echo "rule created";
} catch (Exception $e) {                    
   echo $e->getMessage();
}

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.