我创建了一个模块,它在结帐时显示运输中的自定义字段,并将数据库中的数据保存在报价和销售订单表中,我可以确认数据在那里。我正在尝试在查看订单时以及在发出的确认电子邮件中显示数据。我有它,因此模板在管理订单视图中显示回显文本,但不显示数据库中的数据。电子邮件中目前没有显示任何内容,但我想先修复按顺序查看数据。
为了分解它,我在数据库的两个表中有数据,需要在管理员的订单上显示它。我在 extension_attributes.xml 中有字段
这些字段在 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());
}
}
但是,到目前为止,这是我想出的在管理员中按顺序查看数据的方法,但没有运气。
首先 - 查看>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>
接下来查看>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
我可以在 admin > order 视图中看到上面的回显文本,但看不到 $block 调用的值。
下一步 - 块>Adminhtml>Order>View>OrderView.php
namespace xxx\xxx\Block\Adminhtml\Order\View;
class OrderView extends \Magento\Backend\Block\Template
{
}
我想我需要上面的,所以我可以调用 $block
下一步 - 观察者>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);
}
}
}
使用上面的方法获取数据以显示在 admin > sales > order 中,并使用上面的 OrderView.php。我看到了文字,但没有看到数据。
就像我说的那样,我正在尝试获取数据库中的数据(存在)以显示在管理员的订单上,并最终显示在电子邮件中。
我不知道这是否太令人困惑而无法获得帮助。任何想法都非常感谢。