我在magento企业版:2.3.7-p3
我正在尝试通过Rest API调用更新Magento 2发票状态:{{magento_api_url}}/V1/发票/和方法是下面是API的有效载荷
{"entity" {"entity_id" 8147,"state" 2}}
,但我得到这个错误:致命错误: Uncaught错误:在null上调用成员函数getId()/var/www/html/vendor/magento/module-sales/模型/ResourceModel/订单/Invoice.php: 58
有人能帮我吗?
作为默认的megento问题,所以我们不能在核心文件中进行更改,所以为此我做了一个替代解决方案…
我在我的自定义模块中创建了一个自定义发票接口,具有相同的API端点:{{magento_api_url}}/V1/invoices/在webapi.xml文件中,并在di.xml文件中定义了我们的自定义模型,并成功更新了发票状态。
下面是代码片段自定义界面
interface InvoiceCustomInterface
{/* **保存API* @param MagentoSalesApiDataInvoiceInterface $entity发票接口* @return MagentoSalesApiDataInvoiceInterface发票接口*/
public function save($entity);
}
Webapi.xml
<route url="/V1/invoices/" method="POST">
<service class="VendorModule_NmaeApiInvoiceCustomInterface" method="save"/>
<resources>
<resource ref="Vendor_Module_Nmae::Module_Nmae_invoice" />
</resources>
</route>
di.xml
<preference for="VendorModule_NmaeApiInvoiceCustomInterface" type="VendorModule_NmaeModelApiInvoice"/>
模型文件
class Invoice implements InvoiceCustomInterface
{保护日志记录器美元;
/**
* @var InvoiceRepositoryInterface
*/
private $invoiceRepository;
public function __construct(
LoggerInterface $logger,
InvoiceRepositoryInterface $invoiceRepository
)
{
$this->invoiceRepository = $invoiceRepository;
$this->logger = $logger;
}
/**
* @inheritdoc
* @param $entity
*/
public function save($entity)
{
try {
$invoiceRepo = $this->invoiceRepository->get($entity->getEntityId());
$invoiceRepo->setState($entity->getState());
$this->invoiceRepository->save($invoiceRepo);
} catch (Exception $e) {
$this->logger->info($e->getMessage());
}
return $invoiceRepo;
}
}
所以这个解决方案将解决这个问题。