0

File Path :

SR\DeliveryDate\view\adminhtml\templates\order_info_shipping_info.phtml

code:

<section class="admin__page-section" id="delivery-date-content">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Delivery Date Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div id="delivery-date">
            <strong><?= __('Delivery Date') ?></strong>
            <span><?= $block->getDeliveryDate() ?></span>
        </div>
        <div id="delivery-comment">
            <strong><?= __('Comment') ?>:</strong>
            <br/>
            <?= $block->getDeliveryComment() ?>
        </div>
        <div id="delivery-test">
            <strong><?= __('Test') ?>:</strong>
            <br/>
            <?php
                $orderid = 33;
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);

                //fetch whole order information
                //print_r($order->getData());

                //Or fetch specific information
                //echo $order->getIncrementId();
                //echo $order->getGrandTotal();
                //echo $order->getSubtotal();
                echo $order->getDeliveryDropdown();
            ?>
        </div>
    </div>
</section>
<script type="text/javascript">
    require(
        ['jquery'],
        function($) {
            $('#delivery-date-content').insertAfter($('.order-view-billing-shipping'));
        }
    );
</script>

Line No : $orderid = 33;

I want get dynamically order number after place order.

Can anyone help create block and call to phtml file?

1

2 Answers 2

1

In this module get database values dynamically in admin panel edit some code this file :-

SR/DeliveryDate/Observer/AddHtmlToOrderShippingView.php and add this line in 64 number

$deliveryDateBlock->setDeliveryDropdown($order->getDeliveryDropdown());

Second open the this file SR/DeliveryDate/Observer/AddHtmlToOrderShippingBlock.php.And add this line

$deliveryDateBlock->setDeliveryDropdown($order->getDeliveryDropdown());

Final get values in template file

Go to template path :- SR/DeliveryDate/view/adminhtml/templates/order_info_shipping_info.phtml

add this

<div id="delivery-dropdown">
            <strong><?= __('Dropdown') ?>:</strong>
            <br/>
            <?= $block->getDeliveryDropdown(); ?>
        </div>

After check this output

Hope this help you

Thanks ...

1

app/code/SR/DeliveryDate/Block/Adminhtml

OrderList.php

<?php

namespace SR\DeliveryDate\Block\Adminhtml;

class OrderList extends \Magento\Backend\Block\Template
{
    protected $_orderCollectionFactory;
    protected $orderRepository;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        array $data = []
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->orderRepository = $orderRepository;
        parent::__construct($context, $data);
    }

    public function getOrderList($cust_id)
    {
        $order_collection = $this->_orderCollectionFactory->create();
        $order_collection->addFieldToFilter('customer_id', $cust_id);
        $order_collection->addAttributeToSelect('*');

        return $order_collection;
    }
    public function getOrderItems($order_id)
    {
        return $this->orderRepository->get($order_id);
    }
}

app/code/SR/DeliveryDate/view/adminhtml/templates

orderlist.phtml

<section class="admin__page-section" id="delivery-date-content">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Delivery Date Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div id="delivery-date">
            <strong><?= __('Delivery Date') ?></strong>
            <span><?= $block->getDeliveryDate() ?></span>
        </div>
        <div id="delivery-comment">
            <strong><?= __('Comment') ?>:</strong>
            <br/>
            <?= $block->getDeliveryComment() ?>
        </div>
        <div id="delivery-test">
            <strong><?= __('Test') ?>:</strong>
            <br/>
            <?php

                $cust_id = 1 // get your customer id here and assigne to this varible

                $order_data = $block->getOrderList($cust_id);

                //print_r($block->getOrderList($block->getLoggedinCustomerId())->getData()) ;


                // get all customer orders
                foreach ($order_data as $order_key => $order_value) {

                    // get order id
                    $order_id = $order_value->getIncrementId();

                    // get order data 
                    $order_items = $block->getOrderItems($order_id);

                    // print your data 
                    if (!empty($order_items)) {
                        echo $order_items->getIncrementId()." - ";
                        echo $order_items->getGrandTotal()." - ";
                        echo $order_items->getSubtotal()."<br/>";
                    } else {
                        echo "No Record Found";
                    }

                }
            ?>
        </div>
    </div>
</section>
<script type="text/javascript">
    require(
        ['jquery'],
        function($) {
            $('#delivery-date-content').insertAfter($('.order-view-billing-shipping'));
        }
    );
</script>

You can add this to your layout where you want to show order items list.

<block class="SR\DeliveryDate\Block\Adminhtml\OrderList" name="customer_order_items" template="SR_DeliveryDate::orderlist.phtml" cacheable="false"/>

Run Magento command

php bin/magento s:up
php bin/magento s:s:d-f 
php bin/magento c:c
php bin/magento c:f

I Hope This Helps You.

2
  • I have question. Can we put code from: orderlist.phtml directly to ourt template order_info_shipping_info.phtml ? I put this code directly to order_info_shipping_info.phtml but still something wrong. Thanks for check
    – Sylvester
    Commented May 2, 2020 at 13:07
  • You should change file name in layout also.
    – Msquare
    Commented May 2, 2020 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.