[英]Disable shipping methods having a specific word and based on a shipping class in WooCommerce
[英]Disable shipping for specific products based on country in 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 );
如果選擇的發貨國家/地區不是意大利,我如何禁用產品的發貨?
注意:此代碼適用於 Woocommerce 3+ (但不適用於非常舊的 2.3 版)
您的問題不太清楚……所以您主要有 2 個選項(並在最后檢查這兩個選項的添加到購物車,當客戶注冊時,當檢測到運輸國家或已在購物車或結帳中設置時) :
選項 1 - 與其刪除無法在意大利以外的所有其他國家/地區發貨的產品的運輸方式,您最好刪除顯示自定義通知的相關購物車項目……您必須在僅可發貨的功能中定義產品 ID在意大利:
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 文件中。
添加到購物車驗證功能在最后......
選項 2 - 刪除無法在意大利以外的所有其他國家/地區發貨的產品的運輸方式並顯示自定義錯誤通知……您必須在功能中定義只能在意大利發貨的產品 ID:
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 文件中。
使用自定義通知添加到購物車驗證功能(對於兩個選項)。
您必須在函數中定義只能在意大利發貨的產品 ID。
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 文件中。
所有代碼都經過測試,適用於 Woocommerce 版本 3+ (也可能是 2.6.x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.