0

The selected option is not being save in the databse, please help what i did wrong? I'm running magento 2.4.4 with php 8.1

event.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_submit_before">
        <observer name="DeliveryComment_storeSelector_save_city" instance="DeliveryComment\storeSelector\Observer\SaveCity" />
    </event>
</config>

Obsever file

<?php

namespace DeliveryComment\StoreSelector\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;

class SaveCity implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        /** @var Order $order */
        $order = $observer->getEvent()->getOrder();

        if (!$order) {
            return;
        }

        $selectedCity = $order->getShippingAddress()->getData('delivery_comment');
        if ($selectedCity) {
            $order->setData('delivery_comment', $selectedCity);
        }
    }
}

setup script

<?php
namespace DeliveryComment\storeSelector\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $connection = $installer->getConnection();

        $connection->addColumn(
            $installer->getTable('quote'),
            'delivery_comment',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => false,
                'comment' => 'Delivery Comment',
            ]
        );

        $connection->addColumn(
            $installer->getTable('sales_order'),
            'delivery_comment',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => false,
                'comment' => 'Delivery Comment',
            ]
        );

        $installer->endSetup();
    }
}

DeliveryComment/storeSelector/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
       <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shippingAdditional" xsi:type="array">
                                                            <item name="component" xsi:type="string">uiComponent</item>
                                                            <item name="displayArea" xsi:type="string">shippingAdditional</item>
                                                            <item name="children" xsi:type="array">
                                                                <item name="delivery_comment" xsi:type="array">
                                                                    <item name="component" xsi:type="string">DeliveryComment_storeSelector/js/view/shipping/delivery-comment-block</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

DeliveryComment/storeSelector/view/frontend/web/js/view/shipping/delivery-comment-block.js

define([
    'jquery',
    'ko',
    'uiComponent'
], function ($, ko, Component) {
    'use strict';

    return Component.extend({
        defaults: {
            template: 'DeliveryComment_storeSelector/shipping/delivery-comment-block'
        }
    });
});

DeliveryComment/storeSelector/view/frontend/web/template/shipping/delivery-comment-block.html

<div class="field select">
    <label for="delivery_comment" style='font-weight: 700; color: red;' class="label">
        <span>Please select your Click-n-Collect Store below</span>
    </label>
    <select name="delivery_comment" id="delivery_comment" data-bind="value: delivery_comment">
        <option value="">Please select your click-n-collect store</option>
            <option value="23, Collect from New york"> store in new york city</option>
            <option value="26, Collect from London"> store in london </option>
            <option value="60, Collect from France"> store in france</option>
    </select>
</div>

3
  • use capital S in event.xml same as observer namespace " <observer name="DeliveryComment_storeSelector_save_city" instance="DeliveryComment\StoreSelector\Observer\SaveCity" /> " check the namespaces in all classes
    – Mehran
    Feb 3, 2023 at 11:40
  • somewhere you have write StoreSelector and somewhere storeSelector
    – Mehran
    Feb 3, 2023 at 11:43
  • All the namespaces are right but still doesn't want to save in the database
    – Bayzel
    Feb 6, 2023 at 4:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.