dsj0312 2019-05-25 20:26
浏览 51
已采纳

如何根据Woocommerce中的付款选项更改送货区域?

Target: I want to add different shipping cost for the different payment gateways depending on the pin code user have.

Tried Using Shipping Zones:

I have 3 different shipping zones added in my woo-commerce dashboard. All the zones are created using the pin codes of the city and flat rate. However, if the pin code exists in multiple zones, then the top level zone gets the first priority and all other zones get ignored at the checkout page.

Now I want to change that zone depending on the Payment Gateway user chooses. For example user, 'A' has the pin code '123456' and this pin code exists in two Shipping Zones: 'SH1', 'SH2'. So, if the user chose 'Cash On Delivery' option, then I want to disable shipping zone 'SH1' for him while activating 'SH2'.

Here is the code I tried, but not works.

add_filter( 'woocommerce_available_payment_gateways', 'change_user_zone', 20, 1);
function change_user_zone(  $gateways ){

    $targeted_zones_names = array('COD FCity'); // Targeted zone names
    $current_payment_gateway = WC()->session->get('chosen_payment_method'); // Selected payment gateway

    // Current zone name
    $chosen_methods    = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
    $chosen_method     = explode(':', reset($chosen_methods) );
    $shipping_zone     = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
    $current_zone_name = $shipping_zone->get_zone_name();

    if($current_payment_gateway === "cod" && $current_zone_name === "COD FCity"){
        $WC_Shipping_Zone = new WC_Shipping_Zone();
        $var = $WC_Shipping_Zone->set_zone_locations( "India - Maharashtra - Mumbai" );
    }

    return $gateways;
}

Please help!

Note: I'm looking for a coding solution. If you want to suggest any plugin for this, then use the comment section, please.

  • 写回答

1条回答 默认 最新

  • doutou19761022 2019-05-26 19:00
    关注

    Ok, I don't find a way to change the shipping zone. So I added the extra fee in the cart depending on the Payment Gateway and Shipping Zone.

    add_filter( 'woocommerce_before_calculate_totals', 'update_the_cart_with_extra_fee');
    function update_the_cart_with_extra_fee(){
        global $woocommerce;  
        $targeted_zones_names = array('SH1'); // Targeted zone names
        $current_payment_gateway = WC()->session->get('chosen_payment_method'); // Selected payment gateway
    
        // Current zone name
        $chosen_methods    = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
        $chosen_method     = explode(':', reset($chosen_methods) );
        $shipping_zone     = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
        $current_zone_name = $shipping_zone->get_zone_name();
    
        if($current_payment_gateway === "cod" && $current_zone_name === "SH1"){
            $extra_shipping_cost = 0;  
            //Loop through the cart to find out the extra costs  
            foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {  
                //Get the product info  
                $_product = $values['data'];  
    
                //Get the custom field value - make sure it's i.e. 10 not $10.00  
                $custom_shipping_cost = 60; 
    
    
                //Adding together the extra costs 
                $extra_shipping_cost = $extra_shipping_cost + $custom_shipping_cost;
    
                $woocommerce->cart->add_fee( __('Cash on Delivery', 'woocommerce'), $extra_shipping_cost ); // Place this outside of foreach loop if you want to add fee for every product in the cart.
            }  
        }
    
    }
    

    I hope this will help someone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable