简体   繁体   English

Woocommerce - 仅在某些类别(或除某些类别之外的所有购物车)中根据重量在购物车中添加附加费

[英]Woocommerce - add surcharge in cart based on weight only to some categories (or to all cart except some categories)

A friends ask me to add an extra fee on cart based on weight and only for specific categories (or excluding some categories, it doesn't matters).有朋友要求我根据重量在购物车上添加额外费用,并且仅针对特定类别(或排除某些类别,没关系)。

The topic is, for summer, he wants to add ice into the packages to keep the products cold (ex. milk, cheese, etc).主题是,在夏天,他想在包装中加入冰块以保持产品冷藏(例如牛奶、奶酪等)。

He sells also gadgets and guided visits to his factory, etc so he doesn't want to apply the extra fee to those products.他还销售小工具和参观他的工厂等,因此他不想对这些产品收取额外费用。

Based on " Add custom fee based on total weight in Woocommerce " answer, my code version below, apply an extra fee to the whole cart, excluding the visit-product from this fee because the weight of the visit is obviously 0.基于“ 根据 Woocommerce 中的总重量添加自定义费用”的答案,我的代码版本在下面,对整个购物车收取额外费用,不包括访问产品,因为访问的权重显然是 0。

But I am not an expert with code and I don't know how to insert an array to include categories like 'milk' and 'cheese' (or viceversa to exclude 'visits' and 'gadgets').但我不是代码专家,我不知道如何插入一个数组以包含“牛奶”和“奶酪”等类别(反之亦然以排除“访问”和“小工具”)。

In addiction, my code increase the fee by steps of 3kg (due to sizing of the packets by DHL/UPS/GLS etc)令人上瘾的是,我的代码将费用增加了 3 公斤(由于 DHL/UPS/GLS 等对数据包的大小)

/* Extra Fee based on weight */

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Convert in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 0.00; // initial fee 


    // if cart is > 0 add €1,20 to initial fee by steps of 3000g
    if( $cart_weight > 0 ){
        for( $i = 0; $i < $cart_weight; $i += 3000 ){
            $fee += 1.20;
        }
    }    

    // add the fee / doesn't show extra fee if it's 0
    if ( !empty( $fee )) {
    $cart->add_fee( __( 'Extra for ice' ), $fee, false );
        }
}

Last question is: why the $i variable could be 0...1...1000000 without any change in the results?最后一个问题是:为什么 $i 变量可以是 0...1...1000000 而结果没有任何变化? the code seems working exactly the same...代码似乎完全一样......

thanks谢谢

The following code is based on:以下代码基于:

  • Predefined categories预定义类别
  • Based on product weight (of products belonging to the predefined categories)基于产品重量(属于预定义类别的产品)
  • Fee increases by steps费用逐步增加

(comment with explanation added in the code) (注释并在代码中添加解释)

function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    /* SETTINGS */

    // Specific categories
    $specific_categories = array( 'categorie-1', 'categorie-2' );

    // Initial fee
    $fee = 1.20;

    // Steps of kg
    $steps_of_kg = 3;

    /* END SETTINGS */

    // Set variable
    $total_weight = 0;

    // Loop though each cart item
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];

        // Get weight
        $product_weight = $cart_item['data']->get_weight();

        // NOT empty & has certain category     
        if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Add to total
            $total_weight += $product_weight * $product_quantity;
        }
    }

    if ( $total_weight > 0 ) {          
        $increase_by_steps = ceil( $total_weight / $steps_of_kg );

        // Add fee
        $cart->add_fee( __( 'Extra for ice' ), $fee * $increase_by_steps, false );      
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM