猿问

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

一位朋友让我根据重量在购物车上添加额外费用,并且仅针对特定类别(或排除某些类别,这无关紧要)。

主题是,对于夏天,他想在包装中加冰以保持产品(例如牛奶、奶酪等)冷藏。

他还销售小工具和带导游参观他的工厂等,因此他不想对这些产品收取额外费用。

基于“根据Woocommerce 中的总重量添加自定义费用”答案,我的代码版本如下,对整个购物车应用额外费用,从该费用中排除访问产品,因为访问的权重显然为 0。

但我不是代码专家,我不知道如何插入一个数组以包含“牛奶”和“奶酪”等类别(反之亦然,以排除“访问”和“小工具”)。

上瘾了,我的代码增加了 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 );

        }

}

最后一个问题是:为什么 $i 变量可以是 0...1...1000000 而结果没有任何变化?代码似乎完全一样......


谢谢


BIG阳
浏览 104回答 1
1回答

莫回无

以下代码基于:预定义类别基于产品重量(属于预定义类别的产品)费用逐步增加(注释并在代码中添加解释)function shipping_weight_fee( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; /* SETTINGS */&nbsp; &nbsp; // Specific categories&nbsp; &nbsp; $specific_categories = array( 'categorie-1', 'categorie-2' );&nbsp; &nbsp; // Initial fee&nbsp; &nbsp; $fee = 1.20;&nbsp; &nbsp; // Steps of kg&nbsp; &nbsp; $steps_of_kg = 3;&nbsp; &nbsp; /* END SETTINGS */&nbsp; &nbsp; // Set variable&nbsp; &nbsp; $total_weight = 0;&nbsp; &nbsp; // Loop though each cart item&nbsp; &nbsp; foreach ( $cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get product id&nbsp; &nbsp; &nbsp; &nbsp; $product_id = $cart_item['product_id'];&nbsp; &nbsp; &nbsp; &nbsp; // Get weight&nbsp; &nbsp; &nbsp; &nbsp; $product_weight = $cart_item['data']->get_weight();&nbsp; &nbsp; &nbsp; &nbsp; // NOT empty & has certain category&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Quantity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_quantity = $cart_item['quantity'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add to total&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total_weight += $product_weight * $product_quantity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if ( $total_weight > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $increase_by_steps = ceil( $total_weight / $steps_of_kg );&nbsp; &nbsp; &nbsp; &nbsp; // Add fee&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( __( 'Extra for ice' ), $fee * $increase_by_steps, false );&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );
随时随地看视频慕课网APP
我要回答