在 WooCommerce 3 中生成的发票中显示自定义总节省

2024-01-11

在 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 ///////////// 
?>

这现在应该适合你


相关主题:

  • 如何获取 WooCommerce 订单详细信息 https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details/
  • 在 Woocommerce 3 中获取订单商品和 WC_Order_Item_Product https://stackoverflow.com/questions/45706007/get-order-items-and-wc-order-item-product-in-woocommerce-3/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WooCommerce 3 中生成的发票中显示自定义总节省 的相关文章

随机推荐