在供应商目录中找不到 Magento 2 orderFactory

Magento 2 orderFactory not found in vendor directory

我在 docker 容器内使用 Magento 2.3.4 作为支付网关扩展。首先,这里是受影响的代码:

<?php

namespace Magento\PGM\Block;

use Magento\AdminNotification\Model\Inbox;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Response\Http;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Sales\Model\Order\Payment\Transaction\Builder as TransactionBuilder;
use Magento\Sales\Model\OrderFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\PGM\Logger\Logger;


class Main extends Template
{
    protected $_objectmanager;
    protected $checkoutSession;
    protected $urlBuilder;
    protected $response;
    protected $config;
    protected $messageManager;
    protected $transactionBuilder;
    protected $inbox;
    private $logger;
    private $orderFactory;

    public function __construct(Context $context, Session $checkoutSession, OrderFactory $orderFactory = null, Logger $logger, Http $response, TransactionBuilder $tb, Inbox $inbox)
    {
        $this->checkoutSession = $checkoutSession;
        $this->orderFactory = $orderFactory ?: ObjectManager::getInstance()->get(OrderFactory::class);
        $this->response = $response;
        $this->config = $context->getScopeConfig();
        $this->transactionBuilder = $tb;
        $this->logger = $logger;
        $this->inbox = $inbox;

        $this->urlBuilder = ObjectManager::getInstance()
            ->get('Magento\Framework\UrlInterface');
        parent::__construct($context);
    }

    public function getParentId()
    {
        return $this->getData(OrderAddressInterface::PARENT_ID);
    }

    protected function _prepareLayout()
    {
        $method_data = array();
        $order = $this->orderFactory->create()->load($this->getParentId());
        if ($order) {
            $payment = $order->getPayment();
            // The error is thrown here (" Call to a member function setTransactionId() on null")
            $payment->setTransactionId("-1");
            ...
            $payment->save();
            $order->save();

            ...
    }

    private function setApiData($order, $testmode, $instance)
    {
       ...
    }
}

我收到这个错误:

Call to a member function setTransactionId() on null

我认为这只是一个症状。未创建订单对象,我的 IDE 将 $order->getPayment() 方法标记为根本未找到。

代码本身应该不是问题,但文件夹 'Sales\Model' 不包含 orderFactory.php 文件。该文件是否丢失或已弃用?多个模块使用此文件并创建这样的订单,例如 Paypal PGM,并使用 OrderFactory.php 文件。

据我所知,工厂 class 名称是型号 class 的名称并附加工厂字样。因此,对于我们的示例,我们将拥有 TopicFactory class。您不得创建此 class。 Magento 将为您创建它。每当 Magento 的对象管理器遇到以单词“工厂”结尾的 class 名称时,它会自动在 var/generation 文件夹中生成工厂 class 如果 class 还没有存在。您将在

中看到工厂 class
ROOT/generated/code/<vendor_name>/<module_name>/Model/OrderFactory.php

因此,第一步您应该转到文件夹 Generation 以查看 class 是否存在。

如果不存在,我认为您遇到了权限问题,magento 无法在生成文件夹中生成(无法创建文件或文件夹)工厂 Class。

您好orderFactory在数据库中没有付款,所以你不能用它来获得付款。你可以试试这个:

use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory; 
protected $transactions;
public function __constructor(CollectionFactory $transactions)
{
    $this->transactions = $transactions;
}

在你的方法中:

$transactions = $this->transactions->create()->addOrderIdFilter($orderId);
...
$transactions->setTransactionId("-1");`