php Woocommerce自定义运费计算基于重量

lsmepo6l  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(251)

在Woocommerce,我想有一个功能,我可以包括在我的主题,增加运费的基础上价格和重量。
1.如果价格高于20USD航运是免费的,低于运费:3美元
1.如果重量超过10公斤运费是2美元额外
基于 "航运计算的项目重量和购物车金额" 回答线程,我尝试了类似下面的代码:

//Adding a custom Shipping Fee to cart based conditionally on weight and cart amount
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1);
function custom_conditional_shipping_fee( $cart_object ){

    #### SETTINGS ####

    // Your targeted "heavy" product weight
    $target_weight = 1;

    // Your targeted cart amount
    $target_cart_amount = 20;

    // Price by Kg;
    $price_kg = 2;

    // Amount set in 'flat rate' shipping method;
    $flat_rate_price;

    // Initializing variables
    $fee = 0;
    $calculated_weight = 0;

    // For cart SUBTOTAL amount EXCLUDING TAXES
    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;

    // For cart SUBTOTAL amount INCLUDING TAXES (replace by this):
    // WC()->cart->subtotal >= $target_cart_amount ? $passed = true : $passed = false ;

    // Iterating through each cart items
    foreach( $cart_object->get_cart() as $cart_item ){

        // Item id ($product ID or variation ID)
        if( $cart_item['variation_id'] > 0)
            $item_id = $cart_item['variation_id'];
        else
            $item_id = $cart_item['product_id'];

        // Getting the product weight
        $product_weight = get_post_meta( $item_id , '_weight', true);

        // Line item weight
        $line_item_weight = $cart_item['quantity'] * $product_weight;

        // When cart amount is up to 1kg, Adding weight of heavy items
        if($passed && $product_weight < $target_weight)
            $calculated_weight += $line_item_weight;
    }

    #### Making the fee calculation ####

    // Cart is up to 250 with heavy items
    if ( $passed && $calculated_weight != 0 ) {
        // Fee is based on cumulated weight of heavy items
        $fee = ($calculated_weight * $price_kg) - $flat_rate_price;
    }
    // Cart is below 250
    elseif ( !$passed ) {
        // Fee is based on cart total weight
        $fee = ($cart_object->get_cart_contents_weight( ) * $price_kg) - $flat_rate_price;
    }

    #### APPLYING THE CALCULATED FEE ####

    // When cart is below 250 or when there is heavy items
    if ($fee > 0){
        // Rounding the fee
        $fee = round($fee);
        // This shipping fee is taxable (You can have it not taxable changing last argument to false)
        $cart_object->add_fee( __('Shipping weight fee', 'woocommerce'), $fee, true);
    }
}
    • 编辑:**

同时,我希望它立即在购物车页面上显示这一点。现在它显示"输入地址查看运输选项"。基本上只需查看购物车总数,并根据重量和价格描述的规则显示费率或免费送货。

56lgkhnf

56lgkhnf1#

woocommerce_package_rates是自定义运费的正确过滤器。
您可以通过以下方式实现这一点。
步骤-1:创建两种运输方式,免费送货统一费率与海岸3$
步骤2:复制并粘贴以下代码片段到functions.php
在代码片段中正确配置统一费率和免费送货。

add_filter( 'woocommerce_package_rates',  'modify_shipping_rate', 15, 2 );
function modify_shipping_rate( $available_shipping_methods, $package ){

    global $woocmmerce;
    $total_weight = WC()->cart->cart_contents_weight;
    $total_coast = WC()->cart->get_cart_contents_total();

    if( $total_coast >= 20 ){
        unset($available_shipping_methods['flat_rate:1']); //Remove flat rate for coat abobe 20$
    }elseif( $total_weight > 10 ){
        unset($available_shipping_methods['free_shipping:1']); // remove free shipping for below 20$ 
        $available_shipping_methods['flat_rate:1']->cost += 2; // add 2$ if weight exceeds 10KG
    }else{
        unset($available_shipping_methods['free_shipping:1']); // remove free shipping for below 20$ 
    }

    return $available_shipping_methods;
}

使用以下代码段更改默认购物车消息。

add_filter( 'woocommerce_cart_no_shipping_available_html', 'change_msg_no_available_shipping_methods', 10, 1  );
add_filter( 'woocommerce_no_shipping_available_html', 'change_msg_no_available_shipping_methods', 10, 1 );
function change_msg_no_available_shipping_methods( $default_msg ) {

    $custom_msg = "Enter address to view shipping options";
    if( empty( $custom_msg ) ) {
      return $default_msg;
    }

    return $custom_msg;
}
8ulbf1ek

8ulbf1ek2#

下面的代码不是基于自定义费用,而是基于自定义发货方式。它需要在发货设置中为每个发货区域进行设置:

  • 具有确定成本的"统一费率"(您可享受1月1日的服务)
  • 无要求 (无限制) 的"免费送货"。

该代码将根据重量和税款计算处理统一费率计算成本。
该代码适用于任何运输区域,无需在代码中定义运输方法ID。
下面是代码:

add_filter( 'woocommerce_package_rates',  'filter_package_rates_callback', 10, 2 );
function filter_package_rates_callback( $rates, $package ) {

    ## -------- Settings -------- ##
    $targeted_total   = 20; // The targeted cart amount
    $weight_threshold = 10; // The cart weight threshold
    $extra_for_10kg   = 2;  // 10 Kg addition extra cost;

    $total_weight    = WC()->cart->get_cart_contents_weight();
    $cart_subtotal   = WC()->cart->get_subtotal(); // Excluding taxes

    // Set shipping costs based on weight
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;

        if( $cart_subtotal < $targeted_total || $total_weight >= $weight_threshold ){
            // Remove Free shipping Method
            if( 'free_shipping' === $rate->method_id ) {
                unset( $rates[$rate_key] );
            }
            // Flat rate calculation cost when 10 kg weight is reached
            if( 'flat_rate' === $rate->method_id && $total_weight >= $weight_threshold ) {
                // The default rate cost (set in the shipping method)
                $default_cost = $rate->cost; 

                // The new calculated cost (up to 10 kg)
                $new_cost = $default_cost + $extra_for_10kg; 

                // Tax rate conversion (for tax calculations)
                $tax_rate_converion = $new_cost / $default_cost;

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // TAXES RATE COST (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // New tax calculated cost
                        $taxes[$key] = $tax * $tax_rate_converion; 
                        $has_taxes = true;
                    }
                }
                // Set new taxes cost
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        } else {
            // Remove Flat Rate methods (keeping Free Shipping Method only)
            if( 'flat_rate' === $rate->method_id ) {
                unset( $rates[$rate_key] );
            }
        }
    }
    return $rates;
}

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

    • 刷新发货缓存:**(必填)

1)此代码已经保存在function.php文件中。
2)在配送区域设置中,禁用/保存任何配送方式,然后启用返回/保存。

    • 您已完成**,可以进行测试。
    • 直接显示发货方式***(关于您的问题编辑)*

您在问题中提供的信息不足以了解如何管理这一点。
如果您在一个国家/地区销售,并且您有唯一的配送区域,您可以使用以下方法强制未登录客户所在的国家/地区显示配送方式:

add_action( 'template_redirect', 'allow_display_shipping_methods' );
function allow_display_shipping_methods() {
    // HERE define the targeted country code
    $country_code = 'GB';

    // Set the shipping country if it doesn't exist
    if( ! WC()->customer->get_shipping_country() )
        WC()->customer->set_shipping_country('GB');
}

代码进入您的活动子主题(或活动主题)的function.php文件。
现在,这是另一个问题,在您最初的问题中,应作为新问题提出
相关答案:

  • 在WooCommerce中设置根据购物车总重量计算的自定义运费
  • 免费送货取决于重量和最低购物车金额
  • 运费基于Woocommerce 3中的购物车总重量
  • 隐藏和取消隐藏WooCommerce中基于运输类别的特定运输方式

相关问题