簡體   English   中英

根據費用有條件地自定義WooCommerce購物車的總計輸出

[英]Conditionally customize WooCommerce cart grand total output based on a fee

我嘗試做一件非常簡單的事情,但對我來說,我無法正常工作。

這來自wc-cart-functions.php

        if ( ! empty( $tax_string_array ) ) {
            $taxable_address = WC()->customer->get_taxable_address();
            $estimated_text  = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
                ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
                : '';
            $value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
        }
    }

    echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
}

如果應用了任何$fee則$ value應該不同。 但是我只從$fee變量中獲得0.00 €值(如果我檢查)。

有人可以幫我簡單地在變量中應用FEE的值,並檢查一個簡單的if子句,例如:

如果收取費用---> 1
其他-> 2

我該怎么做?

如您所見,您可以使用位於wc-cart-functions.php核心代碼中的woocommerce_cart_totals_order_total_html過濾器掛鈎,來更改grand cart total的輸出:

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_cart_totals_order_total_html', 10, 1 );
function custom_cart_totals_order_total_html( $value ) {
    // Get the fee cart amount
    $fees_total = WC()->cart->fee_total;

    // HERE is the condition
    if( ! empty($fees_total) && $fees_total != 0 ){
        // Change/customize HERE $value (just for test)
        $value .= " <small>($fees_total)<small>";
    }

    // Always return $value
    return $value;
}

代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。

此代碼已經過測試並且可以工作。

永遠不要覆蓋核心文件,因為許多原因,這是禁止的,危險的且不方便的操作

您將需要在$value條件中自定義代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM