在Woocommerce中基于国家/地区禁用特定产品的运输 [英] Disable shipping for specific products based on country in Woocommerce

查看:28
本文介绍了在Woocommerce中基于国家/地区禁用特定产品的运输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果客户运输的国家/地区不是意大利,我将尝试禁用特定产品的运输

I am trying to disable shipping for a specific product if customer shipping country is not Italy

这是我的代码,但我不知道如何设置国家/地区条件:

Here is my code, but I don't know how to set the country condition:

function hide_shipping_when_class_is_in_cart( $rates, $package ) {
    // shipping class IDs that need the method removed
    $shipping_classes = array('bulky-items');
    $if_exists = false;

    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }

    if( $if_exists ) unset( $rates['free_shipping:7'] );

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );

如果所选的运送国家/地区不是意大利,如何禁用产品的运送?

How can I disable shipping for a product if the selected shipping country is not Italy?

推荐答案

注意:该代码是在Woocommerce 3+ (但不是非常老的2.3版本)中起作用的

Note: This code is made to work in Woocommerce 3+ (but not in very old version 2.3)

您的问题不太清楚...因此,您主要有2个选项(并在最后两个选项中都选中添加到购物车,当客户注册时,当检测到运输国家或在购物车或结帐中设置了运送国家时):

Your question is not so clear… so you have mainly 2 options (and checking add to cart at the end for both options, when customer is registered, when shipping country is detected or has been set in cart or checkout):

选项1 -除了删除不适用于除意大利以外的所有其他国家/地区的产品的运输方式外,您最好删除显示自定义通知的相关购物车项目……您必须定义仅在意大利提供的功能中的产品ID:

OPTION 1 - Instead of removing shipping methods for a product that is not shippable for all other countries than Italy, you should better remove the related cart item displaying a custom notice… You will have to define the product Ids in the function that are only shippable in Italy:

add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );
function checking_and_removing_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Iterate through each cart item
    $found = false;
    foreach( $cart->get_cart() as $cart_item_key => $cart_item )
        if( in_array( $cart_item['data']->get_id(), $products_ids ) ){
            $found = true;
            $cart->remove_cart_item( $cart_item_key ); // remove item
        }

    if( $found ){
         // Custom notice
         wc_clear_notices();
         wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');
    }
}

代码进入您的活动子主题(活动主题)的function.php文件中.

Code goes in function.php file of your active child theme (active theme).

添加到购物车验证功能在最后...

Add to cart validation feature is at the end…


选项2 -删除了除意大利以外其他所有国家/地区都无法发货的产品的发货方式,并显示了自定义错误通知…您将必须在仅用于此功能的产品中定义产品ID在意大利可发货:


OPTION 2 - Removing shipping methods for a product that is not shippable for all other countries than Italy and displaying a custom error notice… You will have to define the product Ids in the function that are only shippable in Italy:

add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
    if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
        return $rates;

    // Only for NON Italians customers
    if( $package['destination']['country'] == 'IT' ) return $rates;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Loop through cart items and checking
    $found = false;
    foreach( $package['contents'] as $item )
        if( in_array( $item['data']->get_id(), $products_ids ) ){
            $found = true;
            break;
        }

    if( ! $found ) return $rates; // If nothing is found: We EXIT

    foreach( $rates as $rate_id => $rate )
        unset($rates[$rate_id]); // Removing all shipping methods

    // Custom notice
    wc_clear_notices();
    wc_add_notice('Some products are only shippable for Italy', 'error');

    return $rates;
}

代码进入您的活动子主题(活动主题)的function.php文件中.

Code goes in function.php file of your active child theme (active theme).

添加到具有自定义通知的购物车验证功能(两个选项均适用).

Add to cart validation feature with a custom notice (for both options).

您将必须在只能在意大利提供的功能中定义产品ID.

You will have to define the product Ids in the function that are only shippable in Italy.

add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );
function avoid_products_for_non_italian( $passed, $product_id, $quantity ) {

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return $passed;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return $passed;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // The condition
    if( in_array( $product_id, $products_ids ) ){
        $passed = false;
        wc_add_notice( 'This product is only shippable for Italy.', 'error' );
    }
    return $passed;
}

代码进入您的活动子主题(活动主题)的function.php文件中.

Code goes in function.php file of your active child theme (active theme).

所有代码均经过测试,可用于Woocommerce 3+版本((也可能是2.6.x))

All code is tested and works for Woocommerce version 3+ (may be 2.6.x too)

这篇关于在Woocommerce中基于国家/地区禁用特定产品的运输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆