我们有仅运送到美国和加拿大的内部产品类别,也有国际运送的 POD 类别。我修改了代码:将特定产品设置为仅在 WooCommerce 中的特定国家/地区发货。
我想在购物车页面显示通知/警告并禁用结帐,除非来自美国和加拿大境外的客户删除购物车中的内部产品。
我在第一个函数中添加了两行代码。他们没有按预期工作。该通知在购物车中随处可见且多次出现。结帐按钮未禁用。我该如何修改这个?
我的代码:
function your_country_shipping_settings(){
$results = array();
// Can be based on "Product IDs" (or "Product categories" ==> false)
$results['type'] = false; // 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" );
//added by me, disable checkout
wc_add_notice( __( "This product is shipping to US address only.", "woocommerce" ), "error" );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
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;
}
内部产品将来可能会运送到美国和加拿大以外的地区,所以我不想隐藏它们。
首先,删除您的更改(从第一个函数):
//added by me, disable checkout
wc_add_notice( __( "This product is shipping to US address only.", "woocommerce" ), "error" );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
然后将以下挂钩函数添加到您的代码中:
add_action( 'woocommerce_calculate_totals', 'disable_checkout_submit_button' );
function disable_checkout_submit_button() {
$package = WC()->shipping->get_packages()[0];
if( is_cart() && 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; // Exit
$product_names = get_items_names( $data['matching'], $package, $data['type'] );
if( count($product_names) > 0 ){
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
wc_clear_notices();
wc_add_notice( __( 'Some products are shipping to US And Canada exclusively.', 'woocommerce' ), "error" );
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并工作。