我正在使用 Magento 模块。用户点击“下订单”,对外部 API 服务进行 API 调用,获取唯一 ID,Magento 订单 ID 和 API 唯一 ID 保存到自定义表中,它使用唯一 ID 构造一个 url 并将用户重定向到那里。付款完成后,他们会被重定向回特定控制器上的 Magento。
这就是我遇到问题的地方。我现在需要将它们发送到
checkout/onepage/success
谢谢页面,但我不明白是什么阻止了我这样做。
我设置了一个非常基本的重定向,它成功地将我重定向到购物车页面,但是当我尝试重定向到感谢页面时,它会将我带回购物车页面。这是我的代码:
<?php
namespace Namespace\Payment\Controller\Redirect;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Namespace\Payment\Helper\Data as Helper;
use Namespace\Payment\Model\MyOrder;
class Callback implements ActionInterface {
protected $request;
protected $response;
protected $helper;
protected $redirectFactory;
protected MyOrder $myOrder;
public function __construct(
RequestInterface $request,
ResponseInterface $response,
Helper $helper,
MyOrder $myOrder,
RedirectFactory $redirectFactory
) {
$this->request = $request;
$this->response = $response;
$this->helper = $helper;
$this->myOrder= $myOrder;
$this->redirectFactory = $redirectFactory;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$orderId = '000000003';
// This part does NOT work
return $this->response->setRedirect(
$this->helper->getSuccessUrl($orderId)
)->sendResponse();
// This part DOES work
return $this->response->setRedirect(
$this->helper->getCartUrl()
)->sendResponse();
}
}
辅助类返回成功 url
https://whatever-ngrok-domain.app/checkout/onepage/success/?order_id=000000003
助手类中的购物车 URL 返回
https://whatever-ngrok-domain.app/checkout/cart/
那么为什么我无法访问 Magento 感谢页面?在哪里可以找到 ANY 类型的文档来更好地理解这一点?我在网上没有找到任何东西。
编辑:在深入研究 magento 代码后,我发现了这个:
public function isValid()
{
if (!$this->checkoutSession->getLastSuccessQuoteId()) {
return false;
}
if (!$this->checkoutSession->getLastQuoteId() || !$this->checkoutSession->getLastOrderId()) {
return false;
}
return true;
}
第一个
if
是失败的,因为显然我没有做任何事情来确保会话填充任何数据,但问题仍然存在,因为在线或“文档”中的任何地方实际上都没有信息" 对于 magento,了解某人需要做什么才能进入感谢页面。当我只有 checkoutSession
本身时,如何确保 $order
数据全部正确填写?
解决了。
显然你需要注入
Magento\Checkout\Model\Session
类,然后在其上调用这些:
$this->checkoutSession->setLastSuccessQuoteId($magentoOrder->getQuoteId());
$this->checkoutSession->setLastQuoteId($magentoOrder->getQuoteId());
$this->checkoutSession->setLastOrderId($magentoOrder->getEntityId());
然后你就可以去结账了。
我没有在文档中找到任何指向这一点的资源,而是我需要搜索互联网的神秘角落来收集这些知识。