0

I am trying to pull the products from the order on the success page, but it keeps returning with "0" when I do var_dump.

<?php
namespace CJ\CJTracking\Block\Checkout;

class Success extends \Magento\Checkout\Block\Onepage\Success
{

    protected $_storeManager;
    protected $_currency;
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var int
     */
    protected $orderId;

    /**
     * @var array
     */
    protected $lastOrder;

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Directory\Model\Currency $currency,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\Order\Config $orderConfig,
    \Magento\Framework\App\Http\Context $httpContext,
    array $data = [],
    \Magento\Sales\Model\OrderFactory $orderFactory
    ) {
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;
        $this->checkoutSession = $checkoutSession;
        $this->_orderFactory = $orderFactory;
        parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
    }
    protected function _prepareLayout()
    {
        $lastOrder = $this->checkoutSession->getLastRealOrder();
        $this->orderId = $lastOrder->getIncrementId();
        $this->lastOrder = $lastOrder->getData();
        return parent::_prepareLayout();
    }

    public function getOrderId()
    {
        return $this->orderId;
    }

    public function getAllVisibleitems()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->orderId);
        return $order->getAllVisibleitems();
    }

    public function getCurrentCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }

    public function getGrandTotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->orderId);
        return $order->getGrandTotal();
    }

    public function getSubtotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->orderId);
        return $order->getSubtotal();
    }

    public function getDiscountAmount()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->orderId);
        return $order->getDiscountAmount();
    }
    public function getCouponCode()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->orderId);
        return $order->getCouponCode();
    }

}

and the phtml is:

<?php
$cjevent = "";

$cjevent = isset($_COOKIE["cjevent"]) ? $_COOKIE["cjevent"] : '';

$order = $block->getOrderId();
$items = $block->getAllVisibleitems();
var_dump($items);
$itemstrings = "";
$itemcount = 0;
$discount = $block->getDiscountAmount();
$coupon = $block->getCouponCode();
$currency = $block->getCurrentCurrencyCode();
echo "#currency: ".$currency;
$amount = $block->getSubtotal();
$customer_email = $block->getCustomerEmail();

/** avoiding double ITEM parameters for the same sku */
$last_checked = "";
/** if order count > 1 we need item based parameter strings */
if(count($items)  > 0){
foreach($items as $item){

    if($item->getSku() != $last_checked){
        $itemcount++;
    $last_checked = $item->getSku();
    $stripped_sku = str_replace('#','',$last_checked);
    $itemcount_float = floatval($item->getQtyOrdered());
    $rounded_count = round($itemcount_float,0);
    $rounded_amount = round($item->getPrice(),2);
    $itemstrings .= "&ITEM".$itemcount."=".$stripped_sku."&AMT".$itemcount."=".$rounded_amount."&QTY".$itemcount
    ."=".$rounded_count."&DCNT".$itemcount."=".$item->getBaseDiscountAmount();
    }
}}

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {
        $utype = "407887";
    } else {
        $utype = "407886";
    }

?>

<img class="d-none" src="https://www.emjcd.com/u?TYPE=<?= $utype; ?>&CID=1550236<?= $itemstrings; ?>&COUPON=<?= $coupon; ?>&OID=<?= $order; ?>&CURRENCY=&CJEVENT=<?= $cjevent; ?>&METHOD=IMG">

UPDATED

I just replaced getItemsCollection with getAllVisibleitems, but still, I cannot access the order data for some reason!

2
  • Check the sizeof items collecction by sizeof($items), if its greater than 0 then try to print $item->getData() in foreach loop. Dec 7, 2018 at 9:33
  • When I did this, it did pull the order data properly, but still, I got an error when I tried to $item->getSku();
    – Ibrahim S
    Dec 7, 2018 at 10:27

2 Answers 2

0

In success block, please update getItemsCollection() with below code:

public function __construct(
    ....
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    ...
) {
    ...
    $this->_objectManager = $objectmanager;
    ...
}

public function getItemsCollection()
{
    $orderId = $this->_objectManager->create('Magento\Checkout\Model\Session')->getLastOrderId();
    $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
    $orderItems = $order->getAllVisibleItems();

    return $orderItems; 
}

Check and let me know how it goes.. I didn't check it at my end.

Updates:

Update my code, please check by adding new dependency of object manager and clearing cache and generation files.

3
  • Exception #0 (InvalidArgumentException): Invalid method Magento\Checkout\Model\Session\Interceptor::create(Array ( ) )
    – Ibrahim S
    Dec 7, 2018 at 10:20
  • Try updated answer.. don't forget to remove generation files (generated/code) after updates. Dec 7, 2018 at 12:35
  • I do not think is the proper way to have this done, like the items already returning with the proper data, but it stops when I do try to get the sku from $items->getData();
    – Ibrahim S
    Dec 7, 2018 at 23:27
0

In below line you getting Order Increment Id

$this->orderId = $lastOrder->getIncrementId();

but when you load your order object , you are trying with Order entity id as place of increment id so try to change below line

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

with below line with order increment id

$order = $this->_orderFactory->create()->loadByIncrementId($this->orderId);

in getAllVisibleitems method.

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.