0

Can any one guide me (or) provide me the steps to create customer segment & cart rule programmatically?

If it is Via command line (or) via setup script (or) custom script executing from root folder.

Thanks in advance.

0

Here is the answer i am posting (Test Script), i believe it will help others. Please make sure validate the data with print_r($data) before save.

CUSTOMER SEGMENT:

<?php
error_reporting(1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
try{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSegment = $objectManager->create('Magento\CustomerSegment\Model\Segment');
    $data = [

        'name' => 'demo segement rule',
        'description' => 'demo segement rule',
        'website_ids' => [
           
                            '0' => '2'
                          ],
        'is_active' => '1',
        'conditions' => [

            '1' =>  [
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Combine\Root',
                    'aggregator' => 'all',
                    'value' => '1',
                    'new_child' => ' '
                    ],
            '1--1' => [
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Combine',
                    'aggregator' => 'any',
                    'value' => '1',
                    'new_child' => ' '
                ],

            '1--1--1' =>[
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Product\Combine\History',
                    'operator' => '==',
                    'value' => 'ordered_history',
                    'aggregator' => 'all',
                    'new_child' => ' '
                ],

            '1--1--1--1' => [
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Product\Attributes',
                    'attribute' => 'sku',
                    'operator' => '==',
                    'value' => 'SKU-ABC'
                ],

            '1--1--2' => [                
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Customer\Attributes',
                    'attribute' => 'purchased_igs',
                    'operator' => '==',
                    'value' => [
                            '0' => '123456'//attribute option value
                        ]

                ],
            '1--3' => [
                    'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Customer\Attributes',
                    'attribute' => 'allow_repurchase',
                    'operator' => '==',
                    'value' => '1'//attribute option value
                ]
        ]

    ];

    $validateResult = $customerSegment->validateData(new \Magento\Framework\DataObject($data));
    if ($validateResult !== true) {
        foreach ($validateResult as $errorMessage) {
                 echo $errorMessage;
          }               
    }
    $customerSegment->loadPost($data);
    $customerSegment->save();
    echo "### SEGMENT ID###".$customerSegment->getId()."\n";    

}catch(\Exception $e){
    echo "Please check the error & try again ".$e->getMessage();
}

?>

CART RULE:

<?php
error_reporting(1);
ini_set('max_execution_time', 0);

use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
try{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$ruleModel = $objectManager->create('Magento\SalesRule\Model\Rule');
$conditions = [
          'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Combine',
          'attribute' => null,
          'operator' => null,
          'value' => '1',
          'is_value_processed' => null,
          'aggregator' => 'all',
          'conditions' => 
          [
            '0' => 
            [
              'type' => 'Magento\\CustomerSegment\\Model\\Segment\\Condition\\Segment',
              'attribute' => false,
              'operator' => '==',
              'value' => '1',//Segment Id
              'is_value_processed' => false,
            ],
          ],
        ];
$conditions_serialized= json_encode($conditions);
$actions = [
  'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Combine',
  'attribute' => null,
  'operator' => null,
  'value' => '1',
  'is_value_processed' => null,
  'aggregator' => 'all',
  'conditions' => 
       [
            '0' => 
                [
                  'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product',
                  'attribute' => 'sku',
                  'operator' => '==',
                  'value' => 'SKU-ABC',
                  'is_value_processed' => false,
                  'attribute_scope' => ''
                ],
          ],
        ];
$actions_serialized = json_encode($actions);
//Required data
$data = [
   
    'name' => 'Demo Cart Rule',
    'description' => 'Demo Cart Rule',
    'from_date' => date("Y-m-d"),
    'to_date' => '',
    'uses_per_customer' => '0',
    'is_active' => '1',
    'conditions_serialized' => $conditions_serialized,
    'actions_serialized' => $actions_serialized,
    'stop_rules_processing' => '0',
    'is_advanced' => '1',
    'sort_order' => '1',
    'simple_action' => 'by_percent',
    'discount_amount' => '25',
    'discount_qty' => '0',
    'discount_step' => '0',
    'apply_to_shipping' => '0',
    'is_rss' => '0',
    'coupon_type' => '1',
    'use_auto_generation' => '0',
    'uses_per_coupon' => '0',
    'simple_free_shipping'=> '0',
    'created_in' => '1',
    'updated_in' => '2147483647',
    'is_digital_rule' => '0',
    'email_template' => ' ',
    'number_of_codes' => '0',
    'website_ids' => [
            '0' => '2'
        ],
    //Assigne customer groups here
    'customer_group_ids' => [
            '0' => '0',
            '1' => '1',
            '2' => '2',
            '3' => '3',
            '4' => '4',
            '5' => '5',
            '6' => '6',
            '7' => '8',
            '8' => '9',
            '9' => '10',
            '10' => '26',
            '11' => '38',
            '12' => '41',
            '13' => '47'
        ],
    'reward_points_delta' => '0',
    'coupon_code' => ' ',
    'store_labels' => [
                        '0' => ' '
                      ],
    'conditions' => [
                        '1' =>[
                                'type' => 'Magento\SalesRule\Model\Rule\Condition\Combine',
                                'aggregator' => 'all',
                                'value' => '1',
                                'new_child' => ' '
                            ],
                        '1--1' =>
                            [
                                'type' => 'Magento\CustomerSegment\Model\Segment\Condition\Segment',
                                'operator' => '==',
                                'value' => '188'//Segment Id
                            ]

                    ],
    'actions' => [
                   '1' =>
                        [
                            'type' => 'Magento\SalesRule\Model\Rule\Condition\Product\Combine',
                            'aggregator' => 'all',
                            'value' => '1',
                            'new_child' => ' '
                        ],
                    '1--1' => 
                        [
                            'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
                            'attribute' => 'sku',                           
                            'operator' => '==',                         
                            'attribute_scope' => '',
                            'value' => 'SKU-ABC',
                        ]
                ]
];

$validateResult = $ruleModel->validateData(new \Magento\Framework\DataObject($data));
    if ($validateResult !== true) {
        foreach ($validateResult as $errorMessage) {
                 echo $errorMessage;
          }               
    }
$ruleModel->loadPost($data);
$ruleModel->save();
echo "### Cart rule ####".$ruleModel->getId();
}catch(\Exception $e){

}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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