php - WooCommerce 自定义运费最低所需小计以及税金计算

标签 php wordpress woocommerce tax shipping-method

我已经在 WooCommerce 中设置了一些启用应税选项的运输方式。如果花费超过 X 金额,我使用此代码设置不同的运费:

// Extra discount on shipping for orders of values of above 150 or 100.
function custom_adjust_shipping_rate( $rates ) {
    $cart_subtotal = WC()->cart->subtotal;

    // Check if the subtotal is greater than value specified
    if ( $cart_subtotal >= 29.99 ) {

        // Loop through each shipping rate
        foreach ( $rates as $rate ) {

            // Store the previous cost in $cost
            $cost = $rate->cost;

            // Adjust the cost as needed
            // original shipping greater than 6 discount by 5 
            if ( $cost == 7.38 ) {
                // discount rate by 5
                $rate->cost = $cost - 2.54;
            }
            // Optional discount for other shipping rates 
            if ( $cost == 4.10 ) {
                $rate->cost = $cost - 5;
            }
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'custom_adjust_shipping_rate', 10 );

但问题是税收计算。我的代码不处理税收。

如何调整自定义运费中的税费?

如何在成本计算中启用税收?

如有任何建议,我们将不胜感激。

最佳答案

要在代码中启用运费计算,请改用以下重新访问的代码:

add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates_cost', 10, 2 );
function adjust_shipping_rates_cost( $rates, $package ) {
    $min_subtotal  = 30; // Set min subtotal
    $cart_subtotal = 0; // Initializing
        
    // Loop through cart items to get items total for the current shipping package 
    foreach( $package['contents'] as $item ) {
        $cart_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
        // $cart_subtotal += $item['line_subtotal']; // Or without taxes
    }

    // Check if the subtotal is greater than specified value
    if ( $cart_subtotal < $min_subtotal ) {
        return $rates; // Exit
    }

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        $has_taxes = false; // Initializing
        $taxes     = array(); // Initializing
        $new_cost = $initial_cost = $rate->cost; // grab initial cost

        if ( $initial_cost == 7.38 ) {
            $new_cost -= 2.54;
        } elseif ( $initial_cost == 4.10 ) {
            $new_cost -= 5;
        }
        $rates[$rate_key]->cost = $new_cost; // Set new rate cost

        // Loop through taxes array (change taxes rate cost if enabled)
        foreach ($rate->taxes as $key => $tax){
            if( $tax > 0 ){
                // Get the tax rate conversion
                $tax_rate    = $tax / $initial_cost;

                // Set the new tax cost in the array
                $taxes[$key] = $new_cost * $tax_rate;
                $has_taxes   = true; // Enabling tax changes
            }
        }
        // set array of shipping tax cost
        if( $has_taxes ) {
            $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

代码位于事件子主题(或事件主题)的functions.php 文件中。它应该可以工作。

不要忘记清空购物车以刷新运输缓存数据。

关于php - WooCommerce 自定义运费最低所需小计以及税金计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66641133/

相关文章:

php - 获取当天的最后一秒

php - 如何在 PHP 中将电子邮件附件保存到服务器?

mysql - 从普通 SQL 查询 WordPress 数据库

mysql - 有没有什么方法可以将新的 Wordpress 站点与 RoR Web 应用程序连接起来,并从 Wordpress 站点管理其旧数据库,而无需进行数据迁移?

php - 迷你卡中的自定​​义价格值仅在单个产品站点上是错误的

java - Android-无法使用 woocommerce api 创建订单

javascript - 如何防止在 PHP 或 javascript 中使用 xlink 的 SVG 中的 billion laugh 攻击?

php - 如何确定变量的内存占用(大小)?

javascript - 如何制作 Flickr 徽章打开新窗口

css - 如何解决我的 Woocommerce 网站上的行内 block 对齐问题?