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 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错