0

I am trying to get the ORDER ID, BaseSubtotalInclTax, BaseShippingInclTax, and TaxAmount on the success.phtml page by using custom block.

On Magento 1 I had to put the following script in the success.phtml file.

    <?php 
    $orderId = $this->getOrderId();
    $order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
    ?>

    order_id: '<?php echo $this->escapeHtml($this->getOrderId()); ?>',
    revenue:  '<?php echo $order->getBaseSubtotalInclTax(); ?>',  
    shipping: '<?php echo $order->getBaseShippingInclTax(); ?>',
    Total Shipping Cost. <?php $shippingc= $order->getBaseCodTaxAmount(); 
    if ($shippingc!= NULL)
    {
        $finaltax= ($order->getTaxAmount() - $shippingc);
    }
    else
    {
        $finaltax= $order->getTaxAmount();
    }
    ?>
    tax:      '<?php echo $finaltax; ?>'    // Total Tax.
    }));



    <?php
    $orderItems = array();
    foreach($order->getAllItems() as $item) 
    {

    $row=array();
    $row['sku'] = $item->getProduct()->getSku();
    $row['original_price'] = $item->getOriginalPrice();
    $row['price'] = $item->getPrice();
    $row['qty_ordered']= (int)$item->getQtyOrdered();
    $row['subtotal']= $item->getSubtotal();
    $row['tax_amount']= $item->getTaxAmount();
    $row['tax_percent']= $item->getTaxPercent();
    $row['discount_amount']= $item->getDiscountAmount();
    $row['row_total']= $item->getRowTotal();
    $row['name']= $item->getName();




    $orderItems[]=$row;

        if ($row['price'] != 0)
        {   echo    "order_id: '".$orderId."',";
            echo    "product_id: '".$row['sku']."',";
            echo    "name: '".$row['name']."',";
            echo    "price: '".$row['original_price']."',";
            echo    "quantity: '".$row['qty_ordered']."'";
        }

    }

?>

I am trying to do that in my custom block on Magento 2. Without success...

app/code/Clud7/Skroutz/Block/Success.php

<?php
namespace Clud7\Skroutz\Block;

use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\ProductRepository;
use Magento\Checkout\Model\Session;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Http\Context;
use Magento\Sales\Model\Order\Config;
use Magento\Sales\Model\OrderFactory;
class Success  extends \Magento\Framework\View\Element\Template
{   
    public function getOrder($id) {
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $orderid=$objectManager->get('Magento\Sales\Model\Order')->getCollection()->addFieldToFilter('increment_id',array('eq',$id))->getFirstItem()->getId();
       return $this->orderRepository->get($orderid);
    }


    public function getSomething()
    {
        return 'returned something from custom block.';
    }


    public function skroutz()
    {   
        $start= '<br /><br />SKROUTZ FEEDBACK STARTS HERE<br /><br />';
        $end='<br /><br />SKROUTZ FEEDBACK ENDS HERE<br /><br />';
        return $start .'returned something from custom block'.$end;
    }


}

app/code/Clud7/Skroutz/view/frontend/templates/order/success.phtml

<?php
 $order = $block->getOrder($block->getOrderId()); 
 $orderid=$block->getOrderId();
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
 $order = $objectManager->get('Magento\Sales\Model\Order')->load($orderid);
 $ordercollection=$order->getItemsCollection()->getData();
?>



<?php 
 echo 'START TEST GET ORDER ID <br />';
 echo  "Order ID:".$orderid."<br/>";
 echo 'END TEST GET ORDER ID <br />';
?>

The result is: enter image description here

  • you have the function getOrder() in your block and in phtml you are calling getOrderId()? – ASQ Jul 9 at 9:29
0

The correct way to get order information is by public function __construnction

Here is the correct code for app/code/Clud7/Skroutz/Block/Success.php

<?php
namespace Clud7\Skroutz\Block;

class Success  extends \Magento\Framework\View\Element\Template
{   


    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;
    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $_orderFactory;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;
    /**
     * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
     */
    protected $_catalogProductTypeConfigurable;
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;
    /**
     * @var \Magento\Sales\Model\Order
     */
    protected $_order;
    /**
     * constructor class
     *
     * @param \Magento\Checkout\Model\Session                                               $checkoutSession
     * @param \Magento\Sales\Model\OrderFactory                                             $orderFactory
     * @param \Magento\Catalog\Model\Product                                                $product
     * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable    $catalogProductTypeConfigurable
     * @param \Magento\Framework\View\Element\Template\Context                              $context
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Catalog\Model\Product $product,
        \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,
        \Magento\Framework\View\Element\Template\Context $context)
    {
        $this->_checkoutSession = $checkoutSession;
        $this->_orderFactory = $orderFactory;
        $this->_product = $product;
        $this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
        $this->_scopeConfig = $context->getScopeConfig();

        if ($this->_checkoutSession->getLastRealOrderId()) {
            $this->_order = $this->_orderFactory->create()->loadByIncrementId($this->_checkoutSession->getLastRealOrderId());
        }
        parent::__construct($context);
    }
    /**
     * Returns the id of the last order
     *
     * @return integer|boolean
     */
    public function getRealOrderId()
    {
        $order = $this->_order;
        if ($order) {
            $lastorderId = $order->getId();
            return $lastorderId;
        }
        return false;
    }
    /**
     * Returns the order subtotal with added tax and shipping fee
     *
     * @return string|boolean
     */
    public function getPrice()
    {
        $order = $this->_order;
        if ($order) {
            $price = number_format($order->getSubtotalInclTax() + $order->getShippingInclTax(), 2);
            return $price;
        }
        return false;
    }
    /**
     * Returns the order shipping fee
     *
     * @return string|boolean
     */
    public function getShippingCost()
    {
        $order = $this->_order;
        if ($order) {
            $shippingCost = number_format($order->getShippingInclTax(), 2);
            return $shippingCost;
        }
        return false;
    }
    /**
     * Returns the order tax amount
     *
     * @return string|boolean
     */
    public function getTaxAmount()
    {
        $order = $this->_order;
        if ($order) {
            $revenuefortax = $order->getSubtotalInclTax() + $order->getShippingInclTax();
            $taxtotal = $revenuefortax / 1.24;
            $taxAmountAlmost = $revenuefortax - $taxtotal;
            $taxAmount = number_format($taxAmountAlmost, 2);
            return $taxAmount;
        }
        return false;
    }
    /**
     * Returns all order items
     *
     * @return array|boolean
     */


}

?>

and app/code/Clud7/Skroutz/view/frontend/templates/order/success.phtml

<?php

$block->getRealOrderId();
//$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
//$order = $objectManager->get('\Magento\Sales\Model\Order')->load($orderid);
//$ordercollection=$order->getItemsCollection()->getData();

echo 'START TEST GET ORDER ID <br />';

echo  "Order ID:".$block->getRealOrderId()."<br/>";
echo  "Price Incl Tax: ".$block->getPrice()."<br />";
echo  "Tax:" .$block->getTaxAmount()."<br />";
echo 'END TEST GET ORDER ID <br />';


?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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