【问题标题】:Magento 2 orderFactory not found in vendor directory在供应商目录中找不到 Magento 2 orderFactory
【发布时间】:2020-03-16 11:46:10
【问题描述】:

我在 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)
    {
       ...
    }
}

我收到此错误:

在 null 上调用成员函数 setTransactionId()

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

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

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

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

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

    所以第一步你应该去Generation文件夹看看这个类是否存在。

    如果它不存在,我认为您正面临权限问题,magento 无法在 Generation 文件夹中生成(无法创建文件或文件夹)Factory Class。

    【讨论】:

    • 这似乎是问题所在,我没有检查 var 文件夹,但它几乎是空的(只有 page_cache 在那里)。我尝试根据 magento 2 文档设置权限,但无济于事。我将尝试重新安装我的 magento 商店并更新此问题。感谢您的时间和精力!
    • 对不起,我给错了路径,工厂类在 ROOT/generated/code///ClassFactory.php 因为你在magento 2.3.4
    • 文件夹generated/code/Magento 只包含Framework 文件夹,所以问题似乎出现在所有生成的代码中?
    • 是的。因为每个工厂类都会生成并位于 Generated/code 下。
    【解决方案2】:

    您好 orderFactory 在 DB 中没有付款,因此您不能使用它来获得付款。你可以试试这个:

    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");`
    

    【讨论】:

    • 感谢您的回答,重新安装magento 2后,我可以尝试一下。
    猜你喜欢
    • 2018-10-03
    • 2018-12-09
    • 2014-10-29
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2016-10-28
    • 2017-01-11
    • 2016-04-27
    相关资源
    最近更新 更多