首页 > 解决方案 > 如何获取外键值并在 Magento 2 的运输和处理信息中显示?

问题描述

我想在管理面板中获取并显示外键值Order > Individual Order > Shipping and Handling Information。我有两个相互连接的表。即order_sales(具有stockid_id链接到的外键collect_in_store)和collect_in_store(具有名称的商店列表)。

  1. Company/Module/Block/Adminhtml/Order/View/Custom.php-
<?php

namespace Company\Module\Block\Adminhtml\Order\View;

class Custom extends \Magento\Framework\View\Element\Template
{
    protected $orderRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        parent::__construct($context, $data);
    }

    public function checkCollectInStore(){
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderRepository->get($orderId);
        return $order;
    }
}
  1. Company/Module/view/adminhtml/layout/sales_order_view.xmlreferenceContainer- 这里的运输块下应该显示什么名称?
<?xml version="1.0"?>
<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>
        <referenceContainer name="right">
            <referenceContainer name="shipping_info"> //This should be under Shipping block
                <block class="Company\Module\Block\Adminhtml\Order\View\Custom" name="sales_order_view_collect_in_store" template="order/view/custom.phtml" />
            </referenceContainer>
        </referenceContainer>
    </body>
</page>
  1. Company/Module/view/adminhtml/templates/order/view/custom.phtml-
<?php 
    $check = $block->checkCollectInStore();
    $shippingCode = $check->getShippingMethod();
    if($shippingCode === 'collectinstore_collectinstore'){
        echo 'Collect In Store: '.$check->getStockistId(); //getting id but need to display shop name.
    }
?> 

现在有了这个代码,我得到stockist_id但需要显示名称,它应该在详细订单页面的运输块下。如何解决这个问题?

标签: phpmagentomagento2

解决方案


推荐阅读