简体   繁体   English

Magento 2 通过 REST API 更新发票状态给出错误

[英]Magento 2 update invoice state through REST API giving error

I am having project in magento enterprise version :2.3.7-p3我在magento企业版中有项目:2.3.7-p3

I'm trying to update Magento 2 invoice state via Rest API call : {{magento_api_url}}/V1/invoices/ and method is post below are payload for api我正在尝试通过 Rest API 调用更新 Magento 2 发票状态:{{magento_api_url}}/V1/invoices/ 下面发布的方法是 api 的有效负载

{ "entity": { "entity_id": 8147, "state": 2 } } {“实体”:{“实体ID”:8147,“状态”:2}}

but I am getting this error : Fatal error : Uncaught Error: Call to a member function getId() on null in /var/www/html/vendor/magento/module-sales/Model/ResourceModel/Order/Invoice.php:58但我收到此错误:致命错误:未捕获的错误:在 /var/www/html/vendor/magento/module-sales/Model/ResourceModel/Order/Invoice.php:58 中调用 null 上的成员函数 getId()

Could some one help me?有人可以帮我吗?

As by default megento issue, So we can not make changes in core file, So for this I have make one alternative solution...由于默认情况下 megento 问题,所以我们不能在核心文件中进行更改,所以为此我做了一个替代解决方案......

I have created one custom invoice interface in my custom module with same API end point : {{magento_api_url}}/V1/invoices/ in webapi.xml file and defined the our custom model with preference in di.xml file and update the invoice state successfully.我在我的自定义模块中创建了一个自定义发票接口,具有相同的 API 端点:{{magento_api_url}}/V1/invoices/ 在 webapi.xml 文件中,并在 di.xml 文件中定义了我们的自定义模型并更新了发票状态成功地。

Below are the code snippet下面是代码片段

Custom Interface自定义界面

interface InvoiceCustomInterface

{ /** * save api * @param \Magento\Sales\Api\Data\InvoiceInterface $entity Invoice interface * @return \Magento\Sales\Api\Data\InvoiceInterface Invoice interface */ { /** * 保存 api * @param \Magento\Sales\Api\Data\InvoiceInterface $entity 发票接口 * @return \Magento\Sales\Api\Data\InvoiceInterface 发票接口 */

public function save($entity);

} }

Webapi.xml Webapi.xml

<route url="/V1/invoices/" method="POST">
    <service class="Vendor\Module_Nmae\Api\InvoiceCustomInterface" method="save"/>
    <resources>
        <resource ref="Vendor_Module_Nmae::Module_Nmae_invoice" />
    </resources>
</route>

di.xml di.xml

<preference for="Vendor\Module_Nmae\Api\InvoiceCustomInterface" type="Vendor\Module_Nmae\Model\Api\Invoice"/>

Model file模型文件

class Invoice implements InvoiceCustomInterface

{ protected $logger; { 受保护的 $logger;

/**
 * @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;
}

} }

So this solution will resolved the issue.所以这个解决方案将解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM