php - 在 woocommerce 中添加总百分比(包括运费)

标签 php wordpress woocommerce

在 Woocommerce 中,我想在总计上增加 3.5%(包括运费)。现在我正在使用这些代码,但这只计算不含运费。

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation' );
function woocommerce_custom_donation() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        $percentage = 0.035; // 3.5%
        // uncomment the line below to include shipping fees in the donation/surcharge calculation
        // $donation = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
    $donation = $woocommerce->cart->cart_contents_total * $percentage;
    $woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}

最佳答案

你也可以试试这个。

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation',10, 1 );
function woocommerce_custom_donation() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        $percentage     = 3.5;
        $cart_total     = $woocommerce->cart->cart_contents_total;
        $shipping_total = $woocommerce->cart->get_shipping_total() ?: 0;
        $tax_total      = $woocommerce->cart->get_taxes_total() ?: 0;

        $grand_total = $cart_total + $shipping_total + $tax_total;
        $donation = $grand_total * $percentage / 100;
        $woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}

enter image description here

关于php - 在 woocommerce 中添加总百分比(包括运费),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69051889/

相关文章:

php - WooCommerce 仅显示已购买的商品

php - 使用 PHP 进行数据转换和加载

php - 安全问题,php/mysql

php - laravel 在插入一行后无法检索 ID

php - 检查页面后如何在实际的 PHP 文件中找到代码?

wordpress - 客户登录后的 Woocommerce Hook

mysql - 向 MySQL 数据库添加未知数量的值

php - 拉维尔 4 : Stock Auth login won't persist across pages

php - 在 WooCommerce WC_Product get_description() 方法中启用简码使用

mysql - SQL查询按发布日期批量删除woocommerce产品