我需要为客人结帐启用打印订单功能。
对于登录用户,打印功能运行良好
但对于访客用户,打印按钮在成功页面上不可见 为了显示打印按钮,我删除了签入文件 success.phtml
<?php if ($this->getCanViewOrder() && $this->getCanPrintOrder()) :?>
<p>
<?php echo $this->__('Click <a href="%s" onclick="this.target=\'_blank\'">here to print</a> a copy of your order confirmation.', $this->getPrintUrl()) ?>
<?php echo $this->getChildHtml() ?>
</p>
<?php endif;?>
现在打印按钮对客人也可见,但是当我点击 它要求登录。
我需要在没有登录的情况下向客人出示打印订单。
打印订单位置就像
localhost/magento1.9/index.php/sales/order/print/order_id/8/
我已经检查过
/var/www/magento1.9/app/code/core/Mage/Sales/Controller/Abstract.php
并找到打印操作,如下
public function printAction()
{
if (!$this->_loadValidOrder()) {
return;
}
$this->loadLayout('print');
$this->renderLayout();
}
但我不明白如何在不登录的情况下为客人提供打印功能。 请建议我提前谢谢
您应该尝试这个扩展:https://github.com/eltrino/PrintOrder它工作得非常好,是一种在客人结帐后提供订单确认打印链接的保存方式。
在 Magento 2 中: 您必须覆盖模块 Sales,然后覆盖以下 2 个控制器,如下所示:
应用程序/代码/您的供应商名称/销售/etc/di.xml
<preference for="Magento\Sales\Controller\Order\PrintAction" type="YourVendorNAme\Sales\Controller\Order\PrintAction"/>
<preference for="Magento\Sales\Controller\AbstractController\OrderLoader" type="YourVendorNAme\Sales\Controller\AbstractController\OrderLoader"/>
应用程序/代码/YourVendorName/Sales/Controller/AbstractController/OrderLoader.php
<?php
namespace YourVendorNAme\Sales\Controller\AbstractController;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Framework\Controller\Result\RedirectFactory;
class OrderLoader implements \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
{
/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Sales\Controller\AbstractController\OrderViewAuthorizationInterface
*/
protected $orderAuthorization;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $url;
/**
* @var ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var RedirectFactory
*/
protected $redirectFactory;
private $checkoutSession;
/**
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param OrderViewAuthorizationInterface $orderAuthorization
* @param Registry $registry
* @param \Magento\Framework\UrlInterface $url
* @param ForwardFactory $resultForwardFactory
* @param RedirectFactory $redirectFactory
*/
public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Sales\Controller\AbstractController\OrderViewAuthorizationInterface $orderAuthorization,
Registry $registry,
\Magento\Framework\UrlInterface $url,
ForwardFactory $resultForwardFactory,
RedirectFactory $redirectFactory,
\Magento\Checkout\Model\Session $checkoutSession
) {
$this->orderFactory = $orderFactory;
$this->orderAuthorization = $orderAuthorization;
$this->registry = $registry;
$this->url = $url;
$this->resultForwardFactory = $resultForwardFactory;
$this->redirectFactory = $redirectFactory;
$this->checkoutSession = $checkoutSession;
}
/**
* @param RequestInterface $request
* @return bool|Forward|Redirect
*/
public function load(RequestInterface $request)
{
$orderId = (int)$request->getParam('order_id');
if (!$orderId) {
/** @var Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('noroute');
}
$order = $this->orderFactory->create()->load($orderId);
$lastOrderId = $this->checkoutSession->getData('last_order_id');
// Print order if customer is logged in or it's the last order of guest customer
if ($this->orderAuthorization->canView($order) || $lastOrderId == $orderId) {
$this->registry->register('current_order', $order);
return true;
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->redirectFactory->create();
return $resultRedirect->setUrl($this->url->getUrl('*/*/history'));
}
}
应用程序/代码/YourVendorName/Sales/Controller/Order/PrintAction.php
<?php
namespace YourVendorNAme\Sales\Controller\Order;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\Page;
use Magento\Sales\Controller\OrderInterface;
class PrintAction extends \Magento\Sales\Controller\AbstractController\PrintAction implements OrderInterface
{
/**
* @return ResponseInterface|ResultInterface|Page
*/
public function execute()
{
$result = $this->orderLoader->load($this->_request);
if ($result instanceof ResultInterface) {
return $result;
}
/** @var Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->addHandle('print');
return $resultPage;
}
}
打印收据是 magento2 的默认行为,仅提供给登录用户。为了避免误用和打印其他我们不知道是否属于访客用户的客户订单,如果他没有登录,则无法确定他的身份