I have custom fields at checkout being saved into quote and sales_order tables.
InstallSchema.php
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/* While module install, creates columns in quote_address and sales_order_address table */
$eavTable1 = $installer->getTable('quote');
$eavTable2 = $installer->getTable('sales_order');
$columns = [
'input_room_shipping_field' => [
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'nullable' => true,
'comment' => 'Input Room',
],
'input_floor_shipping_field' => [
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'nullable' => true,
'comment' => 'Input Floor',
],
'select_custom_shipping_field' => [
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'nullable' => true,
'comment' => 'Select Custom Address',
],
];
$connection = $installer->getConnection();
foreach ($columns as $name => $definition) {
$connection->addColumn($eavTable1, $name, $definition);
$connection->addColumn($eavTable2, $name, $definition);
}
$installer->endSetup();
}
}
And I am using extension_attributes.xml
I have an Observer in Observer>SaveCustomFieldsInOrder.php
class SaveCustomFieldsInOrder implements MagentoFrameworkEventObserverInterface
{
/**
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
$quote = $observer->getEvent()->getQuote();
$order->setData(
"input_room_shipping_field",
$quote->getInputRoomShippingField()
);
$order->setData(
"input_floor_shipping_field",
$quote->getInputFloorShippingField()
);
$order->setData(
"select_custom_shipping_field",
$quote->getSelectCustomShippingField()
);
return $this;
}
}
My plugin – Plugin>checkout>LayoutProcessor.php
class LayoutProcessorPlugin
{
/**
* @param LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(
LayoutProcessor $subject,
array $jsLayout
) {
$validation['required-entry'] = true;
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['custom-shipping-method-fields']['children']['input_room_shipping_field'] = [
'component' => "Magento_Ui/js/form/element/abstract",
'config' => [
'customScope' => 'customShippingMethodFields',
'template' => 'ui/form/field',
'elementTmpl' => "ui/form/element/input",
'id' => "input_room_shipping_field"
],
'dataScope' => 'customShippingMethodFields.custom_shipping_field[input_room_shipping_field]',
'label' => "Input Room",
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => $validation,
'sortOrder' => 2,
'id' => 'custom_shipping_field[input_room_shipping_field]'
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['custom-shipping-method-fields']['children']['input_floor_shipping_field'] = [
'component' => "Magento_Ui/js/form/element/abstract",
'config' => [
'customScope' => 'customShippingMethodFields',
'template' => 'ui/form/field',
'elementTmpl' => "ui/form/element/input",
'id' => "input_floor_shipping_field"
],
'dataScope' => 'customShippingMethodFields.custom_shipping_field[input_floor_shipping_field]',
'label' => "Input Floor",
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => $validation,
'sortOrder' => 3,
'id' => 'custom_shipping_field[input_floor_shipping_field]'
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['custom-shipping-method-fields']['children']['select_custom_shipping_field'] = [
'component' => "Magento_Ui/js/form/element/select",
'config' => [
'customScope' => 'customShippingMethodFields',
'template' => 'ui/form/field',
'elementTmpl' => "ui/form/element/select",
'id' => "select_custom_shipping_field"
],
'dataScope' => 'customShippingMethodFields.custom_shipping_field[select_custom_shipping_field]',
'label' => "Select option",
'options' => $this->getSelectOptions(),
'caption' => 'Please select',
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => $validation,
'sortOrder' => 4,
'id' => 'custom_shipping_field[select_custom_shipping_field]'
];
return $jsLayout;
}
/**
* @return array
*/
protected function getSelectOptions()
{
$items[1]["value"] = "123 Any Street, Bldg A, MyTown, PA 19123";
$items[1]["label"] = "123 Any Street, Bldg A, MyTown, PA 19123";
$items[2]["value"] = "123 Any Street, Bldg B, MyTown, PA 19123";
$items[2]["label"] = "123 Any Street, Bldg B, MyTown, PA 19123";
// $items[3]["value"] = "123 Any Street, Bldg C, MyTown, PA 19123";
// $items[3]["label"] = "Address #3";
//
// $items[4]["value"] = "123 Any Street, Bldg D, MyTown, PA 19123";
// $items[4]["label"] = "Address #4";
//
// $items[5]["value"] = "123 Any Street, Bldg #, MyTown, PA 19123";
// $items[5]["label"] = "Address #5";
return $items;
}
}
And the last plugin – Plugin>Quote
public function __construct(
QuoteRepository $quoteRepository
) {
$this->quoteRepository = $quoteRepository;
}
/**
* @param MagentoCheckoutModelShippingInformationManagement $subject
* @param $cartId
* @param MagentoCheckoutApiDataShippingInformationInterface $addressInformation
*/
public function beforeSaveAddressInformation(
MagentoCheckoutModelShippingInformationManagement $subject,
$cartId,
MagentoCheckoutApiDataShippingInformationInterface $addressInformation
) {
if(!$extAttributes = $addressInformation->getExtensionAttributes())
return;
$quote = $this->quoteRepository->getActive($cartId);
$quote->setInputRoomShippingField($extAttributes->getInputRoomShippingField());
$quote->setInputFloorShippingField($extAttributes->getInputFloorShippingField());
$quote->setDateCustomShippingField($extAttributes->getDateCustomShippingField());
$quote->setSelectCustomShippingField($extAttributes->getSelectCustomShippingField());
}
}
And I am trying to display all my data in a block – BlockOrderViewOrderView.php
class OrderView extends MagentoBackendBlockTemplate
{
public function getInputRoomShippingField ()
{
//some code here that gets the inputroomshippingfield and returns it
$data = "This is Room data";
return $data;
}
}
It uses this template view>Adminhtml>templates>shipping_info.php
< ?php echo __('Room Number') ?>
< ?php echo $block->getInputRoomShippingField(); ?>
< ?php echo __('Floor Number') ?>
< ?php echo $block->getInputFloorShippingField(); ?>
< ?php echo __('Address') ?>
< ?php echo $block->getSelectCustomShippingField(); ?>
And last in view>Adminhtml>layout>sales_order_view.xml
I can see the text in shipping_info.php but not any of the data. I have tried a bunch of things in OrderView.php with no luck. How do I get the data from the users submission to show in the block?
Thanks!