Php 在第3页的生成发票中显示自定义总计保存

Php 在第3页的生成发票中显示自定义总计保存,php,wordpress,woocommerce,invoice,orders,Php,Wordpress,Woocommerce,Invoice,Orders,在WooCommerce中,我使用WooCommerce打印发票和装箱单插件 如何显示此插件生成的发票中任何订单的总节省量 1年前,我一直在使用基于[此答案的代码,在我更新WooCommerce之前,该代码是有效的: <?php $discount_total = 0; $_order = wc_get_order( $order_id ); foreach ($_order->get_items() as $order_item) { i

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

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

1年前,我一直在使用基于[此答案的代码,在我更新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 } ?>

所以现在它不再起作用了。我试图做出改变,但没有成功


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

以下是适用于WooCommerce版本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 ///////////// 
?>


这应该对你有用了


相关线程:


@LoicTheAztec,它给了我上述代码。