I created a module that displays custom fields in shipping at checkout and saves the data in the database in the both the quote and sales order table and I can confirm the data is there. I am trying to get the data to show in the admin when the orders are viewed and in the confirmation email that goes out. I have it so the template displays the echoed text in the admin order view but not the data from the database. Nothing shows currently in the email but I want to fix viewing the data in the order first.
To break it down, I have the data in the two tables in the DB and need to display it on the orders in the admin. I have the fields in extension_attributes.xml
The fields get saved to the DB here in Plugin > Quote > SaveToQuote.php:
class SaveToQuote
{
/**
* @var QuoteRepository
*/
protected $quoteRepository;
/**
* SaveToQuote constructor.
* @param QuoteRepository $quoteRepository
*/
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());
}
}
But here is what I have come up with to view the data in the order in the admin so far with no luck.
First - view>adminhtml>layout>sales_order_view.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="order_info">
<block class="xxx\xxx\Block\Adminhtml\Order\View\OrderView" name="sales_order_view_custom" template="xxx_xxx::shipping_info.phtml" />
</referenceBlock>
</body>
Next in view>adminhtml>templates>shipping_info.phtml
<strong><?php echo __('Room Number') ?></strong>
<span class="room"><?=$block->getInputRoomShippingField(); ?></span>
<strong><?php echo __('Floor Number') ?></strong>
<span class="floor"><?=$block->getInputFloorShippingField(); ?></span>
<strong><?php echo __('Address') ?></strong>
<span class="address"><?=$block->getSelectCustomShippingField(); ?></span
I can see the echoed text above in the admin > order view but not the values called by $block.
Next - Block>Adminhtml>Order>View>OrderView.php
namespace xxx\xxx\Block\Adminhtml\Order\View;
class OrderView extends \Magento\Backend\Block\Template
{
}
I think I need the above so I can call $block
Next - Observer>ShippingDataToAdminObserver.php
namespace xxx\xxx\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class ShippingDataToAdminObserver implements ObserverInterface
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_block;
/**
* @param \Magento\Framework\View\Element\Template $block
*/
public function __construct(
\Magento\Framework\View\Element\Template $block
)
{
$this->_block = $block;
}
/**
* @param EventObserver $observer
*/
public function execute(EventObserver $observer)
{
if($observer->getElementName() == 'order_shipping_view')
{
$orderShippingViewBlock = $observer->getLayout()->getBlock($observer->getElementName());
$order = $orderShippingViewBlock->getOrder();
$RoomDeliveryBlock = $this->_block;
$RoomDeliveryBlock->setTemplate('xxx_xxx::shipping_info.phtml');
$html = $observer->getTransport()->getOutput() . $RoomDeliveryBlock->toHtml();
$observer->getTransport()->setOutput($html);
}
}
}
Used the above to get data to display in admin > sales > order and uses OrderView.php above. I see the text but not the data.
Like I said, I am trying to get the data in the database (which is present) to show on the orders in the admin and eventually emails.
I don't know if this is too confusing to get help or not. Any thoughts are greatly appreciated.