Automattic\WooCommerce\Blocks\Shipping
ShippingController::remove_shipping_if_no_address
Remove shipping (i.e. delivery, not local pickup) if "Hide shipping costs until an address is entered" is enabled, and no address has been entered yet.
Only applies to block checkout because pickup is chosen separately to shipping in that context.
Method of the class: ShippingController{}
No Hooks.
Returns
Array.
Usage
$ShippingController = new ShippingController(); $ShippingController->remove_shipping_if_no_address( $packages );
- $packages(array) (required)
- Array of shipping packages.
ShippingController::remove_shipping_if_no_address() ShippingController::remove shipping if no address code WC 10.8.1
public function remove_shipping_if_no_address( $packages ) {
if ( 'shortcode' === WC()->cart->cart_context ) {
return $packages;
}
$shipping_cost_requires_address = wc_string_to_bool( get_option( 'woocommerce_shipping_cost_requires_address', 'no' ) );
// Return early here for a small performance gain if we don't need to hide shipping costs until an address is entered.
if ( ! $shipping_cost_requires_address ) {
return $packages;
}
$customer = WC()->customer;
if ( $customer instanceof WC_Customer && $customer->has_full_shipping_address() ) {
return $packages;
}
return array_map(
function ( $package ) {
// Package rates is always an array due to a check in core.
$package['rates'] = array_filter(
$package['rates'],
function ( $rate ) {
return $rate instanceof WC_Shipping_Rate && in_array( $rate->get_method_id(), LocalPickupUtils::get_local_pickup_method_ids(), true );
}
);
return $package;
},
$packages
);
}