根据产品类别的运输方式的额外费用

时间:2019-08-30 12:00:58

标签: woocommerce

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

因此,我需要检查购物车中是否有任何产品在$ product_category_id上。 如果是这样,那么我需要将选定的运输成本乘以$ 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。

1 个答案:

答案 0 :(得分:0)

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

 private void MainWindow_Activated(object sender, EventArgs e)
 {
       var pid = Process.GetCurrentProcess().Id;
       // do something
 }