1

i want to add textbox names like notes under size in products page.

Could you please any one tell me how to do. i have tried to via attribute but it doesn't help me. enter image description here

  • pls provide more info....why you need textbox here? – Ranganathan Jul 23 at 12:09
  • i want to allow option for enter some text from user for gift message – amy Jul 23 at 13:37
  • Guide me if I'm wrong. My idea is...you need to create additional column in quote_item table and sales_order_item table, after addtocart you need to save it in quote_item and after placed order you need to move it to sales_order_item from quote_item. And display it in admin order item section..Pls let me know if you need any code examples. – Ranganathan Jul 24 at 4:24
  • yes give me some samples. because i m new to this magento. – amy Jul 24 at 10:06
0

I have created a Module for this question

STEP1: Create a module using required files as

app/code/Vendor/Test/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'Vendor_Test',
   __DIR__
);

app/code/Vendor/Test/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Test" setup_version="0.0.1"></module>
</config>

STEP2: Create text box under qty text box in app/design/frontend/Vendor/Themename/Magento_Catalog/templates/product/view/addtocart.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>

        <!-- Your gift message custom field here -->
        <input type="text"
           name="gift_message"
           id="gift_message"
           value=""
           title="<?= /* @escapeNotVerified */ __('Gift messages') ?>"
           class="input-text"
                       />
        <!-- Your gift message custom field here -->

        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
            </button>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>

STEP3: Create gift_message column in quote_item and sales_order_item tables using setup files

app/code/Vendor/Test/Setup/UpgradeSchema.php

<?php
namespace Vendor\Test\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '0.0.1') < 0) {
            $setup->startSetup();
            $connection = $setup->getConnection();
            $connection->addColumn(
                $setup->getTable('quote_item'),
                'gift_message',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'default' => '',
                    'comment' => 'Gift Message'
                ]
            );
            $connection->addColumn(
                $setup->getTable('sales_order_item'),
                'gift_message',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'default' => '',
                    'comment' => 'Gift Message'
                ]
            );
            $setup->endSetup();
        }
    }
}

STEP4: Create an event checkout_cart_add_product_complete to save custom field value on addtocart app/code/Vendor/Test/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer name="save_gift_message_to_cart" instance="Vendor\Test\Observer\AddProductAfterObserver" />
    </event>
</config>

app/code/Vendor/Test/Observer/AddProductAfterObserver.php

<?php
namespace Vendor\Test\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddProductAfterObserver implements ObserverInterface
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->_checkoutSession = $checkoutSession;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $giftMessages = $observer->getEvent()->getRequest()->getParam('gift_message');
        if(!empty($giftMessages)){
            /** @var \Magento\Quote\Model\Quote\Item $item */
            $item = $this->_checkoutSession->getQuote()->getItemByProduct($product);
            $item->setGiftMessage($giftMessages);
            $item->save();
        }
    }
}

STEP5: Create a plugin for move quote table gift_message data to sales_order_item table column

app/code/Vendor/Test/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="quote_item_to_order_item" type="Vendor\Test\Plugin\QuoteToOrderItem"/>
    </type>
</config>

app/code/Vendor/Test/Plugin/QuoteToOrderItem.php

<?php
namespace Vendor\Test\Plugin;

class QuoteToOrderItem
{
    /**
     * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject
     * @param \Closure $proceed
     * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
     * @param array $additional
     *
     * @return object $orderItem
     */
    public function aroundConvert(
        \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote\Item\AbstractItem $item,
        $additional = []
    ) {
        $orderItem = $proceed($item, $additional);
        $orderItem->setGiftMessage($item->getGiftMessage());
        return $orderItem;
    }
}

STEP6: Display the custom gift_messge field value in adminpanel->sales->orders->specific order->orderitems

app/code/Vendor/Test/view/adminhtml/layout/sales_order_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order_items">
            <arguments>
                <argument name="columns" xsi:type="array">
                    <item name="gift_message" xsi:type="string" translate="true">Gift message</item>
                </argument>
            </arguments>
            <referenceBlock name="default_order_items_renderer">
                <arguments>
                    <argument name="columns" xsi:type="array">
                        <item name="gift_message" xsi:type="string" translate="true">col-giftmessage</item>
                    </argument>
                </arguments>
            </referenceBlock>

            <block class="Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn"
                   name="column_gift_message"
                   template="Vendor_Test::gift_message.phtml" group="column" />
        </referenceBlock>
    </body>
</page>

app/code/Vendor/Test/view/adminhtml/templates/gift_message.phtml

<?php
$_item = $block->getItem();
$giftMessage = $_item->getGiftMessage();

if( !empty($giftMessage) ) {
    echo $_item->getGiftMessage();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.