Magento 2 Display Custom Field After Payment And Save On Order

by JurnalWarga.com 63 views
Iklan Headers

Hey guys! Ever wanted to add a little something extra to your Magento 2 checkout page? Like, maybe a text area right after the payment methods where customers can leave special instructions or notes? Well, you're in the right place! This guide will walk you through how to display a custom field – in this case, a text area – just after the payment method list on the checkout page in Magento 2. Not only that, but we'll also cover how to save the value entered by the customer when they place an order and display it in the admin panel. Cool, right? Let's dive in!

Why Add a Custom Field?

Before we jump into the how-to, let's quickly chat about why you might want to do this. Adding a custom field can significantly enhance the customer experience and provide valuable information for your business. For example, you might want customers to provide delivery instructions, gift messages, or any other specific details related to their order. By capturing this information directly on the checkout page, you ensure you're not missing any crucial details and can fulfill orders more efficiently. Plus, it shows your customers that you're paying attention to their individual needs, which can boost their satisfaction and loyalty. So, adding a custom field is not just a technical tweak; it's a strategic move to improve your service and build stronger customer relationships. Okay, now that we're on the same page about the importance of custom fields, let's get to the fun part: making it happen in Magento 2!

Step 1: Create a Custom Module

Alright, first things first, we need to create a custom module. Think of a module as a little package of code that adds functionality to your Magento 2 store without messing with the core files. It's like adding a new app to your phone – it enhances what your phone can do without altering its fundamental operating system. Creating a module might sound intimidating, but trust me, it's not as scary as it seems. We'll break it down into easy steps.

  1. Module Directory: Navigate to the app/code directory in your Magento 2 installation. This is where all the magic happens for custom modules. Inside app/code, create a directory for your vendor name. Let's say your vendor name is "MyCompany"; you'd create a directory named MyCompany. Inside this directory, create another directory for your module name. We'll call our module "CustomCheckout," so the path will be app/code/MyCompany/CustomCheckout. This structure is crucial because Magento uses it to locate and load your module.

  2. module.xml: Inside your module directory (app/code/MyCompany/CustomCheckout), create a file named module.xml in the etc directory. This file tells Magento that your module exists and provides basic information about it. Add the following code to 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="MyCompany_CustomCheckout" setup_version="1.0.0">
        </module>
    </config>
    

    This XML configuration file declares your module. The name attribute should match your vendor and module name (MyCompany_CustomCheckout). The setup_version attribute is used for database schema and data upgrades, so let's set it to 1.0.0 for now. This file is essential because it's the first thing Magento looks for when it's trying to load your module.

  3. registration.php: Also inside your module directory, create a file named registration.php. This file registers your module with Magento. Add the following code to registration.php:

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

    This PHP script registers your module with Magento's component registrar. The ComponentRegistrar::register function takes three arguments: the component type (in this case, ComponentRegistrar::MODULE), the module name (MyCompany_CustomCheckout), and the path to the module directory (__DIR__). Without this file, Magento won't know your module exists.

  4. Enable the Module: Now that you've created the module files, you need to tell Magento to recognize and load your module. Open your terminal, navigate to your Magento 2 root directory, and run the following commands:

    php bin/magento module:enable MyCompany_CustomCheckout
    php bin/magento setup:upgrade
    php bin/magento setup:di:compile
    php bin/magento setup:static-content:deploy -f
    php bin/magento cache:flush
    

    Let's break down these commands:

    • php bin/magento module:enable MyCompany_CustomCheckout: This command enables your module. Magento will now load the module's configuration and code.
    • php bin/magento setup:upgrade: This command updates the Magento database schema and data based on your module's configuration. It's essential to run this after enabling a new module or making changes to your module's schema.
    • php bin/magento setup:di:compile: This command compiles Magento's dependency injection configuration. It improves performance by pre-generating the object manager's configuration.
    • php bin/magento setup:static-content:deploy -f: This command deploys static content (like CSS, JavaScript, and images) for your store. The -f flag forces the deployment, which is useful when you've made changes to static files.
    • php bin/magento cache:flush: This command clears the Magento cache. It's crucial to clear the cache after making changes to your Magento store to ensure that the changes are reflected on the frontend.

    After running these commands, your module should be enabled and ready to go. You can verify this by running php bin/magento module:status and looking for your module (MyCompany_CustomCheckout) in the list of enabled modules. If you see it there, congratulations! You've successfully created and enabled a custom module in Magento 2. This is a significant step towards adding your custom field to the checkout page. Next, we'll dive into adding the field to the checkout layout.

Step 2: Add the Custom Field to the Checkout Layout

Okay, now that we have our module up and running, let's get to the juicy part: adding that custom text area to the checkout page. This involves a bit of XML layout magic, but don't worry, we'll walk through it together. Think of layout XML files as blueprints that tell Magento where to put things on a page. We're going to create a blueprint that adds our text area right after the payment methods. Ready? Let's do it!

  1. Create a Layout File: Navigate to your module's view/frontend/layout directory. If these directories don't exist, you'll need to create them. Inside the layout directory, create a file named checkout_index_index.xml. This file will contain the layout updates for the checkout page. The naming convention here is important: checkout_index_index.xml tells Magento that these layout updates should be applied to the checkout page (index) when the index action is called.

  2. Add the Layout Update: Open checkout_index_index.xml and add the following code:

    <?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>
            <referenceContainer name="checkout.root">
                <referenceBlock name="checkout.payment.methods.renderers">
                    <block class="Magento\Framework\View\Element\Template" name="custom_checkout_textarea" template="MyCompany_CustomCheckout::checkout/custom_textarea.phtml" before="-"/>
                </referenceBlock>
            </referenceContainer>
        </body>
    </page>
    

    Let's break down what's happening in this XML:

    • <page>: This is the root element for a layout XML file. It tells Magento that this file contains layout instructions for a page.
    • <body>: This element contains the layout updates that you want to apply to the page.
    • <referenceContainer name="checkout.root">: This element references a container in the checkout layout named checkout.root. A container is a placeholder for blocks and other content.
    • <referenceBlock name="checkout.payment.methods.renderers">: This element references a block named checkout.payment.methods.renderers. This block is responsible for rendering the list of payment methods on the checkout page. We're targeting this block because we want to insert our custom field after the payment methods.
    • <block class="Magento\Framework\View\Element\Template" name="custom_checkout_textarea" template="MyCompany_CustomCheckout::checkout/custom_textarea.phtml" before="-"/>: This is the magic line. It adds a new block to the layout. Let's break it down further:
      • class="Magento\Framework\View\Element\Template": This specifies the class for the block. Magento\Framework\View\Element\Template is a basic block class that renders a template file.
      • name="custom_checkout_textarea": This gives the block a name, which we can use to reference it later if needed.
      • template="MyCompany_CustomCheckout::checkout/custom_textarea.phtml": This specifies the template file that the block should render. The format is Vendor_Module::path/to/template.phtml. In this case, we're telling Magento to use a template file named custom_textarea.phtml located in the view/frontend/templates/checkout directory of our module.
      • before="-": This attribute tells Magento to insert our block before the block it's referencing (checkout.payment.methods.renderers). The - value means "at the beginning" of the referenced block's children. If you wanted to insert the block after, you'd use after="-". This is how we ensure our text area appears just after the payment methods.
  3. Create the Template File: Now we need to create the template file that our block will render. Create a directory named templates inside view/frontend. Inside templates, create another directory named checkout. Finally, inside checkout, create a file named custom_textarea.phtml. This is where we'll put the HTML for our text area.

  4. Add the Text Area HTML: Open custom_textarea.phtml and add the following code:

    <div class="custom-checkout-textarea">
        <label for="custom_textarea">Special Instructions:</label>
        <textarea name="custom_textarea" id="custom_textarea" cols="5" rows="3"></textarea>
    </div>
    

    This is a simple HTML snippet that creates a div container, a label, and a textarea element. The name and id attributes are important because we'll use them later to save the value of the text area. This is the visual part of our custom field – the actual text area that customers will see and interact with on the checkout page.

  5. Flush the Cache: After making these changes, you need to flush the Magento cache to see the changes on the frontend. Run the following command in your terminal:

    php bin/magento cache:flush
    

    Once the cache is flushed, go to your checkout page and you should see your new text area displayed just after the payment methods. If you don't see it, double-check that you've created the files in the correct directories and that the XML and PHP code are correct. Typos can be tricky buggers! You might also want to clear your browser cache, just in case.

    Congratulations! You've successfully added a custom field to the checkout page using layout XML. This is a major accomplishment, and you're well on your way to providing a more customized checkout experience for your customers. Next, we'll tackle the important task of saving the value entered in the text area when the order is placed.

Step 3: Save the Custom Field Value on Place Order

Alright, we've got our custom text area showing up beautifully on the checkout page. High fives! But now, we need to make sure that whatever customers type into that text area is actually saved when they place their order. This involves a little bit of backend magic, specifically using Magento's event-observer system. Think of events as notifications that Magento sends out when certain actions happen (like an order being placed), and observers as listeners that react to those notifications. We're going to create an observer that listens for the order placement event and saves the value of our text area. Sounds exciting, right? Let's get to it!

  1. Create an Observer: Navigate to your module's MyCompany/CustomCheckout directory. Create a directory named Observer if it doesn't already exist. Inside the Observer directory, create a PHP file named SaveCustomTextareaValueObserver.php. This is where we'll define our observer class.

  2. Define the Observer Class: Open SaveCustomTextareaValueObserver.php and add the following code:

    <?php
    
    namespace MyCompany\CustomCheckout\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Event\Observer;
    use Magento\Sales\Model\Order;
    
    class SaveCustomTextareaValueObserver implements ObserverInterface
    {
        /**
         * @param Observer $observer
         */
        public function execute(Observer $observer)
        {
            /* @var Order $order */
            $order = $observer->getEvent()->getData('order');
            $quote = $observer->getEvent()->getData('quote');
    
            if ($quote && $order) {
                $customTextareaValue = $quote->getCustomTextarea();
                if ($customTextareaValue) {
                    $order->setData('custom_textarea', $customTextareaValue);
                }
            }
    
            return $this;
        }
    }
    

    Let's break down what this code does:

    • namespace MyCompany\CustomCheckout\Observer;: This line defines the namespace for our observer class. Namespaces are used to organize PHP code and prevent naming conflicts.
    • use Magento\Framework\Event\ObserverInterface;: This line imports the ObserverInterface interface, which our observer class must implement.
    • use Magento\Framework\Event\Observer;: This line imports the Observer class, which our observer class extends.
    • use Magento\Sales\Model\Order;: This line imports the Order model, which represents a sales order in Magento.
    • class SaveCustomTextareaValueObserver implements ObserverInterface: This line defines our observer class, SaveCustomTextareaValueObserver, and tells it to implement the ObserverInterface interface.
    • public function execute(Observer $observer): This is the method that will be executed when the event is dispatched. It takes an Observer object as an argument, which contains information about the event.
    • $order = $observer->getEvent()->getData('order');: This line retrieves the Order object from the event data.
    • $quote = $observer->getEvent()->getData('quote');: This line retrieves the Quote object from the event data. A quote represents the customer's shopping cart before it's converted into an order.
    • if ($quote && $order) { ... }: This conditional statement checks that both the order and the quote exist before proceeding.
    • $customTextareaValue = $quote->getCustomTextarea();: This line attempts to retrieve the value of our custom text area from the quote. We'll need to add this attribute to the quote model later.
    • if ($customTextareaValue) { ... }: This conditional statement checks that a value was entered in the text area before saving it to the order.
    • $order->setData('custom_textarea', $customTextareaValue);: This line sets the value of our custom text area on the order. We're using the setData method to add a new attribute (custom_textarea) to the order and set its value.
    • return $this;: This line returns the observer instance.

    This observer is the key to saving our custom field value. It listens for the order placement event, retrieves the value from the quote, and saves it to the order. But we're not quite done yet – we still need to tell Magento about our observer and add the custom_textarea attribute to the quote.

  3. Declare the Observer in events.xml: To tell Magento about our observer, we need to create an events.xml file. Navigate to your module's view/frontend/etc directory. If the etc directory doesn't exist, create it. Inside etc, create a file named events.xml and add the following code:

    <?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="sales_model_service_quote_submit_before">
            <observer name="save_custom_textarea_value" instance="MyCompany\CustomCheckout\Observer\SaveCustomTextareaValueObserver" />
        </event>
    </config>
    

    This XML configuration file declares our observer. Let's break it down:

    • <event name="sales_model_service_quote_submit_before">: This line specifies the event that our observer will listen for. The sales_model_service_quote_submit_before event is dispatched before a quote is converted into an order.
    • <observer name="save_custom_textarea_value" instance="MyCompany\CustomCheckout\Observer\SaveCustomTextareaValueObserver" />: This line declares our observer. Let's break it down further:
      • name="save_custom_textarea_value": This gives our observer a name. This name is used internally by Magento and doesn't need to be unique across all observers, but it's good practice to give it a descriptive name.
      • instance="MyCompany\CustomCheckout\Observer\SaveCustomTextareaValueObserver": This specifies the class that should be instantiated when the event is dispatched. In this case, it's our SaveCustomTextareaValueObserver class.

    This events.xml file is crucial because it tells Magento to execute our observer whenever the sales_model_service_quote_submit_before event is dispatched. Without this file, our observer would never be called, and our custom field value wouldn't be saved.

  4. Add the custom_textarea Attribute to the Quote: Now we need to add the custom_textarea attribute to the quote model. This will allow us to retrieve the value entered in the text area from the quote in our observer. To do this, we'll use an extension attribute.

    • Create a file named extension_attributes.xml in your module's etc directory. If the etc directory doesn't exist, create it. Add the following code to extension_attributes.xml:

      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
          <extension_attributes for="Magento\Quote\Api\Data\CartInterface">
              <attribute code="custom_textarea" type="string"/>
          </extension_attributes>
          <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
              <attribute code="custom_textarea" type="string"/>
          </extension_attributes>
      </config>
      

      This XML configuration file defines an extension attribute for the Magento\Quote\Api\Data\CartInterface and Magento\Quote\Api\Data\AddressInterface. Extension attributes allow you to add custom attributes to existing Magento entities without modifying the core code. This is a best practice because it ensures that your customizations won't be overwritten when you upgrade Magento.

      • for="Magento\Quote\Api\Data\CartInterface": This specifies the interface that we're adding the extension attribute to. In this case, it's the CartInterface, which represents the quote.
      • <attribute code="custom_textarea" type="string"/>: This defines the extension attribute. Let's break it down further:
        • code="custom_textarea": This is the name of the attribute. We'll use this name to access the attribute in our code.
        • type="string": This specifies the data type of the attribute. In this case, it's a string, which is appropriate for a text area.
    • Create a file named di.xml in your module's etc directory. If the etc directory doesn't exist, create it. Add the following code to 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">
              <plugin name="save_custom_textarea_to_quote" type="MyCompany\CustomCheckout\Plugin\Quote" sortOrder="10"/>
          </type>
           <type name="Magento\Quote\Model\Quote\Address">
              <plugin name="save_custom_textarea_to_quote_address" type="MyCompany\CustomCheckout\Plugin\Quote\Address" sortOrder="10"/>
          </type>
      </config>
      
    • Create a directory named Plugin in your module's MyCompany/CustomCheckout directory. Inside the Plugin directory, create a PHP file named Quote.php and Address.php. Add the following code to Quote.php:

      <?php
      namespace MyCompany\CustomCheckout\Plugin;
      
      use Magento\Quote\Model\Quote;
      
      class Quote
      {
          public function beforeMerge(
              Quote $quote,
              Quote $mergeQuote
          ) {
              $quote->setCustomTextarea($mergeQuote->getCustomTextarea());
              return [
                  $quote,
                  $mergeQuote
              ];
          }
      }
      
    • Add the following code to Address.php:

      <?php
      namespace MyCompany\CustomCheckout\Plugin\Quote;
      
      use Magento\Quote\Model\Quote\Address;
      
      class Address
      {
           /**
            * @param Address $subject
            * @param $cartAddress
            * @return array
            */
          public function beforeImportData(
              Address $subject,
              $cartAddress
          ) {
              if ($cartAddress->getCustomTextarea()) {
                  $subject->setCustomTextarea($cartAddress->getCustomTextarea());
              }
      
              return [ $cartAddress ];
          }
      }
      
    • To save custom field value to quote

    <?php
    namespace MyCompany\CustomCheckout\Plugin;
    
    class SaveCustomFieldToQuote implements \Magento\Framework\Event\ObserverInterface
    {
        /**
         * @var \Magento\Framework\App\RequestInterface
         */
        protected $_request;
    
        /**
         * @param \Magento\Framework\App\RequestInterface $request
         */
        public function __construct(
            \Magento\Framework\App\RequestInterface $request
        ) {
            $this->_request = $request;
        }
    
        /**
         * Execute observer method
         *
         * @param \Magento\Framework\Event\Observer $observer
         */
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $quote = $observer->getEvent()->getData('quote');
            $request = $this->_request->getParams();
    
            if (isset($request['custom_textarea'])) {
                $quote->setCustomTextarea($request['custom_textarea']);
            }
    
            return $this;
        }
    }
    
    • Create events.xml file in MyCompany/CustomCheckout/etc/frontend
    <?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_submit_all_after">
            <observer name="save_custom_field_to_order" instance="MyCompany\CustomCheckout\Observer\SaveCustomFieldToOrder" />
        </event>
         <event name="sales_quote_save_before">
            <observer name="save_custom_field_to_quote" instance="MyCompany\CustomCheckout\Plugin\SaveCustomFieldToQuote" />
        </event>
    </config>
    

    This extension_attributes.xml file is essential because it tells Magento to add the custom_textarea attribute to the quote. Without it, we wouldn't be able to retrieve the value from the quote in our observer.

  5. Flush the Cache: After making these changes, you need to flush the Magento cache again. Run the following command in your terminal:

    php bin/magento cache:flush
    

    Now, when a customer places an order, the value they enter in the custom text area should be saved to the order. To verify this, you can check the sales_order table in your database and look for the custom_textarea column. If you see the value there, congratulations! You've successfully saved the custom field value on place order. This is a huge step in creating a fully functional custom field.

Step 4: Display the Saved Value in Admin Sales Order

Okay, we're on the home stretch now! We've successfully added our custom text area to the checkout page and saved the value when an order is placed. Now, we want to be able to see that value in the admin panel when viewing an order. This will allow store administrators to easily access the special instructions or notes provided by the customer. To do this, we'll need to add our custom field to the order view page in the admin panel. This involves a bit more layout XML magic, but you've got this! Let's dive in!

  1. Create a Layout File: Navigate to your module's view/adminhtml/layout directory. If these directories don't exist, you'll need to create them. Inside the layout directory, create a file named sales_order_view.xml. This file will contain the layout updates for the order view page in the admin panel. The naming convention here is a little different than in the frontend: sales_order_view.xml tells Magento that these layout updates should be applied to the order view page in the admin panel.

  2. Add the Layout Update: Open sales_order_view.xml and add the following code:

    <?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>
            <referenceContainer name="order_info">
                <block class="Magento\Framework\View\Element\Text\ListText" name="custom_textarea_order_info" before="-">
                    <block class="Magento\Framework\View\Element\Template" name="custom_textarea_order_info_block" template="MyCompany_CustomCheckout::order/custom_textarea_info.phtml"/>
                </block>
            </referenceContainer>
        </body>
    </page>
    

    Let's break down what's happening in this XML:

    • <page>: This is the root element for a layout XML file, just like in the frontend layout files.
    • <body>: This element contains the layout updates that you want to apply to the page.
    • <referenceContainer name="order_info">: This element references a container in the order view layout named order_info. This container is where the order information is displayed in the admin panel.
    • <block class="Magento\Framework\View\Element\Text\ListText" name="custom_textarea_order_info" before="-">: This element adds a new block to the layout. Let's break it down further:
      • class="Magento\Framework\View\Element\Text\ListText": This specifies the class for the block. Magento\Framework\View\Element\Text\ListText is a block class that allows you to add text and other blocks to it. It's often used as a container for other blocks.
      • name="custom_textarea_order_info": This gives the block a name, which we can use to reference it later if needed.
      • before="-": This attribute tells Magento to insert our block before the other blocks in the order_info container. The - value means "at the beginning" of the container's children.
    • <block class="Magento\Framework\View\Element\Template" name="custom_textarea_order_info_block" template="MyCompany_CustomCheckout::order/custom_textarea_info.phtml"/>: This element adds another block to the layout, but this time it's a template block. Let's break it down further:
      • class="Magento\Framework\View\Element\Template": This specifies the class for the block, just like in the frontend layout file.
      • name="custom_textarea_order_info_block": This gives the block a name.
      • template="MyCompany_CustomCheckout::order/custom_textarea_info.phtml": This specifies the template file that the block should render. In this case, we're telling Magento to use a template file named custom_textarea_info.phtml located in the view/adminhtml/templates/order directory of our module.

    This layout XML adds a container block (custom_textarea_order_info) to the order_info container and then adds a template block (custom_textarea_order_info_block) inside that container. This structure allows us to easily add our custom field information to the order view page.

  3. Create the Template File: Now we need to create the template file that our block will render. Create a directory named templates inside view/adminhtml. Inside templates, create another directory named order. Finally, inside order, create a file named custom_textarea_info.phtml. This is where we'll put the PHP code to display the custom field value.

  4. Add the PHP Code to Display the Value: Open custom_textarea_info.phtml and add the following code:

    <?php
    /** @var \Magento\Sales\Block\Adminhtml\Order\View $block */
    $customTextarea = $block->getOrder()->getData('custom_textarea');
    if ($customTextarea):
    ?>
    <div class="order-custom-textarea">
        <strong>Special Instructions:</strong>
        <p><?php echo $block->escapeHtml($customTextarea); ?></p>
    </div>
    <?php endif; ?>
    

    Let's break down what this code does:

    • <?php /** @var \Magento\Sales\Block\Adminhtml\Order\View $block */: This is a PHPDoc comment that tells our IDE that the $block variable is an instance of the Magento\Sales\Block\Adminhtml\Order\View class. This helps with code completion and error checking.
    • $customTextarea = $block->getOrder()->getData('custom_textarea');: This line retrieves the value of our custom text area from the order. We're using the getOrder method to get the Order object and then the getData method to retrieve the value of the custom_textarea attribute.
    • if ($customTextarea):: This conditional statement checks that a value was saved in the custom_textarea attribute before displaying it.
    • <div class="order-custom-textarea"> ... </div>: This HTML creates a div container for our custom field information.
    • <strong>Special Instructions:</strong>: This displays the label for our custom field in bold.
    • <p><?php echo $block->escapeHtml($customTextarea); ?></p>: This displays the value of our custom text area in a paragraph. The escapeHtml method is used to prevent cross-site scripting (XSS) vulnerabilities by escaping any HTML characters in the value. This is a security best practice that you should always follow when displaying user-generated content.

    This template file is what displays our custom field value in the admin panel. It retrieves the value from the order and displays it in a formatted way. Without this file, we wouldn't be able to see the value in the admin panel.

  5. Flush the Cache: After making these changes, you need to flush the Magento cache one last time. Run the following command in your terminal:

    php bin/magento cache:flush
    

    Now, go to the admin panel, navigate to Sales > Orders, and view an order that has a value in the custom_textarea field. You should see your custom field information displayed in the order information section. If you see it there, you've done it! You've successfully displayed the saved value in the admin sales order.

Woohoo! Give yourself a pat on the back, guys! You've just walked through the entire process of adding a custom field to the checkout page in Magento 2, saving the value on place order, and displaying it in the admin panel. That's no small feat! We covered a lot of ground, from creating a custom module to using layout XML, observers, and extension attributes. You've learned some powerful techniques that you can use to customize your Magento 2 store in countless ways.

By adding a custom field, you've not only enhanced the customer experience by allowing them to provide valuable information, but you've also made it easier for your team to fulfill orders efficiently. This is a win-win situation for both your customers and your business.

Remember, the key to mastering Magento 2 customization is practice and persistence. Don't be afraid to experiment and try new things. The more you work with Magento 2, the more comfortable you'll become with its architecture and the more easily you'll be able to customize it to meet your specific needs.

So, go forth and customize! Add those extra fields, tweak those layouts, and make your Magento 2 store truly unique. And if you ever get stuck, remember that the Magento community is full of helpful folks who are always willing to lend a hand. Keep learning, keep building, and keep making your store awesome!