php WooCommerce运费基于购物车项目计数

roejwanj  于 2023-01-12  发布在  PHP
关注(0)|答案(3)|浏览(135)

我想计算运费的基础上,产品的数量添加到购物车一样,
如果我购买一个移动的然后它将计算运费为2. 5和超过两个或两个移动后,我购买然后运费将为5. 0

<?php
    $qty(1) * 2.5 = 2.5
    $qty(2) * 2.5 = 5.0
    $qty(3) * 2.5 = 5.0
?>

那么,有没有什么想法或建议,如何计算运费的基础上的产品数量?

68de4m5k

68de4m5k1#

您可以添加自定义费用:添加到主题functions.php或使用插件

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );

function woocommerce_custom_surcharge() {
  global $woocommerce;

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

    $price_per_mobile = 2.5;
    $shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);   
    $woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );

}
8yparm6h

8yparm6h2#

继续Woocommerce过滤器woocommerce_package_rates。在那里你可以自定义购物车页面中可用的所有运费。
这里是代码添加额外的成本,以所有项目的国内和国际航运

add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
    $origin_country = 'US';
    $amount_to_add_domestic = 10;
    $amount_to_add_inter_national = 20;

    $amount_to_add = ($package['destination']['country'] == $origin_country) ? 
    $amount_to_add_domestic : $amount_to_add_inter_national;

    $item_count = 0;
    foreach ($package['contents'] as $key => $item) {
        $item_count += $item['quantity'];
    }

    foreach ($available_shipping_methods as $methord_name => $methord) {
        $available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
    }
    return $available_shipping_methods;
}
vdzxcuhz

vdzxcuhz3#

    • 更新日期:**

由于您的问题有点不清楚,您只需在wooCommerce配送设置的统一费率配送方式成本(针对每个配送区域)中添加**[qty]*2.5**即可。

    • 但如果您的购物车中有2个不同的项目,则无法使用**,例如:* * 一米一米一**
    • 因此,此答案适用于所有情况:**

1)首先,您需要为每个配送区设置一个**"统一费率"配送方式,费用将设置为2.5**(在您的WooCommerce配送设置中)。
2)添加此代码,将计算每个购物车项目(根据项目的总数量)新更新的运费:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    // If there is more than 1 cart item
    if( $cart_count > 1 ){
        // Iterating through each shipping rate
        foreach($rates as $rate_key => $rate_values){
            // Targeting "Flat Rate" shipping method
            if ( 'flat_rate' === $rate_values->method_id ) {
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes; 
            }
        }
    }
    return $rates;
}
  • 代码在您的活动子主题(或主题)的function.php文件或任何插件文件中。*

此代码在WooCommerce 3+上测试并工作

    • 您将需要刷新装运区缓存:**禁用统一费率,然后保存。然后重新启用此费率并保存。

相关问题