Skip to content

Question: Object Manager objects' get methods return 1 #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CoreyCole opened this issue Aug 28, 2015 · 7 comments
Closed

Question: Object Manager objects' get methods return 1 #1760

CoreyCole opened this issue Aug 28, 2015 · 7 comments
Labels
Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed

Comments

@CoreyCole
Copy link

Hello, I'm trying to print data to the success page about the most recent order placed. In app/code/Magento/Checkout/Controller/Onepage/Success.php I've added the following php:

<?php
$orderID = $session->getLastOrderId();
$orderData = $this->_objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderID);
$orderItems = $orderData->getAllItems();
$orderString = "";
foreach ($orderItems as $item) {
    $orderString = $orderString.$item->getSku();
}
$orderAddress = $orderData->getShippingAddress();
?>
<div id="order-data">
    <pre id="order-number"><?= $orderID ?></pre>
    <pre id="order-items"><?= print_r($orderItems) ?></pre>
    <pre id="order-items-string"><?= print_r($orderString) ?></pre>
    <pre id="order-address"><?= print_r($orderAddress) ?></pre>
    <pre id="order-name"><?= print_r($orderData->getFirstName()) ?></pre>
</div>

I know it's working to some degree. The page is loading without errors and in the admin panel, I confirmed that the order is being placed with the printed $orderID with valid data. However, only the $orderID seems to be printing correctly. The output div comes out as follows:

<div id="order-data">
    <!-- correct ID -->
    <pre id="order-number">63</pre> 
    <!-- looks like an empty array followed by a 1? -->
    <pre id="order-items">
        Array
        (
        )
        1
    </pre> 
    <pre id="order-items-string">1</pre> <!-- all the other get methods return "1" -->
    <pre id="order-address">1</pre>
    <pre id="order-name">1</pre>
</div>

Sorry if this is a rudimentary question, but I've been struggling with this for a while. I can't find any pages in the php developer guide docs about interacting with the model. What am I doing wrong?

@Vinai
Copy link
Contributor

Vinai commented Aug 28, 2015

At least one thing that is wrong on first glance is that you are printing the return value of [print_r()](http://php.net/manual/en/function.print-r.php), which by default is a boolean true, after the string representation of the argument.
In order to make print_r() return the string representation of the argument, you have to pass a truthy second argument, e.g. <?= print_r($orderItems, true) ?>.
Otherwise it seems that all the arguments you are dumping out however are empty, so they are output as an empty string.

@vpelipenko vpelipenko added the CS label Aug 31, 2015
@CoreyCole
Copy link
Author

Thank @Vinai! I was not aware of the second param to print_r. I figured out my error though.

$orderID = $session->getLastOrderId()

returns an integer while

loadByIncrementId()

looks for the string with leading zeros. Calling:

loadByIncrementId("0000000".$orderID);

temporarily fixed it, but I'll look for a method that returns the id as the full string.

@Vinai
Copy link
Contributor

Vinai commented Aug 31, 2015

Great! Just a plain old $order->load($orderId) should do the trick.
By the way, are you aware that using the object manager directly to instantiate the objects you want is not the right way to do things?

@CoreyCole
Copy link
Author

No I was unaware - what is the better way?

@Vinai
Copy link
Contributor

Vinai commented Aug 31, 2015

You simply declare the dependencies you need in your class constructor, and they will be automatically injected when your class in created by the object manager.
Entities like orders however are a special case, so called non-injectables. Even though technically you could have them injected just like any other object, you want to inject a entity factory in such a case.
For example, if you want access to an order, you would declare a dependency on the OrderFactory in your constructor.

public function __construct(\Magento\Sales\Model\OrderFactory $orderFactory)
{
    $this->orderFactory = $orderFactory;
}

Now, every time you need an order instance, you call

$order = $this->orderFactory->create()->load($orderId);

Since you where working in a core class and outputting HTML in a PHP class I assumed you where just playing around, which is fine. But if you want to do real customizations, that is not the best thing to do.

@CoreyCole
Copy link
Author

Thanks for all the help @Vinai! That worked, but for anyone else looking at this, I also had to add the field above the constructor.

/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderFactory;

magento-team pushed a commit that referenced this issue Nov 24, 2017
[EngCom] Public Pull Requests - 2.1-develop
 - MAGETWO-84238: #10765 Export data from grid not adding custom rendered data magento2 #12373
 - MAGETWO-83282: Backport 2.1-develop] Fix datetime type product that show current date when is empty in grids #12033
@magento-engcom-team magento-engcom-team added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Aug 26, 2020
@magento-engcom-team
Copy link
Contributor

Hi @CoreyCole. Thank you for your report.
The issue has been fixed in #29632 by @jmonteros422 in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.2 release.

@magento-engcom-team magento-engcom-team added the Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed label Aug 26, 2020
magento-engcom-team added a commit that referenced this issue Aug 26, 2020
…ed Media Gallery' disabled #29632

 - Merge Pull Request #29632 from jmonteros422/magento2-1:1760-media-gallery-page-and-category-grid-page-opened-successfully-if-enhanced-media-gallery-disabled
 - Merged commits:
   1. afe499d
   2. 0994c44
   3. 5a70b81
   4. 2267021
magento-engcom-team pushed a commit that referenced this issue Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed
Projects
None yet
Development

No branches or pull requests

4 participants