在 Woocommerce 中根据国家/地区禁用特定产品的运送

Leo*_*eon 2 php wordpress product cart woocommerce

如果客户发货国家/地区不是意大利,我正在尝试禁用特定产品的发货

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

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 );
Run Code Online (Sandbox Code Playgroud)

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

Loi*_*tec 8

\n

注意:此代码适用于 Woocommerce 3+ (但不适用于非常旧的版本 2.3)

\n
\n\n

您的问题不太清楚\xe2\x80\xa6,因此您主要有2个选项(并在两个选项的末尾检查“添加到购物车”,当客户注册时,当检测到运输国家/地区或已在购物车或结帐中设置时) :

\n\n

选项 1 - 您最好删除显示自定义通知\xe2\x80\xa6 的相关购物车项目,而不是删除不可运送到意大利以外的所有其他国家/地区的产品的运送方式,您必须在仅可在意大利发货的功能:

\n\n
add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );\nfunction checking_and_removing_items( $cart ) {\n    if ( is_admin() && ! defined( 'DOING_AJAX' ) )\n        return;\n\n    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )\n        return;\n\n    $custome_shipping_country = WC()->customer->get_shipping_country();\n\n    if( empty($custome_shipping_country) ){\n        $package = WC()->shipping->get_packages()[0];\n        if( ! isset($package['destination']['country']) ) return;\n        $custome_shipping_country = $package['destination']['country'];\n    }\n\n    // Only for NON Italians customers\n    if( $custome_shipping_country == 'IT' ) return;\n\n    // ==> HERE set your product IDs (ITALY ONLY)\n    $products_ids = array(37, 57);\n\n    // Iterate through each cart item\n    $found = false;\n    foreach( $cart->get_cart() as $cart_item_key => $cart_item )\n        if( in_array( $cart_item['data']->get_id(), $products_ids ) ){\n            $found = true;\n            $cart->remove_cart_item( $cart_item_key ); // remove item\n        }\n\n    if( $found ){\n         // Custom notice\n         wc_clear_notices();\n         wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(活动主题)的 function.php 文件中。

\n\n
\n

添加到购物车验证功能在最后\xe2\x80\xa6

\n
\n\n
\n\n

选项 2 - 删除不可向意大利以外的所有其他国家/地区发货的产品的发货方式,并显示自定义错误通知\xe2\x80\xa6 您必须在函数中定义仅可在意大利发货的产品 ID:

\n\n
add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );\nfunction disable_shipping_methods( $rates, $package ) {\n    if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )\n        return $rates;\n\n    // Only for NON Italians customers\n    if( $package['destination']['country'] == 'IT' ) return $rates;\n\n    // ==> HERE set your product IDs (ITALY ONLY)\n    $products_ids = array(37, 57);\n\n    // Loop through cart items and checking\n    $found = false;\n    foreach( $package['contents'] as $item )\n        if( in_array( $item['data']->get_id(), $products_ids ) ){\n            $found = true;\n            break;\n        }\n\n    if( ! $found ) return $rates; // If nothing is found: We EXIT\n\n    foreach( $rates as $rate_id => $rate )\n        unset($rates[$rate_id]); // Removing all shipping methods\n\n    // Custom notice\n    wc_clear_notices();\n    wc_add_notice('Some products are only shippable for Italy', 'error');\n\n    return $rates;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(活动主题)的 function.php 文件中。

\n\n
\n\n

添加到购物车验证功能并带有自定义通知(对于两个选项)。

\n\n

您必须在函数中定义仅可在意大利发货的产品 ID。

\n\n
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );\nfunction avoid_products_for_non_italian( $passed, $product_id, $quantity ) {\n\n    $custome_shipping_country = WC()->customer->get_shipping_country();\n\n    if( empty($custome_shipping_country) ){\n        $package = WC()->shipping->get_packages()[0];\n        if( ! isset($package['destination']['country']) ) return $passed;\n        $custome_shipping_country = $package['destination']['country'];\n    }\n\n    // Only for NON Italians customers\n    if( $custome_shipping_country == 'IT' ) return $passed;\n\n    // ==> HERE set your product IDs (ITALY ONLY)\n    $products_ids = array(37, 57);\n\n    // The condition\n    if( in_array( $product_id, $products_ids ) ){\n        $passed = false;\n        wc_add_notice( 'This product is only shippable for Italy.', 'error' );\n    }\n    return $passed;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(活动主题)的 function.php 文件中。

\n\n

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

\n