1

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] => Magento\CatalogRule\Model\Rule\Condition\Combine
                        [aggregator] => all
                        [value] => 1
                        [new_child] => 
                    )

                [1--1] => Array
                    (
                        [type] => Magento\CatalogRule\Model\Rule\Condition\Product
                        [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 .

5
  • 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.
    – Gagan
    Commented Jul 26, 2018 at 7:36
  • @LAW Yes it is same.
    – Verdu
    Commented Jul 26, 2018 at 7:48
  • If you try printing array in the core controller with save action can you check if it reaches there?
    – Gagan
    Commented Jul 26, 2018 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
    Commented Jul 26, 2018 at 7:56
  • magento.stackexchange.com/questions/156119/… Compared your array with this, seemed slightly different can you remove the difference and hence try?
    – Gagan
    Commented Jul 26, 2018 at 8:00

2 Answers 2

1

You have to use the loadPost function of the model.

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

An example is:

 /**
 * Save action
 *
 * @return \Magento\Framework\Controller\ResultInterface
 */
public function execute() {
    $data = $this->getRequest()->getPostValue();
    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    if ($data) {

        /** @var \Vendor\Module\Model\Example $model */
        $model = $this->_objectManager->create('Entrepids\Warranty\Model\Warranty');

        $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('Magento\Backend\Model\Session')->setFormData(false);
            if ($this->getRequest()->getParam('back')) {
                return $resultRedirect->setPath('*/*/edit', ['warranty_id' => $model->getId(), '_current' => true]);
            }
            return $resultRedirect->setPath('*/*/');
        } catch (\Magento\Framework\Exception\LocalizedException $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('*/*/');
}
0

Follow the Below link is a full Example.

Click Here For Part 1

Click Here For Part 2

Click Here For Part 3

Check condition with products in Magento 2

I Hope This Helps You.

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.