Skip to main content
3 of 3
deleted 180 characters in body

check the below code

In controller:

<?php

namespace Softadroit\Prescription\Controller\Index;

class Save extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_pageFactory          = $pageFactory;
        $this->cartFactory           = $cartFactory;
        $this->resultRedirectFactory = $resultRedirectFactory;

        return parent::__construct($context);
    }

    public function execute()
    {
        $post = $this->getRequest()->getPost();

        /* echo "<pre>";
        print_r($post);
        die(); */

        if (!empty($post)) {
            foreach ($post['name'] as $key => $name) {
                $age         = $post['age'][$key];
                $weight      = $post['weight'][$key];
                $height      = $post['height'][$key];
                $gender      = $post['subject_gender'][$key];
                $product_id  = $post['product_id'][$key];
                $customer_id = $post['customer_id'][$key];

                $model = $this->_objectManager->create('Softadroit\Prescription\Model\Prescription');

                $model->setData([
                    "name"        => $name,
                    "age"         => $age,
                    "weight"      => $weight,
                    "height"      => $height,
                    "gender"      => $gender,
                    "product_id"  => $product_id,
                    "customer_id" => $customer_id,
                ]);

                $msg  = "";
                $data = $model->save();
                if ($data != '') {
//$model->save();
                    $this->messageManager->addSuccessMessage('Thanks your form is submitted successfully!');
                } else {
                    $this->messageManager->addSuccessMessage('Error in submitting data. Please contact on this email!');
                }
            }
            echo $msg;

            $cartObject = $this->cartFactory->create()->truncate();
            $cartObject->saveQuote();

        }

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('prescription/index/success');
        return $resultRedirect;
    }
}