このサイト(ここ)(WP v4.9)でWooCommerce v3.2.4を使用し、定額料金が適用されているオーバーウェイト/オーバーサイズの配送クラスの11の製品:カナダで
$ 20、米国で$ 25。
他のすべての製品は、注文が100ドルを超えない限り、10ドル(カナダ)と15ドル(米国)の定額配送があり、送料無料が自動的に適用されます。
カートに重量超過/サイズ超過の商品がある場合、クライアントは送料無料を無効にしたいと考えています。問題は、カート内に通常の商品と特大の商品が混在している場合、利用できる配送方法がないとカートに表示され、配送方法が適用されないことです。
私が使用していますXAdapter Woocommerce送料表レートを「太りすぎ」配送クラスに高いコストを適用するプラグイン。
更新
WooCommerce Shipping Zone設定を使用して、特定の配送クラスに定額料金を設定できることに気付いたので、このプラグインを無効にしました。以下のスクリーンショットを参照してください。
私はいくつかのコードを使用して:
Here is that code:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package){
$hide_when_shipping_class_exist = array(
163 => array(
'flat_rate:1',
'flat_rate:2',
'free_shipping:3',
'free_shipping:5'
)
);
$hide_when_shipping_class_not_exist = array(
163 => array( 'wf_woocommerce_shipping_pro:overweightoversizeoverweight')
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
EDIT
Here is a list of IDs of the rates per shipping zone:
Canada
flat_rate:1
free_shipping:3
local_pickup:4
USA
flat_rate:2
free_shipping:5
UPDATE 2: (Without any plugin need, just settings and code)
The function below will always show "Local pickup" shipping for canada and will:
そのコードは次のとおりです。
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($rates, $package){
// Defining & Initializing variables
$free_shipping_rates = array(
'free_shipping:3',
'free_shipping:5'
);
// Defining & Initializing variables
$shipping_class_id = 163;
$free = array();
$over_found = $has_free = false;
// Check if "Oversize" shipping class (163) is in cart items
foreach(WC()->cart->get_cart() as $key => $cart_item){
if($cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$over_found = true;
break;
}
}
// 1. Hiding free shipping but always show Local pickup for Canada
if( $over_found ){
foreach($free_shipping_rates as $rate_id) {
unset( $rates[$rate_id] );
}
}
// 2. Hiding Flat rate OR Free shipping --> depending on cart amount
// (but always show Local pickup for Canada)
else {
foreach ( $rates as $rate_id => $rate ) {
// Hide all "Flat rates" when "Free Shipping" is available
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
$has_free = true;
} elseif ( 'local_pickup' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
return $has_free ? $free : $rates;
}
return $rates;
}
コードは、アクティブな子テーマ(またはテーマ)のfunction.phpファイル、または任意のプラグインファイルに含まれます。
WooCommerce 3でテストされ、動作します。
配送キャッシュを更新します(時々必要です):
1)最初にカートを空にします。
2)このコードはすでにfunction.phpファイルに保存されています。
3)配送ゾーンの設定に移動し、1つの「定額」(たとえば)と「保存」を無効にします。次に、その「定額」と「保存」を再度有効にします。完了し、テストできます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加