【问题标题】:Extra cost for shipping method based on product categories [duplicate]基于产品类别的运输方式的额外费用[重复]
【发布时间】:2020-01-03 16:28:00
【问题描述】:

我正在尝试根据产品类别为用户选择的运输方式添加额外费用(使用 $multiplier)。

所以,我需要检查购物车中的任何产品是否在 $product_category_id 上。 如果是这样,那么我需要将选定的运费乘以 $multiplier 值。

此 $multiplier 应应用于按送货方式计算的总运费(而不是购物车中的每件商品)。

add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
    global $woocommerce;

        if ( get_field( 'multiplier', 'option' ) ) :
            $multiplier = get_field( 'multiplier', 'option' );
        else :
            $multiplier = 1;
        endif;

        $product_category_id    = array( 86, 21, 47 ); 
        $product_cats_ids       = wc_get_product_term_ids( $product_id, 'product_cat' ); 
        $product_category_id_check = false;

        foreach( $products as $cart_item ){
            $product_id = $cart_item['product_id'];
        }

        foreach ( $product_category_id as $key => $value ) {
            if ( in_array( $value, $product_cats_ids ) ) { 
                $product_category_id_check = true;
            }
        }

        if ( ! is_admin() && $product_category_id_check ) {
                    // If Shiptor 1
                    if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) {
                        $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier;
                    } 

                    // If Shiptor 2
                    if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) {
                        $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier;
                    }

                    //If anothee shipping method
                    if ( isset( $rates['rpaefw_post_calc:16'] )) {
                        $rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier;
                    }
        }


    return $rates;
}

执行代码后没有结果。我在代码中提供的运输方式没有应用 $multiplier。

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    在活动主题的functions.php中添加以下代码sn-p -

    function add_extra_woocommerce_shipping_rate_cost( $cost, $method ) {
        /* let's assume its applicable for flat rate and local pickup
         * And instance id for flat rate = 12
         * And instance id for local pickup = 14 
         * 
         */
        if ( in_array( $method->get_instance_id(), array( 12, 14) ) && WC()->cart ) {
            if ( get_field( 'multiplier', 'option' ) ) :
                $multiplier = get_field( 'multiplier', 'option' );
            else :
                $multiplier = 1;
            endif;
    
            $product_category_id = array( 86, 21, 47 ); // applicable categories
            $product_category_id_check = false;
            $items = WC()->cart->get_cart();
            foreach( $items as $item => $values ) { 
                $product_cats_ids = wp_get_post_terms($values['data']->get_id(),'product_cat',array('fields'=>'ids'));
                foreach ( $product_cats_ids as $id ) {
                    if( in_array( $id, $product_category_id ) ) 
                        $product_category_id_check = true;
                }
            }
            if( $product_category_id_check ){
                $cost = $cost * $multiplier;
            }
        }
        return $cost;
    }
    add_filter( 'woocommerce_shipping_rate_cost', 'add_extra_woocommerce_shipping_rate_cost', 99, 2 );
    

    【讨论】:

    • 但是我怎样才能为某些运输方式提供条件呢?我不需要对所有应用 $multiplier,而只需要对 shiptor-shiptor:14:delivery-point_shiptor_terminal + shiptor-shiptor:14:to-door_shiptor_courier + rpaefw_post_calc:16 应用(我在上面的代码中有这个条件)
    • 只需检查第一个 if 语句,使用数组运输方法条件,如` in_array($method->get_instance_id(), array('for_ship1_id', 'for_ship2_id')`
    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多