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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用