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



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

<?php
namespace MagentoPGMBlock;
use MagentoAdminNotificationModelInbox;
use MagentoCheckoutModelSession;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkAppResponseHttp;
use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext;
use MagentoSalesApiDataOrderAddressInterface;
use MagentoSalesModelOrderPaymentTransaction;
use MagentoSalesModelOrderPaymentTransactionBuilder as TransactionBuilder;
use MagentoSalesModelOrderFactory;
use MagentoStoreModelScopeInterface;
use MagentoPGMLoggerLogger;

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('MagentoFrameworkUrlInterface');
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)
{
...
}
}

我得到这个错误:

对空上的成员函数setTransactionId((的调用

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

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

正如我所知,Factory类名是Model类的名称,并附加Factory单词。因此,对于我们的示例,我们将有TopicFactory类。您不能创建此类。Magento会为您创建它。每当Magento的对象管理器遇到以单词"Factory"结尾的类名时,如果该类还不存在,它将自动在var/generation文件夹中生成Factory类。您将在中看到工厂类

ROOT/generated/code/<vendor_name>/<module_name>/Model/OrderFactory.php

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

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

Hi-orderFactory在DB中没有付款,因此您无法使用它来获得付款。你可以试试这个:

use MagentoSalesModelResourceModelOrderPaymentTransactionCollectionFactory; 
protected $transactions;
public function __constructor(CollectionFactory $transactions)
{
$this->transactions = $transactions;
}

在您的方法中:

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

相关内容

  • 没有找到相关文章

最新更新