0

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' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
               'nullable' => true,
               'comment' => 'Input Room',
           ],

           'input_floor_shipping_field' => [
               'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
               'nullable' => true,
               'comment' => 'Input Floor',
           ],

           'select_custom_shipping_field' => [
               'type' => \Magento\Framework\DB\Ddl\Table::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

<extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
       <attribute code="input_room_shipping_field" type="string" />
       <attribute code="input_floor_shipping_field" type="string" />
       <attribute code="select_custom_shipping_field" type="string" />
   </extension_attributes>

I have an Observer in Observer>SaveCustomFieldsInOrder.php

class SaveCustomFieldsInOrder implements \Magento\Framework\Event\ObserverInterface
{
   /**
    * @param \Magento\Framework\Event\Observer $observer
    * @return $this
    */
   public function execute(\Magento\Framework\Event\Observer $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 \Magento\Checkout\Model\ShippingInformationManagement $subject
    * @param $cartId
    * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    */
   public function beforeSaveAddressInformation(
       \Magento\Checkout\Model\ShippingInformationManagement $subject,
       $cartId,
       \Magento\Checkout\Api\Data\ShippingInformationInterface $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 - Block\Order\View\OrderView.php

class OrderView extends \Magento\Backend\Block\Template
{

  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

<strong><?php echo __('Room Number') ?></strong>
  <span class="room"><?php echo $block->getInputRoomShippingField(); ?></span>
  <strong><?php echo __('Floor Number') ?></strong>
  <span class="floor"><?php echo $block->getInputFloorShippingField(); ?></span>
  <strong><?php echo __('Address') ?></strong>
  <span class="address"><?php echo $block->getSelectCustomShippingField(); ?></span>

And last in view>Adminhtml>layout>sales_order_view.xml

<referenceBlock name="order_info">
            <block class="xxx\xxx\Block\Order\View\OrderView" name="sales_order_view_custom" template="xxx_xxx::shipping_info.phtml" />
        </referenceBlock>

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!

Your Answer

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

Browse other questions tagged or ask your own question.