Frage

I want to get the grand total on success.phtml page so I can use it with various tracking codes for third-party vendors.

I follow this guide but I couldn't get it to work:

Vendor/Module/Block/Checkout/Success.php

namespace Vendor\Module\Block\Checkout;

class Success extends \Magento\Checkout\Block\Onepage\Success
{
    /**
     * @return int
     */
    public function getGrandTotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->getLastOrderId());
        return $order->getGrandTotal();
    }
}

Vendor/themename/Magento_Checkout/templates/success.phtml

$grandTotal = $block->getGrandTotal();
$grandTotal = number_format($grandTotal, 2, '.', '');

I get this error in magento_dir/var/log/system.log:

[2017-07-02 20:37:22] main.CRITICAL: Invalid template file: 'button.phtml' in module: 'Vendor_Module' block's name: 'checkout.success.print.button' [] []
[2017-07-02 20:37:22] main.CRITICAL: Invalid template file: 'success.phtml' in module: 'Vendor_Module' block's name: 'checkout.success' [] []

Is there a way to actually see the PHP error? /var/log/nginx/error.log doesn't show anything and neither does magento_dir/var/report/

War es hilfreich?

Lösung

Please try the below code.

step1 - app/code/Training/Grandtotalsuccesspage/registration.php.

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

step2 - app/code/Training/Grandtotalsuccesspage/etc/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="Training_Grandtotalsuccesspage" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

step3 - app/code/Training/Grandtotalsuccesspage/etc/di.xml.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Training\Grandtotalsuccesspage\Block\Onepage\Success"/>
</config>

step4 - app/code/Training/Grandtotalsuccesspage/Block/Onepage/Success.php.

<?php

namespace Training\Grandtotalsuccesspage\Block\Onepage;

/**
 * One page checkout success page
 */
class Success extends \Magento\Checkout\Block\Onepage\Success
{

    protected $orderItemsDetails;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Sales\Model\Order $orderItemsDetails
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Sales\Model\Order $orderItemsDetails,
        array $data = []
    ) {
        parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
        $this->orderItemsDetails = $orderItemsDetails;
    }

    public function getOrderItemsDetails()
    {
        $IncrementId  = $this->_checkoutSession->getLastRealOrder()->getIncrementId();
        $order_information = $this->orderItemsDetails->loadByIncrementId($IncrementId);
       return $order_information;
    }
}

step5 - app/code/Training/Grandtotalsuccesspage/view/frontend/layout/checkout_onepage_success.xml.

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Success Page</title>
    </head>
    <body>
        <referenceBlock name="page.main.title">
            <block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success.print.button" template="button.phtml"/>
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Thank you for your purchase!</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Training\Grandtotalsuccesspage\Block\Onepage\Success" name="checkout.success" template="success.phtml" cacheable="false"/>
        </referenceContainer>
        <container name="order.success.additional.info" label="Order Success Additional Info"/>
        <referenceBlock name="checkout.registration" remove="true" />
    </body>
</page>

step6 - app/code/Training/Grandtotalsuccesspage/view/frontend/templates/success.phtml.

<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<?php $_order = $block->getOrderItemsDetails(); ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?php echo __('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><?php echo __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif;?>
            <p><?php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif;?>

    <?php echo $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?php /* @escapeNotVerified */ echo $block->getUrl() ?>"><span><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>
<?php
$shipping_addresses = $_order->getShippingAddress()->getData();
$billing_addresses = $_order->getBillingAddress()->getData();
?>
<table>
    <tr><td>Shipping Address</td><td><?php print_r($shipping_addresses); ?></td></tr>
    <tr><td>Billing Address</td><td><?php print_r($billing_addresses); ?></td></tr>
    <tr><td>Sub Total</td><td><?php echo $_order->formatPrice($_order->getSubTotal()); ?></td>
    <tr><td>Shipping & Handling</td><td><?php echo $_order->formatPrice($_order->getShippingAmount()); ?></td>
    <tr><td>Grand Total</td><td><?php echo $_order->formatPrice($_order->getGrandTotal()); ?></td></tr>
</table>

Run setup upgrade.

Run flush cache.

Give folder permission to var folder.

The output is look likes below screen shot.

enter image description here

You can download full package from github https://github.com/jothibasuj/M2--Order-success-page-customization

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top