Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2以编程方式创建发票

在Magento 2中,除了以编程方式创建产品和客户外,您还可以按照Magento 2以编程方式创建发票主题中的给定指南,轻松地以编程方式创建发票

为什么Magento 2商店需要设置用于创建发票的程序?如您所知,每次创建发票时,都意味着成功下达了订单,同时Magento会通知您已从客户那里赚了钱。但是,通过这种常规方式,如果您的客户在订单中实施了部分付款,则您将从Magento 2系统获得批量通知,并创建同等数量的发票。因此,此主题是形成发票的绝佳解决方案,而不必依赖订单的总付款额。

在Magento 2中以编程方式创建发票的概述

步骤1:声明事件sales_order_invoice_pay

在自定义模块中使用events.xml文件启动设置: /app/code/Mageplaza/HelloWorld/etc/events.xml

<?xml version="1.0"?>
    <event name="sales_order_invoice_pay">
        <observer name="webpos_sales_order_invoice_pay" instance="Mageplaza\HelloWorld\Observer\SalesOrderInvoicePay" />
    </event>  
</config>

步骤2:设置观察者类

/app/code/Mageplaza/HelloWorld/Observer/SalesOrderInvoicePay.php

<?php
namespace Mageplaza\HelloWorld\Observer\Sales;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class SalesOrderInvoicePay implements ObserverInterface
{   
/**
* @param EventObserver $observer
* @return $this
*/
public function execute(EventObserver $observer)
{
     $invoice = $observer->getEvent()->getInvoice();
     $order = $invoice->getOrder();
   
     /* reset total_paid & base_total_paid of order */
     $order->setTotalPaid($order->getTotalPaid() - $invoice->getGrandTotal());
     $order->setBaseTotalPaid($order->getBaseTotalPaid() - $invoice->getBaseGrandTotal());
}    
}

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论