Magento 2.2.5 : Save Catalog Rule ConditionsMagento 2 Create Catalog Price Rule ProgrammaticallyCatalog Rules...

Who is this Ant Woman character in this image alongside the Wasp?

How can animals be objects of ethics without being subjects as well?

What are "industrial chops"?

If I delete my router's history can my ISP still provide it to my parents?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

Why zero tolerance on nudity in space?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

初めてです, is '初めて' an adverb?

How to limit sight distance to 1 KM

Why would space fleets be aligned?

Why avoid shared user accounts?

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Finding a mistake using Mayer-Vietoris

Simple text-based tic-tac-toe

Can you share a component pouch with another creature?

How to prevent users from executing commands through browser URL

Can a person refuse a presidential pardon?

How much mayhem could I cause as a sentient fish?

How to say "Brexit" in Latin?

Why do neural networks need so many training examples to perform?

awk + sum all numbers

Does SQL Server 2017, including older versions, support 8k disk sector sizes?

Early credit roll before the end of the film

what does しにみえてる mean?



Magento 2.2.5 : Save Catalog Rule Conditions


Magento 2 Create Catalog Price Rule ProgrammaticallyCatalog Rules Don't Get Applied on Product SaveCatalog Rule Round rule_price of catalogrule_product_priceHow does Magento save catalog rule website data into relational tables?Magento 2: catalog rule takes too long to saveCustom Product Attribute not applying in Promo RulesMagento 2 Create Catalog Price Rule ProgrammaticallyMagento 2 : Notice Error - Array to String conversionMagento 1.9 - issue creating coupon rule. An error occurred while saving the rule dataMagento 2.2.5 : How to place a check on prouduct page on product attributesMagento 2.1.11 - adminhtml - Saving model to database with boolean field does not match value













0















I am trying to save catalog rule condtions from my custom module, below is the array I get in my controller save.php



[rule] => Array
(
[conditions] => Array
(
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[value] => 1
[new_child] =>
)

[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
[operator] => ==
[value] => 4
)

)

)


The array is further modified to conditions by doing:



if (isset($data['rule'])) {
$data['conditions'] = $data['rule']['conditions'];
unset($data['rule']);
}


My database table column for conditions is conditions_serialized as it is in catalogrule table. Now my question is what are the steps I am missing in between -- because when I save a record I get the error:




Something went wrong while saving the record !




Any one who has implemented this might be having a better insight in to the problem , please advise .










share|improve this question

























  • Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

    – LAW
    Jul 26 '18 at 7:36











  • @LAW Yes it is same.

    – Verdu
    Jul 26 '18 at 7:48











  • If you try printing array in the core controller with save action can you check if it reaches there?

    – LAW
    Jul 26 '18 at 7:54











  • I am saving this with my own controller and own ui form , the values are coming form my own custom form.

    – Verdu
    Jul 26 '18 at 7:56











  • magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

    – LAW
    Jul 26 '18 at 8:00
















0















I am trying to save catalog rule condtions from my custom module, below is the array I get in my controller save.php



[rule] => Array
(
[conditions] => Array
(
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[value] => 1
[new_child] =>
)

[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
[operator] => ==
[value] => 4
)

)

)


The array is further modified to conditions by doing:



if (isset($data['rule'])) {
$data['conditions'] = $data['rule']['conditions'];
unset($data['rule']);
}


My database table column for conditions is conditions_serialized as it is in catalogrule table. Now my question is what are the steps I am missing in between -- because when I save a record I get the error:




Something went wrong while saving the record !




Any one who has implemented this might be having a better insight in to the problem , please advise .










share|improve this question

























  • Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

    – LAW
    Jul 26 '18 at 7:36











  • @LAW Yes it is same.

    – Verdu
    Jul 26 '18 at 7:48











  • If you try printing array in the core controller with save action can you check if it reaches there?

    – LAW
    Jul 26 '18 at 7:54











  • I am saving this with my own controller and own ui form , the values are coming form my own custom form.

    – Verdu
    Jul 26 '18 at 7:56











  • magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

    – LAW
    Jul 26 '18 at 8:00














0












0








0








I am trying to save catalog rule condtions from my custom module, below is the array I get in my controller save.php



[rule] => Array
(
[conditions] => Array
(
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[value] => 1
[new_child] =>
)

[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
[operator] => ==
[value] => 4
)

)

)


The array is further modified to conditions by doing:



if (isset($data['rule'])) {
$data['conditions'] = $data['rule']['conditions'];
unset($data['rule']);
}


My database table column for conditions is conditions_serialized as it is in catalogrule table. Now my question is what are the steps I am missing in between -- because when I save a record I get the error:




Something went wrong while saving the record !




Any one who has implemented this might be having a better insight in to the problem , please advise .










share|improve this question
















I am trying to save catalog rule condtions from my custom module, below is the array I get in my controller save.php



[rule] => Array
(
[conditions] => Array
(
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[value] => 1
[new_child] =>
)

[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
[operator] => ==
[value] => 4
)

)

)


The array is further modified to conditions by doing:



if (isset($data['rule'])) {
$data['conditions'] = $data['rule']['conditions'];
unset($data['rule']);
}


My database table column for conditions is conditions_serialized as it is in catalogrule table. Now my question is what are the steps I am missing in between -- because when I save a record I get the error:




Something went wrong while saving the record !




Any one who has implemented this might be having a better insight in to the problem , please advise .







database catalog-price-rules catalog-rules






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 26 '18 at 6:55







Verdu

















asked Jul 26 '18 at 6:45









VerduVerdu

1,066523




1,066523













  • Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

    – LAW
    Jul 26 '18 at 7:36











  • @LAW Yes it is same.

    – Verdu
    Jul 26 '18 at 7:48











  • If you try printing array in the core controller with save action can you check if it reaches there?

    – LAW
    Jul 26 '18 at 7:54











  • I am saving this with my own controller and own ui form , the values are coming form my own custom form.

    – Verdu
    Jul 26 '18 at 7:56











  • magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

    – LAW
    Jul 26 '18 at 8:00



















  • Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

    – LAW
    Jul 26 '18 at 7:36











  • @LAW Yes it is same.

    – Verdu
    Jul 26 '18 at 7:48











  • If you try printing array in the core controller with save action can you check if it reaches there?

    – LAW
    Jul 26 '18 at 7:54











  • I am saving this with my own controller and own ui form , the values are coming form my own custom form.

    – Verdu
    Jul 26 '18 at 7:56











  • magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

    – LAW
    Jul 26 '18 at 8:00

















Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

– LAW
Jul 26 '18 at 7:36





Try by printing array from saving catalog rule by admin, and see is the array thus formed is same as your array. The issue can be due to missing array entries needed in the db.

– LAW
Jul 26 '18 at 7:36













@LAW Yes it is same.

– Verdu
Jul 26 '18 at 7:48





@LAW Yes it is same.

– Verdu
Jul 26 '18 at 7:48













If you try printing array in the core controller with save action can you check if it reaches there?

– LAW
Jul 26 '18 at 7:54





If you try printing array in the core controller with save action can you check if it reaches there?

– LAW
Jul 26 '18 at 7:54













I am saving this with my own controller and own ui form , the values are coming form my own custom form.

– Verdu
Jul 26 '18 at 7:56





I am saving this with my own controller and own ui form , the values are coming form my own custom form.

– Verdu
Jul 26 '18 at 7:56













magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

– LAW
Jul 26 '18 at 8:00





magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?

– LAW
Jul 26 '18 at 8:00










1 Answer
1






active

oldest

votes


















0














You have to use the loadPost function of the model.



$model->loadPost($data)->save();



An example is:



 /**
* Save action
*
* @return MagentoFrameworkControllerResultInterface
*/
public function execute() {
$data = $this->getRequest()->getPostValue();
/** @var MagentoBackendModelViewResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {

/** @var VendorModuleModelExample $model */
$model = $this->_objectManager->create('EntrepidsWarrantyModelWarranty');

$id = $this->getRequest()->getParam('example_id');
if ($id) {
$model->load($id);
}

if (isset($data['rule'])) {
$data['conditions'] = $data['rule']['conditions'];
unset($data['rule']);
}

try {
$model->loadPost($data)->save();
$this->messageManager->addSuccess(__('You saved this warranty.'));
$this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
}
return $resultRedirect->setPath('*/*/');
} catch (MagentoFrameworkExceptionLocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the warranty.'));
}

$this->_getSession()->setFormData($data);
return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $this->getRequest()->getParam('warranty_id')]);
}
return $resultRedirect->setPath('*/*/');
}




share























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236006%2fmagento-2-2-5-save-catalog-rule-conditions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You have to use the loadPost function of the model.



    $model->loadPost($data)->save();



    An example is:



     /**
    * Save action
    *
    * @return MagentoFrameworkControllerResultInterface
    */
    public function execute() {
    $data = $this->getRequest()->getPostValue();
    /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    if ($data) {

    /** @var VendorModuleModelExample $model */
    $model = $this->_objectManager->create('EntrepidsWarrantyModelWarranty');

    $id = $this->getRequest()->getParam('example_id');
    if ($id) {
    $model->load($id);
    }

    if (isset($data['rule'])) {
    $data['conditions'] = $data['rule']['conditions'];
    unset($data['rule']);
    }

    try {
    $model->loadPost($data)->save();
    $this->messageManager->addSuccess(__('You saved this warranty.'));
    $this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
    if ($this->getRequest()->getParam('back')) {
    return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
    }
    return $resultRedirect->setPath('*/*/');
    } catch (MagentoFrameworkExceptionLocalizedException $e) {
    $this->messageManager->addError($e->getMessage());
    } catch (RuntimeException $e) {
    $this->messageManager->addError($e->getMessage());
    } catch (Exception $e) {
    $this->messageManager->addException($e, __('Something went wrong while saving the warranty.'));
    }

    $this->_getSession()->setFormData($data);
    return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $this->getRequest()->getParam('warranty_id')]);
    }
    return $resultRedirect->setPath('*/*/');
    }




    share




























      0














      You have to use the loadPost function of the model.



      $model->loadPost($data)->save();



      An example is:



       /**
      * Save action
      *
      * @return MagentoFrameworkControllerResultInterface
      */
      public function execute() {
      $data = $this->getRequest()->getPostValue();
      /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
      $resultRedirect = $this->resultRedirectFactory->create();
      if ($data) {

      /** @var VendorModuleModelExample $model */
      $model = $this->_objectManager->create('EntrepidsWarrantyModelWarranty');

      $id = $this->getRequest()->getParam('example_id');
      if ($id) {
      $model->load($id);
      }

      if (isset($data['rule'])) {
      $data['conditions'] = $data['rule']['conditions'];
      unset($data['rule']);
      }

      try {
      $model->loadPost($data)->save();
      $this->messageManager->addSuccess(__('You saved this warranty.'));
      $this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
      if ($this->getRequest()->getParam('back')) {
      return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
      }
      return $resultRedirect->setPath('*/*/');
      } catch (MagentoFrameworkExceptionLocalizedException $e) {
      $this->messageManager->addError($e->getMessage());
      } catch (RuntimeException $e) {
      $this->messageManager->addError($e->getMessage());
      } catch (Exception $e) {
      $this->messageManager->addException($e, __('Something went wrong while saving the warranty.'));
      }

      $this->_getSession()->setFormData($data);
      return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $this->getRequest()->getParam('warranty_id')]);
      }
      return $resultRedirect->setPath('*/*/');
      }




      share


























        0












        0








        0







        You have to use the loadPost function of the model.



        $model->loadPost($data)->save();



        An example is:



         /**
        * Save action
        *
        * @return MagentoFrameworkControllerResultInterface
        */
        public function execute() {
        $data = $this->getRequest()->getPostValue();
        /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {

        /** @var VendorModuleModelExample $model */
        $model = $this->_objectManager->create('EntrepidsWarrantyModelWarranty');

        $id = $this->getRequest()->getParam('example_id');
        if ($id) {
        $model->load($id);
        }

        if (isset($data['rule'])) {
        $data['conditions'] = $data['rule']['conditions'];
        unset($data['rule']);
        }

        try {
        $model->loadPost($data)->save();
        $this->messageManager->addSuccess(__('You saved this warranty.'));
        $this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
        if ($this->getRequest()->getParam('back')) {
        return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
        }
        return $resultRedirect->setPath('*/*/');
        } catch (MagentoFrameworkExceptionLocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
        } catch (RuntimeException $e) {
        $this->messageManager->addError($e->getMessage());
        } catch (Exception $e) {
        $this->messageManager->addException($e, __('Something went wrong while saving the warranty.'));
        }

        $this->_getSession()->setFormData($data);
        return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $this->getRequest()->getParam('warranty_id')]);
        }
        return $resultRedirect->setPath('*/*/');
        }




        share













        You have to use the loadPost function of the model.



        $model->loadPost($data)->save();



        An example is:



         /**
        * Save action
        *
        * @return MagentoFrameworkControllerResultInterface
        */
        public function execute() {
        $data = $this->getRequest()->getPostValue();
        /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {

        /** @var VendorModuleModelExample $model */
        $model = $this->_objectManager->create('EntrepidsWarrantyModelWarranty');

        $id = $this->getRequest()->getParam('example_id');
        if ($id) {
        $model->load($id);
        }

        if (isset($data['rule'])) {
        $data['conditions'] = $data['rule']['conditions'];
        unset($data['rule']);
        }

        try {
        $model->loadPost($data)->save();
        $this->messageManager->addSuccess(__('You saved this warranty.'));
        $this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
        if ($this->getRequest()->getParam('back')) {
        return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
        }
        return $resultRedirect->setPath('*/*/');
        } catch (MagentoFrameworkExceptionLocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
        } catch (RuntimeException $e) {
        $this->messageManager->addError($e->getMessage());
        } catch (Exception $e) {
        $this->messageManager->addException($e, __('Something went wrong while saving the warranty.'));
        }

        $this->_getSession()->setFormData($data);
        return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $this->getRequest()->getParam('warranty_id')]);
        }
        return $resultRedirect->setPath('*/*/');
        }





        share











        share


        share










        answered 1 min ago









        Miguel Ángel Garrido GutiérrezMiguel Ángel Garrido Gutiérrez

        112




        112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236006%2fmagento-2-2-5-save-catalog-rule-conditions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

            How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

            夢乃愛華...