将特定产品设置为仅在 WooCommerce 中的特定国家/地区发货
Set specific Products to be shipped only in specific countries in WooCommerce
如何在 Woocommerce 中将某些产品设置为只能运送到某些国家/地区?
例如结帐时,如果Country
不是USA
,我想显示消息"We cannot deliver to your country."
这主要可以通过挂接到 woocommerce_package_rates
过滤器挂钩中的自定义函数来完成。
下面的代码将允许您根据定义的国家/地区代码为特定产品 ID 或特定产品类别(您可以选择) 禁用送货。
您必须在第一个函数中添加您的设置。
代码:
// HERE your settings - Utility function
function your_country_shipping_settings(){
$results = array();
// Can be based on "Product IDs" (or "Product categories" ==> false)
$results['type'] = true; // or false
// Allowed countries (Only compatible country codes - 2 digits)
$results['countries'] = array( 'US', 'CA' );
if( $results['type'] ){
// Restricted product IDs
$results['matching'] = array( 37, 38 );
} else {
// Restricted product categories (IDs, Slugs or Names)
$results['matching'] = array('t-shirts', 'sweet-shirts' );
}
// Message
$results['message'] = __( "can not be delivered to your country.", "woocommerce" );
return $results;
}
// Utility function that check cart items
function get_items_names( $matching, $package, $type ){
$product_names = array();
// Search in cart items
foreach( $package['contents'] as $item ){
if( $type ){
if( in_array( $item['data']->get_id(), $matching ) )
$product_names[] = $item['data']->get_name();
} else {
if( has_term( $matching, 'product_cat', $item['product_id'] ) )
$product_names[] = $item['data']->get_name();
}
}
return $product_names;
}
// Conditionally disabling shipping methods
add_filter('woocommerce_package_rates','custom_country_shipping_rules', 10, 2 );
function custom_country_shipping_rules( $rates, $package ){
if( isset($package['destination']['country']) && isset($package['contents']) ){
// Load your settings
$data = your_country_shipping_settings();
// If matching allowed countries ==> We Exit
if( in_array( $package['destination']['country'], $data['countries'] ) )
return $rates; // Exit
$product_names = get_items_names( $data['matching'], $package, $data['type'] );
// When product match we Remove all shipping methods
if( count($product_names) > 0 ){
// Removing all shipping methods
foreach( $rates as $rate_id => $rate )
unset( $rates[$rate_id] );
}
}
return $rates;
}
// Conditionally displaying a shipping message
add_filter('woocommerce_cart_no_shipping_available_html','custom_country_shipping_notice', 10, 1 );
add_filter('woocommerce_no_shipping_available_html','custom_country_shipping_notice', 10, 1 );
function custom_country_shipping_notice( $html ){
$package = WC()->shipping->get_packages()[0];
if( isset($package['destination']['country']) && isset($package['contents']) ){
// Load your settings
$data = your_country_shipping_settings();
// If matching allowed countries ==> We Exit
if( in_array( $package['destination']['country'], $data['countries'] ) )
return $html; // Exit
$product_names = get_items_names( $data['matching'], $package, $data['type'] );
if( count($product_names) > 0 ){
$text = '"' . implode( '", "', $product_names ) . '" ' . $data['message'];
$html = wpautop( $text );
}
}
return $html;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。
当购物车中的商品与您的设置匹配时,如果客户位于所定义的其他国家/地区,他将获得类似以下信息:
在购物车页面上:
在结帐页面上:
如何在 Woocommerce 中将某些产品设置为只能运送到某些国家/地区?
例如结帐时,如果Country
不是USA
,我想显示消息"We cannot deliver to your country."
这主要可以通过挂接到 woocommerce_package_rates
过滤器挂钩中的自定义函数来完成。
下面的代码将允许您根据定义的国家/地区代码为特定产品 ID 或特定产品类别(您可以选择) 禁用送货。
您必须在第一个函数中添加您的设置。
代码:
// HERE your settings - Utility function
function your_country_shipping_settings(){
$results = array();
// Can be based on "Product IDs" (or "Product categories" ==> false)
$results['type'] = true; // or false
// Allowed countries (Only compatible country codes - 2 digits)
$results['countries'] = array( 'US', 'CA' );
if( $results['type'] ){
// Restricted product IDs
$results['matching'] = array( 37, 38 );
} else {
// Restricted product categories (IDs, Slugs or Names)
$results['matching'] = array('t-shirts', 'sweet-shirts' );
}
// Message
$results['message'] = __( "can not be delivered to your country.", "woocommerce" );
return $results;
}
// Utility function that check cart items
function get_items_names( $matching, $package, $type ){
$product_names = array();
// Search in cart items
foreach( $package['contents'] as $item ){
if( $type ){
if( in_array( $item['data']->get_id(), $matching ) )
$product_names[] = $item['data']->get_name();
} else {
if( has_term( $matching, 'product_cat', $item['product_id'] ) )
$product_names[] = $item['data']->get_name();
}
}
return $product_names;
}
// Conditionally disabling shipping methods
add_filter('woocommerce_package_rates','custom_country_shipping_rules', 10, 2 );
function custom_country_shipping_rules( $rates, $package ){
if( isset($package['destination']['country']) && isset($package['contents']) ){
// Load your settings
$data = your_country_shipping_settings();
// If matching allowed countries ==> We Exit
if( in_array( $package['destination']['country'], $data['countries'] ) )
return $rates; // Exit
$product_names = get_items_names( $data['matching'], $package, $data['type'] );
// When product match we Remove all shipping methods
if( count($product_names) > 0 ){
// Removing all shipping methods
foreach( $rates as $rate_id => $rate )
unset( $rates[$rate_id] );
}
}
return $rates;
}
// Conditionally displaying a shipping message
add_filter('woocommerce_cart_no_shipping_available_html','custom_country_shipping_notice', 10, 1 );
add_filter('woocommerce_no_shipping_available_html','custom_country_shipping_notice', 10, 1 );
function custom_country_shipping_notice( $html ){
$package = WC()->shipping->get_packages()[0];
if( isset($package['destination']['country']) && isset($package['contents']) ){
// Load your settings
$data = your_country_shipping_settings();
// If matching allowed countries ==> We Exit
if( in_array( $package['destination']['country'], $data['countries'] ) )
return $html; // Exit
$product_names = get_items_names( $data['matching'], $package, $data['type'] );
if( count($product_names) > 0 ){
$text = '"' . implode( '", "', $product_names ) . '" ' . $data['message'];
$html = wpautop( $text );
}
}
return $html;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。
当购物车中的商品与您的设置匹配时,如果客户位于所定义的其他国家/地区,他将获得类似以下信息:
在购物车页面上:
在结帐页面上: