| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sendy\WooCommerce\ShippingMethods; |
| 4 |
|
| 5 |
use WC_Shipping_Flat_Rate; |
| 6 |
|
| 7 |
/** |
| 8 |
* @mixin WC_Shipping_Flat_Rate |
| 9 |
*/ |
| 10 |
trait ShippingMethodTrait |
| 11 |
{ |
| 12 |
public function init_form_fields() |
| 13 |
{ |
| 14 |
$this->form_fields = []; |
| 15 |
} |
| 16 |
|
| 17 |
public function calculate_shipping($package = []) |
| 18 |
{ |
| 19 |
// Set free shipping rate if cart subtotal exceeds minimum_for_free_shipping |
| 20 |
$minimum_for_free_shipping = $this->get_option('minimum_for_free_shipping'); |
| 21 |
|
| 22 |
if ('' !== $minimum_for_free_shipping && $package['cart_subtotal'] > $minimum_for_free_shipping) { |
| 23 |
$rate = [ |
| 24 |
'id' => $this->get_rate_id(), |
| 25 |
'label' => $this->title, |
| 26 |
'cost' => 0, |
| 27 |
'package' => $package, |
| 28 |
]; |
| 29 |
|
| 30 |
$this->add_rate($rate); |
| 31 |
} else { |
| 32 |
parent::calculate_shipping($package); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param array<string,array<string,mixed>> $form_fields |
| 38 |
* @return array<string,array<string,mixed>> |
| 39 |
*/ |
| 40 |
public function instance_form_fields(array $form_fields): array |
| 41 |
{ |
| 42 |
$form_fields['title']['default'] = $this->method_title; |
| 43 |
|
| 44 |
$form_fields['minimum_for_free_shipping'] = [ |
| 45 |
// translators: %s contains the currency symbol of the shop |
| 46 |
'title' => sprintf(esc_html__('Free shipping from %s', 'sendy'), get_woocommerce_currency_symbol()), |
| 47 |
'type' => 'number', |
| 48 |
'desc_tip' => esc_html__('Keep empty if you don’t want to use Free shipping', 'sendy'), |
| 49 |
'default' => 0, |
| 50 |
]; |
| 51 |
|
| 52 |
return $form_fields; |
| 53 |
} |
| 54 |
} |
| 55 |
|