Magento 2 : How to show products and shipping method on order success page? As picture below:
-
09-03-2021 - |
質問
htdocs/vendor/magento/module-checkout/view/frontend/templates/success.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
<?php if ($block->getOrderId()):?>
<?php if ($block->getCanViewOrder()) :?>
<p style=" font-size: 19px; font-weight: 700;color: #4F91D0;" ><?= __('Your order number is: %1.', sprintf('<a href="%s" class="order-number" ><strong style="font-weight: 600; color: white; background-color: #4F91D0; font-size: 30px;" >%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
<?php else :?>
<p><?= __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
<?php endif;?>
<!-- <p><?= /* @escapeNotVerified */ __('We\'ll email you an order confirmation with details and tracking info.') ?></p>-->
<p>
Enviaremos para você um e-mail de confirmação do seu pedido com detalhes e informações de rastreamento.
Adicione o e-mail <a href="mailto:suporte@suporte.com.br"> suporte@suporte.com.br</a> a no seu livro de endereços e verifique sua pasta de spam caso não receba o e-mail de confirmação do pedido.<br>
Agora é só aguardar a confirmação do seu pagamento. Você receberá um e-mail assim que o seu pagamento for confirmado.<br>
Em caso de dúvidas, entre em contato com o e-mail informado acima ou ligue-nos no <a href="tel:40201702">0000-0000</a> (custo de ligação local).
</p>
<br>
<h2>Status do Pedido</h2>
<!-- <p class="status"> </p> -->
<div id="status_bar">
<img class="status" src="https://d2fqgw7smz4ag9.cloudfront.net/pub/media/wysiwyg/status-1.gif" />
</div>
<h2>Suas Compras</h2>
<?= $block->getProductItem() ?> //This is where I want you to display the purchase you made
<h2>Dados do Envio</h2>
<?= $block->getMethodShipping() ?> //This is where I want you to display the delivery method selected by the user.
<?php endif;?>
<?= $block->getAdditionalInfoHtml() ?>
<div class="actions-toolbar">
<div class="primary">
<a class="action primary continue" href="<?= /* @escapeNotVerified */ $block->getContinueUrl() ?>"><span><?= /* @escapeNotVerified */ __('Continue Shopping') ?></span></a>
</div>
</div>
</div>
htdocs/vendor/magento/module-checkout/Block/Onepage
/* Methods for displaying purchased products and selected shipping method */
public function getProductItem()
{
return 'View my purchase'; //What would the method of displaying a purchase look like?
}
public function getMethodShipping()
{
return 'Selected Shipping Type'; //What would be the method for displaying the selected shipping type?
}
解決
Create module for that don't make changes in core files.
You can show all order details by following steps.
Remove from xml which you dont want to display.
create app\code\Ketan\Module\view\frontend\layout\checkout_onepage_success.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="sales_order_item_renderers"/>
<update handle="sales_order_item_price"/>
<body>
<referenceBlock name="checkout.success" remove="true"/>
<referenceContainer name="content">
<block class="Magento\Checkout\Block\Onepage\Success" name="ketan.checkout.success" template="Ketan_Module::onepage/success.phtml" cacheable="false">
<block class="Ketan\Module\Block\Onepage\Success\Details" name="ketan.checkout.success.details" as="details" template="Ketan_Module::onepage/details.phtml" cacheable="false">
<block class="Magento\Sales\Block\Order\Info" as="info" name="sales.order.info" />
<block class="Magento\Sales\Block\Order\View" name="sales.order.view" cacheable="false" as="order_view" template="Ketan_Module::onepage/details/order/view.phtml">
<block class="Magento\Sales\Block\Order\Items" name="order_items" template="order/items.phtml">
<block class="Magento\Framework\View\Element\RendererList" name="sales.order.items.renderers" as="renderer.list"/>
<block class="Magento\Sales\Block\Order\Totals" name="order_totals" template="order/totals.phtml">
<arguments>
<argument name="label_properties" xsi:type="string">colspan="4" class="mark"</argument>
<argument name="value_properties" xsi:type="string">class="amount"</argument>
</arguments>
<block class="Magento\Tax\Block\Sales\Order\Tax" name="tax" template="order/tax.phtml"/>
</block>
</block>
</block>
</block>
<container name="order.success.additional.info" label="Order Success Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
create Block file for template app\code\Ketan\Module\Block\Onepage\Success\Details.php
<?php
namespace Ketan\Module\Block\Onepage\Success;
use Magento\Framework\View\Element\Template;
class Details extends Template
{
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;
public function __construct(
Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Checkout\Model\Session $session,
array $data = []
) {
parent::__construct($context, $data);
$this->registry = $registry;
$this->session = $session;
}
protected function _prepareLayout()
{
if (!$this->registry->registry('current_order')) {
$this->registry->register('current_order', $this->getOrder());
}
return parent::_prepareLayout();
}
/**
* Retrieve current order model instance
*
* @return \Magento\Sales\Model\Order
*/
public function getOrder()
{
return $this->session->getLastRealOrder();
}
}
Create view files which are override from xml files
- app\code\Ketan\Module\view\frontend\templates\onepage\details.phtml
- app\code\Ketan\Module\view\frontend\templates\onepage\success.phtml
- app\code\Ketan\Module\view\frontend\templates\onepage\details\order\view.phtml
他のヒント
Please try with below code :
like : pass $block->getOrderId()
in $block->getMethodShipping($block->getOrderId())
public function getMethodShipping($orderid)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //if it's working than please use via construct
$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderid);
return $order->getShippingMethod();
}
same process do with getProductItem()
I hope its working
Please try with below code :
1) Override order success block
app/code/CompanyName/ModuleName/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="CompanyName\ModuleName\Block\Onepage\Success"/>
</config>
2) order success block
<?php
namespace CompanyName\ModuleName\Block\Onepage;
class Success extends \Magento\Checkout\Block\Onepage\Success
{
protected $_orderDetail;
protected $_order;
protected $_item;
protected $_renderer;
protected $_priceHelper;
protected $_customerSession;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param \Magento\Framework\App\Http\Context $httpContext
* @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 $orderDetails,
\Magento\Catalog\Model\ProductRepository $item,
\Magento\Sales\Model\Order\Address\Renderer $renderer,
\Magento\Framework\Pricing\Helper\Data $priceHelper,
\Magento\Catalog\Block\Product\ListProduct $listProduct,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\Session $customerSession,
array $data = []
) {
$this->_orderDetail = $orderDetails;
$this->_renderer = $renderer;
$this->_item = $item;
$this->_priceHelper = $priceHelper;
$this->_listProduct = $listProduct;
$this->_storeManager = $storeManager;
$this->_customerSession = $customerSession;
$this->_order = null;
parent::__construct(
$context, $checkoutSession, $orderConfig, $httpContext, $data
);
}
public function getOrder($id) {
$this->_order = $this->_orderDetail->loadByIncrementId($id);
return $this->_order;
}
public function getItem($itemId) {
return $this->_item->getById($itemId);
}
public function getFormatedAddress($address) {
return $this->_renderer->format($address, 'html');
}
public function getPaymentMethodtitle($order) {
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
return $method->getTitle();
}
/**
* Get Formated Price
* @param fload price
* @return boolean
*/
public function getFormatedPrice($price='')
{
return $this->_priceHelper->currency($price, true, false);
}
public function getMediaUrl()
{
$currentStore = $this->_storeManager->getStore();
return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
}
Override order success.phtml and Add html from Override success.html file
CompanyName/ModuleName/view/frontend/templates/success.phtml
3) Get Shipping Method
<?php $order = $this->getOrder($this->getOrderId()); ?>
<?php if(!empty($order->getShippingDescription())):?>
<div class="shipping-method">
<strong class="box-title">
<span class="box-des"><?php /* @escapeNotVerified */ echo __('Shipping Method'); ?></span>
</strong>
<div class="box-content">
<?php echo $order->getShippingDescription(); ?>
</div>
</div>
<?php endif;?>
4) Get Product Details
<?php $order = $this->getOrder($this->getOrderId()); ?>
<div class="order-detail">
<table>
<thead>
<tr>
<th colspan="3"><?php /* @escapeNotVerified */ echo __('Product Name'); ?></th>
<th class="table-number"><?php /* @escapeNotVerified */ echo __('Price'); ?></th>
<th class="table-number"><?php /* @escapeNotVerified */ echo __('Qty'); ?></th>
<th class="table-number"><?php /* @escapeNotVerified */ echo __('Subtotal'); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($order->getAllVisibleItems() as $item) {
$product = $this->getItem($item->getProductId());
$options = $item->getProductOptions();
$imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$productImage = $imageBlock->getImage($product, 'category_page_list');
?>
<tr class="data-item">
<td colspan="3">
<div class="product-image">
<a href="<?php echo $product->getProductUrl(); ?>"><?php echo $productImage->toHtml(); ?></a>
</div>
<div class="product-name-sku">
<div class="product-name">
<span><?php echo $item->getName(); ?></span>
</div>
<div class="product-name">
<h5><?php /* @escapeNotVerified */ echo __('SKU'); ?></h5>
<span><?php echo $item->getSku(); ?></span>
</div>
</div>
</td>
<td class="table-number"><?php echo $this->getFormatedPrice($item->getPrice()); ?></td>
<td class="table-number"><?php echo $item->getQtyOrdered(); ?></td>
<td class="table-number"><?php echo $this->getFormatedPrice($item->getBaseRowTotal()); ?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr class="subtotal">
<th colspan="5" class="table-method" scope="row">
<?php /* @escapeNotVerified */ echo __('Subtotal'); ?>
</th>
<td class="number" data-th="<?php /* @escapeNotVerified */ echo __('Subtotal'); ?>">
<span class="price"><?php echo $this->getFormatedPrice($order->getSubtotal()); ?></span>
</td>
</tr>
<tr class="shipping">
<th colspan="5" class="table-method" scope="row">
<?php /* @escapeNotVerified */ echo __('Shipping & Handling'); ?>
</th>
<td class="number" data-th="<?php /* @escapeNotVerified */ echo __('Shipping & Handling'); ?>">
<span class="price"><?php echo $this->getFormatedPrice($order->getShippingAmount()); ?></span>
</td>
</tr>
<tr class="grand_total">
<th colspan="5" class="table-method" scope="row">
<strong><?php /* @escapeNotVerified */ echo __('Grand Total'); ?></strong>
</th>
<td class="number" data-th="<?php /* @escapeNotVerified */ echo __('Grand Total'); ?>">
<strong><span class="price"><?php echo $this->getFormatedPrice($order->getGrandTotal()); ?></span></strong>
</td>
</tr>
</tfoot>
</table>
</div>
It's working