Question

I am working in \success.phtml file, with the final objective of placing a tracking iframe on the confirmation page.

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile
?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success  **/?>


<div class="checkout-success">
<?php if ($block->getOrderId()):?>
    <?php if ($block->getCanViewOrder()) :?>
        <p><?= __('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
    <?php  else :?>
        <p><?= __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
    <?php endif;?>
        <p><?= /* @escapeNotVerified */ __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
<?php endif;?>

<?= $block->getAdditionalInfoHtml() ?>

<div class="actions-toolbar">
    <div class="primary">
        <a class="action primary continue" href="<?= /* @escapeNotVerified 
         */ $block->getContinueUrl() ?>"><span><?= /* @escapeNotVerified */ 
         __('Continue Shopping') ?></span></a>
    </div>
</div>
</div>
<div>
<?php
    $block_methods = get_class_methods($block);
?>
</div>
<script type="text/javascript">
    var obj = <?php echo json_encode($block_methods); ?>;
    console.log(obj);
</script>

I am trying to access the product/shopping cart info of the order, so I made a get_class_methods($block) call as seen above. The output said that $block has 80 different methods, but none of the methods listed were getOrderId(), which confuses me since that method is used in the file. Can anyone explain this? Also would it be possibly to access the methods of a $block class from a different file? I am pretty new to coding so I am pretty confused by this.

Was it helpful?

Solution

Throughout, Magento uses a range of magic methods (they start with get, set, uns and has) which do not appear in the method list.

These methods are shortcuts to access data that has been set on the object. Most of these classes extend from the base class \Magento\Framework\DataObject including block classes.

In this particular case the block is initialised with data here vendor/magento/module-checkout/Block/Onepage/Success.php

protected function prepareBlockData()
{
    $order = $this->_checkoutSession->getLastRealOrder();

    $this->addData(
        [
            'is_order_visible' => $this->isVisible($order),
            'view_order_url' => $this->getUrl(
                'sales/order/view/',
                ['order_id' => $order->getEntityId()]
            ),
            'print_url' => $this->getUrl(
                'sales/order/print',
                ['order_id' => $order->getEntityId()]
            ),
            'can_print_order' => $this->isVisible($order),
            'can_view_order'  => $this->canViewOrder($order),
            'order_id'  => $order->getIncrementId()
        ]
    );
}

Of interest is the last line where we add the order increment to the data property.

This can then later be accessed via $block->getOrderId() which can also be stated differently with $block->getData('order_id').

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