Question

Hi all I need your help,

I am writing a new module (quote request, when user go to the product details page, they can request a quote for the product)

I created a form and display in the product details page. When user submit the form, the data should store in the custom table and send email to one of the store admin.

I am stuck at form submission, here is my issue. when user go to the product details page, I need to load the product sku to a hidden field and pass the value to the controller. But I am not getting the product sku in the phtml page.

I am trying to pass the value from the block class to the phtml file and not working.

here is my block file

<?php
namespace Vendor\QuoteRequest\Block;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
class QuotePage extends Template

{
    public $_registry;
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    array $data = [])
{
    $this->_registry    =   $registry;
    parent::__construct($context, $data);
      }
  public function getCurrentProduct(){
        return $this->_registry->registry('current_product');

   }
}

and here is my phtml file

<div class="product-right">
<form class="form george-quote" action="<?php /* @escapeNotVerified */ echo $this->getUrl("quoterequest/index/index")?>" id="quote-form" method="post" data-hasrequired="* Required Fields">
    <fieldset class="fieldset">
        <div class="field name required">
            <label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
            <div class="control">
                <input name="email" id="email" title="email" value="" class="input-text" type="email" />
            </div>
        </div>
        <div class="field name required">
            <label class="label" for="firstname"><span><?= $block->escapeHtml(__('First Name')) ?></span></label>
            <div class="control">
                <input name="firstname" id="firstname" title="First Name" value="" class="input-text" type="text" />
            </div>
        </div>
        <div class="field name required">
            <label class="label" for="lastname"><span><?= $block->escapeHtml(__('Last Name')) ?></span></label>
            <div class="control">
                <input name="lastname" id="lastname" title="Last Name" value="" type="text" class="input-text" />
            </div>
        </div>
        <div class="field name required">
            <label class="label" for="phone"><span><?= $block->escapeHtml(__('Phone')) ?></span></label>
            <div class="control">
                <input name="phone" id="phone" title="Phone" value="" class="input-text" type="text" />
            </div>
        </div>
        <?= $block->getChildHtml('form.additional.info') ?>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary">
            <input type="hidden" name="productsku" id="productsku" value="<?= $block->currentProduct->getSku(); ?>" />
            <input type="hidden" name="hideit" id="hideit" value="" />
            <button type="submit" id="add" title="Submit" class="action submit primary">
                <span><?= $block->escapeHtml(__('Request Quote')) ?></span>
            </button>
        </div>
    </div>

</form>

my xml file

    <?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.main">
                <block class="Magento\Catalog\Block\Product\View" name="quotepage.part"  template="Vendor_QuoteRequest::product/view/quotepage.phtml" after="product.info.main">
                </block>
        </referenceContainer>
    </body>
Was it helpful?

Solution

please also check catalog_product_view.xml file.

<referenceBlock name="product.info.details">
    <block class="Vendor\QuoteRequest\Block\QuotePage" name="prod.inquiry" group="detailed_info" template="Vendor_QuoteRequest::test.phtml">
        <arguments>
            <argument translate="true" name="title" xsi:type="string">Product enquiry</argument>
            <argument name="priority" xsi:type="string">1</argument>
        </arguments>
    </block>
</referenceBlock>

please check my code you use the getCurrentProduct in block file and phtml to call currentProduct function.

<?php
$product = $block->getCurrentProduct();
$productId = $product->getId();
$productName = $product->getName();
$productSku = $product->getSku();
?>
<div class="product-right"> <form class="form george-quote" action="<?php /* @escapeNotVerified */ echo $this->getUrl("quoterequest/index/index")?>" id="quote-form" method="post" data-hasrequired="* Required Fields">
        <fieldset class="fieldset">
            <div class="field name required">
                <label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
                <div class="control">
                    <input name="email" id="email" title="email" value="" class="input-text" type="email" />
                </div>
            </div>
            <div class="field name required">
                <label class="label" for="firstname"><span><?= $block->escapeHtml(__('First Name')) ?></span></label>
                <div class="control">
                    <input name="firstname" id="firstname" title="First Name" value="" class="input-text" type="text" />
                </div>
            </div>
            <div class="field name required">
                <label class="label" for="lastname"><span><?= $block->escapeHtml(__('Last Name')) ?></span></label>
                <div class="control">
                    <input name="lastname" id="lastname" title="Last Name" value="" type="text" class="input-text" />
                </div>
            </div>
            <div class="field name required">
                <label class="label" for="phone"><span><?= $block->escapeHtml(__('Phone')) ?></span></label>
                <div class="control">
                    <input name="phone" id="phone" title="Phone" value="" class="input-text" type="text" />
                </div>
            </div>
        </fieldset>
        <div class="actions-toolbar">
            <div class="primary">
                <input type="hidden" name="productsku" id="productsku" value="<?php echo $productSku; ?>" />
                <input type="hidden" name="hideit" id="hideit" value="" />
                <button type="submit" id="add" title="Submit" class="action submit primary">
                    <span><?= $block->escapeHtml(__('Request Quote')) ?></span>
                </button>
            </div>
        </div>

    </form>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top