Question

I am currently attempting to modify my version of Magento ver. 2.3.4 by creating a new module to inject a custom attribute into the checkout success page.

The code in the files is as follows

registration.php

<?php
        \Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            'CustomTrade_CallAttribute',
            __DIR__
        );

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomTrade_CallAttribute" >
    </module>
</config>

Data.php

<?php

namespace CustomTrade\CallAttribute\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session as CustomerSession;
class Data extends AbstractHelper
{
    protected $customerRepository;
    public function __construct(
        Context $context,
        CustomerRepositoryInterface $customerRepository,
        CustomeSession $customerSession
        )
    {
        $this->customerRepository = $customerRepository;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }
    public function getOrderNumberValue()
    {
        $customerId  = $this->customerSession->getCustomer()->getId();
        $customer = $this->customerRepository->getById($customerId);
        return $customer->getData('trade_ordernumber');
    }
}

And I have modified the success.phtml file in the following way

<?php
/**
 * Copyright ? Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()) :?>
        <?php if ($block->getCanViewOrder()) :?>
                <h1><?= $block->escapeHtml(__('Your order # is: <span>%1</span>.', $block->getOrderId()), ['span']) ?></h1>
    
            <p><?= $block->escapeHtml(__('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeUrl($block->getViewOrderUrl()), $block->getOrderId())), ['a', 'strong']) ?></p>
        <?php  else :?>
            <p><?= $block->escapeHtml(__('Your order # is: <span>%1</span>.', $block->getOrderId()), ['span']) ?></p>
        <?php endif;?>
            <p><?= $block->escapeHtml(__('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="<?= $block->escapeUrl($block->getContinueUrl()) ?>"><span><?= $block->escapeHtml(__('Continue Shopping')) ?></span></a>
        </div>
    </div>
</div>



<?php
$dataHelper = $this->helper('CustomTrade\CallAttribute\Helper\Data');
echo $dataHelper->getOrderNumberValue();
?>

Unfortunately I now seem to be getting the following error message instead of the usual success page.

    1 exception(s):
    Exception #0 (Exception): Recoverable Error: Object of class Magento\Framework\Api\AttributeValue could not be converted to string in /var/www/vhosts/mysite.com/httpdocs/app/design/frontend/Infortis/ultimo_child/Magento_Checkout/templates/success.phtml on line 33
    
    Exception #0 (Exception): Recoverable Error: Object of class Magento\Framework\Api\AttributeValue could not be converted to string in /var/www/vhosts/mysite.com/httpdocs/app/design/frontend/Infortis/ultimo_child/Magento_Checkout/templates/success.phtml on line 33

<pre>#1 include('/var/www/vhosts/...') called at [vendor/magento/framework/View/TemplateEngine/Php.php:59]
#2 Magento\Framework\View\TemplateEngine\Php->render(&Magento\Checkout\Block\Onepage\Success#0000000035b33d640000000055dab643#, '/var/www/vhosts/...', array()) called at [vendor/magento/framework/View/Element/Template.php:271]
#3 Magento\Framework\View\Element\Template->fetchView('/var/www/vhosts/...') called at [vendor/magento/framework/View/Element/Template.php:301]
#4 Magento\Framework\View\Element\Template->_toHtml() called at [vendor/magento/framework/View/Element/AbstractBlock.php:1099]
#5 Magento\Framework\View\Element\AbstractBlock->Magento\Framework\View\Element\{closure}() called at [vendor/magento/framework/View/Element/AbstractBlock.php:1103]
#6 Magento\Framework\View\Element\AbstractBlock->_loadCache() called at [vendor/magento/framework/View/Element/AbstractBlock.php:673]
#7 Magento\Framework\View\Element\AbstractBlock->toHtml() called at [vendor/magento/framework/View/Layout.php:564]
#8 Magento\Framework\View\Layout->_renderBlock('checkout.success') called at [vendor/magento/framework/View/Layout.php:540]
#9 Magento\Framework\View\Layout->renderNonCachedElement('checkout.success') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#10 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('checkout.success') called at [vendor/magento/framework/View/Layout.php:495]
#11 Magento\Framework\View\Layout->renderElement('checkout.success', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#12 Magento\Framework\View\Layout\Interceptor->renderElement('checkout.success', false) called at [vendor/magento/framework/View/Layout.php:592]
#13 Magento\Framework\View\Layout->_renderContainer('content', false) called at [vendor/magento/framework/View/Layout.php:542]
#14 Magento\Framework\View\Layout->renderNonCachedElement('content') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#15 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('content') called at [vendor/magento/framework/View/Layout.php:495]
#16 Magento\Framework\View\Layout->renderElement('content', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#17 Magento\Framework\View\Layout\Interceptor->renderElement('content', false) called at [vendor/magento/framework/View/Layout.php:592]
#18 Magento\Framework\View\Layout->_renderContainer('main', false) called at [vendor/magento/framework/View/Layout.php:542]
#19 Magento\Framework\View\Layout->renderNonCachedElement('main') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#20 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('main') called at [vendor/magento/framework/View/Layout.php:495]
#21 Magento\Framework\View\Layout->renderElement('main', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#22 Magento\Framework\View\Layout\Interceptor->renderElement('main', false) called at [vendor/magento/framework/View/Layout.php:592]
#23 Magento\Framework\View\Layout->_renderContainer('columns', false) called at [vendor/magento/framework/View/Layout.php:542]
#24 Magento\Framework\View\Layout->renderNonCachedElement('columns') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#25 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('columns') called at [vendor/magento/framework/View/Layout.php:495]
#26 Magento\Framework\View\Layout->renderElement('columns', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#27 Magento\Framework\View\Layout\Interceptor->renderElement('columns', false) called at [vendor/magento/framework/View/Layout.php:592]
#28 Magento\Framework\View\Layout->_renderContainer('main.content', false) called at [vendor/magento/framework/View/Layout.php:542]
#29 Magento\Framework\View\Layout->renderNonCachedElement('main.content') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#30 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('main.content') called at [vendor/magento/framework/View/Layout.php:495]
#31 Magento\Framework\View\Layout->renderElement('main.content', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#32 Magento\Framework\View\Layout\Interceptor->renderElement('main.content', false) called at [vendor/magento/framework/View/Layout.php:592]
#33 Magento\Framework\View\Layout->_renderContainer('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:542]
#34 Magento\Framework\View\Layout->renderNonCachedElement('base-main-contai...') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#35 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('base-main-contai...') called at [vendor/magento/framework/View/Layout.php:495]
#36 Magento\Framework\View\Layout->renderElement('base-main-contai...', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#37 Magento\Framework\View\Layout\Interceptor->renderElement('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:592]
#38 Magento\Framework\View\Layout->_renderContainer('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:542]
#39 Magento\Framework\View\Layout->renderNonCachedElement('base-main-contai...') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#40 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('base-main-contai...') called at [vendor/magento/framework/View/Layout.php:495]
#41 Magento\Framework\View\Layout->renderElement('base-main-contai...', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#42 Magento\Framework\View\Layout\Interceptor->renderElement('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:592]
#43 Magento\Framework\View\Layout->_renderContainer('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:542]
#44 Magento\Framework\View\Layout->renderNonCachedElement('base-main-contai...') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#45 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('base-main-contai...') called at [vendor/magento/framework/View/Layout.php:495]
#46 Magento\Framework\View\Layout->renderElement('base-main-contai...', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#47 Magento\Framework\View\Layout\Interceptor->renderElement('base-main-contai...', false) called at [vendor/magento/framework/View/Layout.php:592]
#48 Magento\Framework\View\Layout->_renderContainer('page.wrapper', false) called at [vendor/magento/framework/View/Layout.php:542]
#49 Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#50 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper') called at [vendor/magento/framework/View/Layout.php:495]
#51 Magento\Framework\View\Layout->renderElement('page.wrapper', false) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#52 Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper', false) called at [vendor/magento/framework/View/Layout.php:592]
#53 Magento\Framework\View\Layout->_renderContainer('root', false) called at [vendor/magento/framework/View/Layout.php:542]
#54 Magento\Framework\View\Layout->renderNonCachedElement('root') called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#55 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root') called at [vendor/magento/framework/View/Layout.php:495]
#56 Magento\Framework\View\Layout->renderElement('root', true) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#57 Magento\Framework\View\Layout\Interceptor->renderElement('root') called at [vendor/magento/framework/View/Layout.php:961]
#58 Magento\Framework\View\Layout->getOutput() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#59 Magento\Framework\View\Layout\Interceptor->___callParent('getOutput', array()) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#60 Magento\Framework\View\Layout\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#61 Magento\Framework\View\Layout\Interceptor->___callPlugins('getOutput', array(), array(array('layout-model-cac...'))) called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:494]
#62 Magento\Framework\View\Layout\Interceptor->getOutput() called at [vendor/magento/framework/View/Result/Page.php:258]
#63 Magento\Framework\View\Result\Page->render(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#) called at [vendor/magento/framework/View/Result/Layout.php:171]
#64 Magento\Framework\View\Result\Layout->renderResult(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#) called at [vendor/magento/framework/Interception/Interceptor.php:58]
#65 Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', array(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#)) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#66 Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#) called at [vendor/magento/framework/Interception/Interceptor.php:153]
#67 Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', array(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#), array(array('result-messages', 'result-builtin-c...', 'result-varnish-c...'))) called at [generated/code/Magento/Framework/View/Result/Page/Interceptor.php:130]
#68 Magento\Framework\View\Result\Page\Interceptor->renderResult(&Magento\Framework\App\Response\Http\Interceptor#0000000035b30d000000000055dab643#) called at [vendor/magento/framework/App/Http.php:120]
#69 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#70 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#71 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#0000000035b30d1b0000000055dab643#) called at [index.php:42]
</pre>

I am not sure why this is having trouble converting into a string. Can anyone shed some light on this issue?

Thank you.

EDIT 1

Following advice from Nadeem Sayyed I did a Var dump and also got the following result:

object(Magento\Framework\Api\AttributeValue)#3737 (1) 
{ ["_data":protected]=> array(2) { ["attribute_code"]=> string(17) "trade_ordernumber" ["value"]=> string(15) "TRADEORDER00123" } }

EDIT 2

I now seem to have gotten this working: the Data.php file now looks as follows

    <?php

namespace CustomTrade\CallAttribute\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
    protected $customerRepository;
    public function __construct(
        Context $context,
        CustomerRepositoryInterface $customerRepository,
        CustomerSession $customerSession
        )
    {
        $this->customerRepository = $customerRepository;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function getOrderNumberValue()
    {
        $customerId  = $this->customerSession->getCustomer()->getId();
        $customer = $this->customerRepository->getById($customerId);
        return $customer->getCustomAttribute('trade_ordernumber')->getValue();
    }
}

And the success.phtml file is here

    <?php
/**
 * Copyright ? Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()) :?>
        <?php if ($block->getCanViewOrder()) :?>
                <h1><?= $block->escapeHtml(__('Your order # is: <span>%1</span>.', $block->getOrderId()), ['span']) ?></h1>
    
            <p><?= $block->escapeHtml(__('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeUrl($block->getViewOrderUrl()), $block->getOrderId())), ['a', 'strong']) ?></p>
        <?php  else :?>
            <p><?= $block->escapeHtml(__('Your order # is: <span>%1</span>.', $block->getOrderId()), ['span']) ?></p>
        <?php endif;?>
            <p><?= $block->escapeHtml(__('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="<?= $block->escapeUrl($block->getContinueUrl()) ?>"><span><?= $block->escapeHtml(__('Continue Shopping')) ?></span></a>
        </div>
    </div>
</div>


<?php
    $dataHelper = $this->helper('CustomTrade\CallAttribute\Helper\Data');
    echo $dataHelper->getOrderNumberValue();
?>

This seems to produce the correct result.

Was it helpful?

Solution

It seems that $dataHelper->getOrderNumberValue() is returning an object instead of string.

You could do a var_dump($dataHelper->getOrderNumberValue()) and see the return value of the function getOrderNumberValue

As a good developer I would do the following if I am expecting string as a return

public function getOrderNumberValue(): string
{
    $customerId  = $this->customerSession->getCustomer()->getId();
    $customer = $this->customerRepository->getById($customerId);
    return $customer->getData('trade_ordernumber');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top