0

I'm trying to create catalog rule by installing the module, but it's not working. This is my CreateRule\Setup\InstallData.php file

<?php
namespace Perspective\CreateRule\Setup;

use Exception;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallData implements InstallDataInterface
{
    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var \Magento\CatalogRule\Model\RuleFactory
     */
    private $ruleFactory;

    /**
     * @var \Magento\CatalogRule\Model\CatalogRuleRepository
     */
    private $catalogRuleRepository;

    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
        \Magento\CatalogRule\Model\RuleFactory $ruleFactory,
        \Magento\CatalogRule\Model\CatalogRuleRepository $catalogRuleRepository
    )
    {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->ruleFactory = $ruleFactory;
        $this->catalogRuleRepository = $catalogRuleRepository;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->moduleDataSetup->startSetup();
        
        $rule = $this->ruleFactory->create()
        ->setName('PriceDiscountCron Rule')
        ->setDescription('Rule from Price Discount Module')
        ->setIsActive(0) 
        ->setCustomerGroupIds(array(1))
        ->setWebsiteIds(array(1)) 
        ->setFromDate('') 
        ->setToDate('') 
        ->setSimpleAction('by_percent') 
        ->setDiscountAmount(10) 
        ->setStopRulesProcessing(0);

        try {
            $this->catalogRuleRepository->save($rule);
        } catch (Exception $e) {                    
           echo $e->getMessage();
        }

        $this->moduleDataSetup->endSetup();
    }
}

I found out new error

enter image description here

1 Answer 1

0

Please try below code.

Create a new data patch PHP file in your module's.

file path:- Vendor/Module/Setup/Patch/Data/CatalogPriceRule.php

<?php

namespace Vendor/Module/Setup/Patch/Data;

use Magento\CatalogRule\Api\Data\RuleInterfaceFactory; use Magento\CatalogRule\Model\RuleFactory; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class CreateCatalogPriceRule implements DataPatchInterface, PatchRevertableInterface {
    /**
     * @var RuleFactory
     */
    private $ruleFactory;

    /**
     * @var RuleInterfaceFactory
     */
    private $ruleDataFactory;

    public function __construct(
        RuleFactory $ruleFactory,
        RuleInterfaceFactory $ruleDataFactory
    ) {
        $this->ruleFactory = $ruleFactory;
        $this->ruleDataFactory = $ruleDataFactory;
    }

    public function apply()
    {
        $ruleData = [
            'name' => 'New Catalog Price Rule',
            'description' => 'Description of the new catalog price rule',
            'is_active' => 1,
            'website_ids' => [1],
            'customer_group_ids' => [1],
            'from_date' => '2023-05-15',
            'to_date' => '2023-05-31',
            'simple_action' => 'by_percent',
            'discount_amount' => 10,        ];

        $rule = $this->ruleFactory->create();

        $ruleDataObject = $this->ruleDataFactory->create(['data' => $ruleData]);        $rule->setData($ruleDataObject->getData());         $rule->save();
    }

    public function revert()
    {
        // code to revert the changes made in apply()
    }  }

please run this command:- php bin/magento s:up

2
  • Now i have this problem Unable to apply data patch Perspective\CreateRule\Setup\Patch\Data\CreateRule for module Perspective_CreateRule. Original exception message: Area code is not set
    – MaxLogic
    Commented May 15, 2023 at 11:34
  • Upd: I solved this by adding $this->appState->setAreaCode('adminhtml'); to apply function, where $this->appState is \Magento\Framework\App\State class
    – MaxLogic
    Commented May 15, 2023 at 11:40

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.