0

Step 1: Here with each product, I want to display a form and store data in the Database.

Step 2: Get all the data with product id and order id in the Admin Panel.

My module PHP file Code

<?php
namespace Softadroit\Prescription\Block;

class Prescription extends \Magento\Framework\View\Element\Template
{
    protected $_orderFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;;
        parent::__construct($context, $data);
    }

    /**
     * Get quote object associated with cart. By default it is current customer session quote
     *
     * @return \Magento\Quote\Model\Quote
     */
    public function getQuoteData()
    {
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }
}
?>

And this is my .phtml file code

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();

foreach($quote->getAllVisibleItems() as $_item) {
    echo 'Name: '.$_item->getName().'<br/>';
    echo 'Quantity: '.$_item->getQty().'<br/>';
    echo 'Price: $'.$_item->getPrice().'<br/>';
    echo "<br/>";
}
7
  • Describe the requirement in some more detail, what you are trying and what is the issue into that one? Commented Aug 21, 2020 at 13:18
  • @DhirenVasoya I want to add a form to collect some data from the customer before checkout. So I have to display form with the individual product added to the checkout. And store that form in the database. I am new to Magento so I don't know how it works Commented Aug 21, 2020 at 13:24
  • Then have you make any thing? Commented Aug 21, 2020 at 13:25
  • No I am looking for, how it can be done Commented Aug 21, 2020 at 13:26
  • For each product, what information you want to take from customer? And from that how much you need to save? Commented Aug 21, 2020 at 13:27

1 Answer 1

0

Use below Code for phtml file:

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>

<div class="row">
    <div class="col-md-8">
        <form name="addData" method="post" id="addData" class="form" action="<?php echo $this->getFormAction(); ?>" data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>" data-mage-init='{"validation":{}}'>
        <?php foreach($quote->getAllVisibleItems() as $_item) { 
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
            $customerId = $block->getLoggedinCustomerId();
            //echo $product->getPrescription(); //die();
            $is_priscription = $product->getData('prescription');
            if($is_priscription != "" && $is_priscription == '5431'){  // change prescription option id here ?>
            <fieldset class="fieldset">
                <legend class="legend"><span>Add Prescription: <?php echo $_item->getName(); ?></span></legend>
                <fieldset class="fieldset row">
                    <div class="fields col-md-6">
                        <div class="field name required">
                            <label class="label" for="name"><span>Name</span></label>
                            <div class="control">
                            <input name="name[]" id="name" title="Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="age"><span>Age</span></label>
                            <div class="control">
                                <input name="age[]" id="Age" title="Age" class="input-text" type="int" data-validate="{required:true}"> Years</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="height"><span>Height</span></label>
                            <div class="control">
                                <input name="height[]" id="Height" title="Height" class="input-text" type="text" data-validate="{required:true}"> FT</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="weight"><span>Weight</span></label>
                            <div class="control">
                                <input name="weight[]" id="Weight" title="Weight" class="input-text" type="int" data-validate="{required:true}"> KG</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="gender"><span>Gender</span></label>
                            <select name="subject_gender[]">
                            <option>---Select Gender--</option>
                            <option value="male">Male</option>
                            <option value="female">Female</option>
                            </select>
                        </div>                      
                        <!--<div class="field date required">
                            <label class="label" for="publish_date"><span>Publish Date</span></label>
                            <div class="control">
                                <input type="text" name="publish_date" id="datepicker"  data-validate="{required:true}">
                            </div>
                        </div>-->
                    </div>
                </fieldset>
                <fieldset style="display:none">             
                    <div class="field required" style="display:none">
                    <label class="label" for="product_id"><span></span></label>
                        <select name="product_id[]">
                        <option value="<?php echo $_item->getProductId(); ?>"><?php echo $_item->getProductId(); ?></option>
                        </select>
                    </div>
                    <div class="field required" style="display:none">
                    <label class="label" for="customer_id"><span></span></label>
                        <select name="customer_id[]">
                        <option value="<?php echo $customerId; ?>"><?php echo $customerId; ?></option>
                        </select>
                    </div>
                </fieldset>
            </fieldset>
            <?php } } ?>
        <div class="actions-toolbar">
            <div class="primary">
                <button type="submit" class="action submit primary" title="Save"><span>Save</span></button>
            </div>
        </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    require(["jquery", "mage/calendar"], function($){
    $("#datepicker").calendar({
        showsTime: false,
        dateFormat: "M/d/Y",
        yearRange: "-120y:c+nn",
        maxDate: "-1d", changeMonth: true, changeYear: true})
    });
</script>

Block php file

<?php
namespace Softadroit\Prescription\Block;

class Prescription extends \Magento\Framework\View\Element\Template
{
    protected $_orderFactory;
    
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession->create();;
        parent::__construct($context, $data);
    }
    
    
    public function getFormAction()
    {   
        return $this->getUrl('prescription/index/save', ['_secure' => true]);  
    }
    
    public function getLoggedinCustomerId() {
        if ($this->_customerSession->isLoggedIn()) {
            return $this->_customerSession->getId();
        }
        return false;
    }
    
    public function getQuoteData()
    {
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }
}
?>

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.