Question

I have a custom shipping module that passes some information into the Sales Order and the email. The problem is that it shows the data even when the shipping method isn't used. How can I get the shipping method so I can use it to only display the information if that shipping method is chosen?

This adds my data into the Sales Order in the Admin - view/adminhtml/templates/order/view

<?php
$formFields = $block->getOrderFormFields();

// Get Shipping Method or Code
$shippingMethod = $order->getShippingMethod();

?>
<?php if($shippingMethod == 'shippingmethod_shippingmethod'): ?>
    <section class="admin__page-section">
        <div class="admin__page-section-title">
            <span class="title"><?php /* @escapeNotVerified */ echo __('Shipping Option') ?></span>
        </div>
        <div class="admin__page-section-content">
            <div class="order-information">
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Building Address') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutBuildingAddress()); ?>
                    </div>
                </div>
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Floor Number') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutFloorNumber()); ?>
                    </div>
                </div>
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Room Number') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutRoomNumber()); ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?php endif; ?>

This is in Block/Order

namespace Vendor\Module\Block\Order;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order;

/**
 * Class CustomFields
 *
 * @category Block/Order
 * @package  Vendor\Module\Block
 */

class OrderItems extends \Magento\Backend\Block\Template
{
    protected $order;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Sales\Model\Order $order,
        array $data = []
    ) {
        $this->order = $order;
        parent::__construct($context, $data);
    }

    /**
     * Get current order
     *
     * @return Order
     */

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getOrder()
    {
        $order = $this->order->loadByIncrementId($orderId);

        return $order;

    }

}

I get this error:

Undefined variable: order

How can I get the shipping code or method from the order?

Thanks, Stan

Was it helpful?

Solution

Please follow the below steps to get the shipping method and its details in phtml file.

app/code/Test/RoomDelivery/view/adminhtml/layout/sales_order_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales_order_edit">
            <block class="Test\RoomDelivery\Block\Adminhtml\Order\CustomFields" name="order_custom_fields" template="Test_RoomDelivery::order/view/custom_fields.phtml" />
        </referenceBlock>
    </body>
</page>

app/code/Test/RoomDelivery/Block/Adminhtml/Order/CustomFields.php

declare(strict_types=1);

namespace Test\RoomDelivery\Block\Adminhtml\Order;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order; 

/**
 * Class CustomFields
 *
 * @category Block/Order
 * @package  Test\RoomDelivery\Block\Adminhtml\Order
 */
class CustomFields extends Template
{
   
    /**
     * Core registry
     *
     * @var Registry
     */
    protected $coreRegistry = null;

    /**
     * CustomFieldsRepositoryInterface
     *
     * @var CustomFieldsRepositoryInterface
     */
    protected $customFieldsRepository;

    /**
     * CustomFields constructor.
     *
     * @param Context                         $context                Context
     * @param Registry                        $registry               Registry
     * @param CustomFieldsRepositoryInterface $customFieldsRepository CustomFieldsRepositoryInterface
     * @param array                           $data                   Data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        $this->coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Get current order
     *
     * @return Order
     */
    public function getOrder() : Order
    {
        return $this->coreRegistry->registry('current_order');
    } 
}

app/code/Test/RoomDelivery/view/adminhtml/templates/order/view/custom_fields.phtml

$order = $block->getOrder();
$orderDetails = $order->getId();
echo 'details ' . $orderDetails . '<br />';
echo $order->getShippingDescription();

Please let me know if you are facing any issue.

I hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top