在 WooCommerce 3 中生成的发票中显示自定义总节省 [英] Display Custom Total Saving in generated invoice in WooCommerce 3

查看:46
本文介绍了在 WooCommerce 3 中生成的发票中显示自定义总节省的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我使用 WooCommerce 打印发票 &装箱单插件...

In WooCommerce I am using WooCommerce Print Invoices & Packing lists plugin…

如何在此插件生成的发票中显示任何订单的总节省额?

How can I display the total savings of any order in the invoices generated by this plugin ?

1 年前,我一直在使用基于 [this answer] 的代码,并且在我更新 WooCommerce 之前就可以使用:

1 year ago I have been using this code based on [this answer] and that was working before I updated WooCommerce :

<?php 

    $discount_total = 0;
    $_order = wc_get_order( $order_id );
    foreach ($_order->get_items() as $order_item) {
        if ($order_item['variation_id'] > 0) {
            $line_item = wc_get_product( $order_item['variation_id'] );
        } else {
            $line_item = wc_get_product( $order_item['product_id'] );
        }
        $sale_price = $line_item->sale_price;
        $regular_price = $line_item->regular_price;

        if ( !empty($sale_price) ) {
            $discount = ($regular_price - $sale_price) * $order_item['item_meta']['_qty'][0];
            $discount_total += $discount;
        }
    }
    $discount_saving = round ( $discount_total + $_order->get_total_discount() );
    if ( $discount_total > 0 ) {

    <tr>
        <td class="order_saving" colspan="5"><strong><?php _e( 'Total Savings:  ', 'woocommerce' ); ?></strong></td>
        <td class="value" colspan="2" ><span class="woocommerce-Price-saving saving"><?php echo $discount_saving ; ?></span></td>
    </tr>
<?php } ?>

所以现在它不再起作用了.我尝试进行更改但没有成功.

So now it doesn't works anymore. I have tried to make changes without success.

那么正确的解决方案是什么?

So what is the right solution?

推荐答案

以下是适用于 WooCommerce 版本 3+ 的更新代码:

Here is the updated code that will work for WooCommerce version 3+:

<?php 
///////////// HERE BEGINS CUSTOMIZATION /////////////

$discount_total = 0;
$_order = wc_get_order( $order_id );
foreach ($_order->get_items() as $line_item) {
    // The WC_product object
    $_product = $line_item->get_product(); 

    // The product prices
    $sale_price = $_product->get_sale_price();
    $regular_price = $_product->get_regular_price();

    if ( !empty($sale_price) ) {
        $discount = ($regular_price - $sale_price) * $line_item->get_quantity();
        $discount_total += $discount;
    }
}

$discount_saving = round ( $discount_total + $_order->get_total_discount() );

if ( $discount_total > 0 ) {
    ?>

    <tr>
    <td class="order_saving" colspan="5"><strong><?php _e( 'Total Savings: ', 'woocommerce' ); ?></strong></td>
    <td class="value" colspan="2" ><span class="woocommerce-Price-saving saving"><?php echo $discount_saving ; ?></span></td>
    </tr>

    <?php
}
///////////// END OF CUSTOMIZATION ///////////// 
?>

这现在应该适合你

相关主题:

这篇关于在 WooCommerce 3 中生成的发票中显示自定义总节省的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆