0
votes

When customer enters the details in the form fields and enter submit button then the Order email should send customer's email immediately and also Order should be created in Backend as well. Once its submitted(After email sent & order created), the success message will display on same current product page.

For this, I haved added my Custom order form on my theme's product detailed page. Form contains the fields like Name, Email, Quantity, Product Name and Price(Current Product Name & Price when customer Visits).

How to send email and create order programmatically on Product page custom form?

Any help mostly appreciated.

1
will your form include payment option also or it will be set cod or something offline ?Rohit Goel
yes, its COD payment optionMesk

1 Answers

0
votes

You would need to create a block class for your template ( there are many resources on how to create a controller for Magento 2, you can reference them).

There you can have a method that gets the product URL for adding the order:

public function getAddProductUrl($product, $additional = [])
{
    return $this->_cartHelper->getAddUrl($product, $additional);
}

Then in your phtml template file add the following html form and javascript or somethng similar to this:

<script type="text/x-magento-init">
    {
    "[data-role=tocart-form]": {
    "catalogAddToCart": {}
    }
    }
</script>
<div class="product-add-form">
    <?php $product = $block->getProduct(); ?>
    <form data-product-sku="<?= /* @NoEscape */ $product->getSku() ?>"
          action="<?= /* @NoEscape */ $block->getAddProductUrl($product) ?>" method="post" data-role="tocart-form">
        <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $product->getId() ?>" />
        <?= $block->getBlockHtml('formkey') ?>
        <button type="submit" title="<?php echo /* @escapeNotVerified */ __('Add to Cart'); ?>" class="action primary tocart">
            <span><?php echo /* @escapeNotVerified */ __('Add to Cart'); ?></span>
        </button>
    </form>
</div>