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/77559475/show-local-pickup-shipping-method-only-for-specific-woocommerce-product
24 lines
838 B
Text
24 lines
838 B
Text
// Allow local pickup only for specific product Ids
|
|
add_action( 'woocommerce_package_rates', 'show_hide_local_pickup_shipping_methods', 10, 2 );
|
|
function show_hide_local_pickup_shipping_methods( $rates, $package ) {
|
|
|
|
// HERE BELOW the product Ids in the array allowing local pickup
|
|
$product_ids = array(12, 34, 56);
|
|
$product_found = false;
|
|
|
|
// Loop through cart items
|
|
foreach( $package['contents'] as $item ){
|
|
if( count( array_intersect($product_ids, [$item['variation_id'], $item['product_id']]) ) > 0 ) {
|
|
$product_found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Loop through shipping methods
|
|
foreach( $rates as $rate_key => $rate ) {
|
|
if( 'local_pickup' === $rate->method_id && ! $product_found ) {
|
|
unset($rates[$rate_key]);
|
|
}
|
|
}
|
|
return $rates;
|
|
}
|