| 1 |
<?php |
| 2 |
|
| 3 |
use Disco\App\Disco; |
| 4 |
|
| 5 |
if ( ! function_exists( 'disco_add_free_shipping_rate' ) ) { |
| 6 |
|
| 7 |
/** |
| 8 |
* Add free shipping rate to the package. If any campaign matches, then apply free shipping. |
| 9 |
* |
| 10 |
* @param array $rates Array of rates found for the package. |
| 11 |
* @return array |
| 12 |
*/ |
| 13 |
function disco_add_free_shipping_rate( $rates ) { |
| 14 |
$is_free_shipping = ( new Disco )->apply_free_shipping( WC()->cart ); |
| 15 |
|
| 16 |
if ( ! $is_free_shipping ) { |
| 17 |
return $rates; |
| 18 |
} |
| 19 |
|
| 20 |
foreach ( $rates as $rate_id => $rate ) { |
| 21 |
if ( 'free_shipping' === $rate->method_id ) { |
| 22 |
return $rates; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// If free shipping is not available, override rates with free shipping |
| 27 |
// TODO: Add Label from campaign |
| 28 |
$free_shipping_rate = new WC_Shipping_Rate( 'disco_free_shipping', __( 'Free Shipping', 'disco' ), 0, array(), 'disco_free_shipping' ); |
| 29 |
$rates['disco_free_shipping'] = $free_shipping_rate; |
| 30 |
|
| 31 |
return $rates; |
| 32 |
} |
| 33 |
|
| 34 |
add_filter( 'woocommerce_package_rates', 'disco_add_free_shipping_rate', 100, 1 ); |
| 35 |
} |
| 36 |
|