2

Im try to duplicate some salesrule exist, i can duplicate this, but i can't use setConditionsSerialized and setActionsSerialized with getCondition and getActionCondition, its dont working

    use Magento\SalesRule\Model\RuleRepository;
    use Magento\SalesRule\Model\RuleFactory;

        protected $_ruleRepostitory;

        protected $_ruleFactory;

 public function __construct(
        RuleFactory $ruleFactory,
        RuleRepository $ruleRepostitory,
) {
        $this->_ruleRepostitory = $ruleRepostitory;
        $this->_ruleFactory = $ruleFactory;
}

                $id = 1;
                $getRule = $this->_ruleRepostitory->getById($id);
                $ruleModel = $this->_ruleFactory->create();
                $Default = 'ABCZXC';
                $randomNumber = random_int(000, 999);
                $ruleModel->setName($getRule->getName())
                    ->setDescription($getRule->getDescription())
                    ->setCouponType(2)
                    ->setConditionsSerialized(json_encode($getRule->getCondition()))
                    ->setActionsSerialized(json_encode($getRule->getActionCondition()))
                    ->setCouponCode($random = $Default . " - " . $randomNumber)
                    ->setFromDate($getRule->getFromDate())
                    ->setToDate($getRule->getFromDate())
                    ->setUsesPerCustomer($getRule->getUsesPerCustomer())
                    ->setCustomerGroupIds($getRule->getCustomerGroupIds())
                    ->setIsActive($getRule->getIsActive())
                    ->setStopRulesProcessing($getRule->getStopRulesProcessing())
                    ->setIsAdvanced($getRule->getIsAdvanced())
                    ->setProductIds($getRule->getProductIds())
                    ->setSortOrder($getRule->getSortOrder())
                    ->setSimpleAction($getRule->getSimpleAction())
                    ->setDiscountAmount($getRule->getDiscountAmount())
                    ->setDiscountQty($getRule->getDiscountQty())
                    ->setDiscountStep($getRule->getDiscountStep())
                    ->setSimpleFreeShipping($getRule->getSimpleFreeShipping())
                    ->setApplyToShipping($getRule->getApplyToShipping())
                    ->setTimesUsed($getRule->getTimesUsed())
                    ->setIsRss($getRule->getIsRss())
                    ->setWebsiteIds($getRule->getWebsiteIds())
                    ->setUsesPerCoupon($getRule->getUsesPerCoupon());
  $ruleModel->save();
2

Yeah, I did it, this is my code for duplicate Sales Rule . Does anyone have different answer .

use Magento\SalesRule\Model\RuleFactory;

protected $_ruleFactory;

public function __construct(
        RuleFactory $ruleFactory
)
{
        $this->_ruleFactory = $ruleFactory;
        parent::__construct($context);
}

public function execute()
{
    $duplicateRule = $this->_ruleFactory->create();
            $ruleModel = $this->_ruleFactory->create()->load($id);
            $default = 'ABCZXC'; // random code with ABCZXC+xxx, your can Enabled Use Auto Generation if you want
            print_r($ruleModel->getData());
            $randomNumber = random_int(000, 999);
            $duplicateRule
                ->setName($ruleModel->getName())
                ->setDescription($ruleModel->getDescription())
                ->setCouponType($ruleModel->getCouponType())
                ->setConditionsSerialized($ruleModel->getConditionsSerialized())
                ->setActionsSerialized($ruleModel->getActionsSerialized())
                ->setCouponCode($random = $default . " - " . $randomNumber)
                ->setFromDate($ruleModel->getFromDate())
                ->setToDate($ruleModel->getFromDate())
                ->setUsesPerCustomer($ruleModel->getUsesPerCustomer())
                ->setCustomerGroupIds($ruleModel->getCustomerGroupIds())
                ->setIsActive($ruleModel->getIsActive())
                ->setStopRulesProcessing($ruleModel->getStopRulesProcessing())
                ->setIsAdvanced($ruleModel->getIsAdvanced())
                ->setProductIds($ruleModel->getProductIds())
                ->setSortOrder($ruleModel->getSortOrder())
                ->setSimpleAction($ruleModel->getSimpleAction())
                ->setDiscountAmount($ruleModel->getDiscountAmount())
                ->setDiscountQty($ruleModel->getDiscountQty())
                ->setDiscountStep($ruleModel->getDiscountStep())
                ->setSimpleFreeShipping($ruleModel->getSimpleFreeShipping())
                ->setUseAutoGeneration($ruleModel->getUseAutoGeneration())
                ->setApplyToShipping($ruleModel->getApplyToShipping())
                ->setTimesUsed($ruleModel->getTimesUsed())
                ->setIsRss($ruleModel->getIsRss())
                ->setWebsiteIds($ruleModel->getWebsiteIds())
                ->setUsesPerCoupon($ruleModel->getUsesPerCoupon());
            $duplicateRule->save();
}

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.