mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
https://stackoverflow.com/questions/66636944/woocommerce-deposits-keep-only-specific-shipping-methods-for-deposits/66642821
24 lines
814 B
Text
24 lines
814 B
Text
add_filter( 'woocommerce_package_rates', 'only_local_pickup_for_deposits', 100, 2 );
|
|
function only_local_pickup_for_deposits( $rates, $package ) {
|
|
$has_deposit = false;
|
|
|
|
// Loop through cart items for the current shipping package
|
|
foreach( $package['contents'] as $item ) {
|
|
if ( isset($item['is_deposit']) && $item['is_deposit'] ) {
|
|
$has_deposit = true;
|
|
break; // Stop the loop
|
|
}
|
|
}
|
|
|
|
// If deposit is enabled for a cart item
|
|
if( $has_deposit ) {
|
|
// Loop through shipping rates
|
|
foreach ( $rates as $rate_key => $rate ) {
|
|
// Remove all shipping methods except "Local pickup"
|
|
if ( 'local_pickup' !== $rate->method_id ) {
|
|
unset($rates[$rate_key]);
|
|
}
|
|
}
|
|
}
|
|
return $rates;
|
|
}
|