0

我想在发票上的折扣券后显示产品的成本。我有以下代码,但它只显示基本价格,而不是优惠券代码后的成本:

add_filter( 'wpo_wcpdf_woocommerce_totals', 'wpo_wcpdf_woocommerce_totals_custom', 10, 2 );
function wpo_wcpdf_woocommerce_totals_custom( $totals, $order ) {
    $totals = array(
        'subtotal'  => array(
            'label' => __('Subtotal', 'wpo_wcpdf'),
            'value' => $order->get_subtotal_to_display(),
        ),
    );
    return $totals;
}

我想改成$totals$discount但没有用。

4

1 回答 1

1

您的实际代码只是删除所有总计,仅显示小计。请尝试以下钩子函数,该函数将在折扣金额后显示折扣小计:

add_filter( 'wpo_wcpdf_woocommerce_totals', 'add_discounted_subtotal_to_pdf_invoices', 10, 2 );
function add_discounted_subtotal_to_pdf_invoices( $totals, $order ) {
    // Get 'subtotal' raw amount value
    $subtotal = strip_tags($totals['cart_subtotal']['value']);
    $subtotal = (float) preg_replace('/[^0-9.]+/', '', $subtotal);

    // Get 'discount' raw amount value
    $discount = strip_tags($totals['discount']['value']);
    $discount = (float) preg_replace('/[^0-9.]+/', '', $discount);

    $new_totals = array();
    // Loop through totals lines
    foreach( $totals as $key => $values ){
        $new_totals[$key] = $totals[$key];
        // Inset new calculated 'Subtotal discounted' after total discount
        if( $key == 'discount' && $discount != 0 && !empty($discount) ){
            $new_totals['subtotal_discounted'] = array(
                'label' => __('Subtotal discounted', 'wpo_wcpdf'),
                'value' => wc_price($subtotal - $discount)
            );
        }
    }
    return $new_totals;
}

代码进入活动子主题(或主题)的 function.php 文件中。

测试和工作。它也应该对你有用。

于 2018-02-22T16:52:34.163 回答