| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\CartRules; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Roles; |
| 4 |
use TierPricingTable\Settings\CustomOptions\TPTCheckboxListOption; |
| 5 |
use TierPricingTable\Settings\Sections\SubsectionAbstract; |
| 6 |
use WC_Shipping_Zone; |
| 7 |
use WC_Shipping_Zones; |
| 8 |
|
| 9 |
class ShippingMethodsSubsection extends SubsectionAbstract { |
| 10 |
|
| 11 |
public function getTitle(): string { |
| 12 |
return __( 'Shipping methods by role', 'tier-pricing-table' ); |
| 13 |
} |
| 14 |
|
| 15 |
public function getDescription(): string { |
| 16 |
if ( ! $this->getMethods() ) { |
| 17 |
return __( 'No shipping method is set up yet. Add shipping zones and methods under WooCommerce → Settings → Shipping, then choose here who may use each.', 'tier-pricing-table' ); |
| 18 |
} |
| 19 |
|
| 20 |
return __( 'Who may use each shipping method of each zone, for example free shipping for retail customers only. Leave a method empty to keep it available to everyone.', 'tier-pricing-table' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function getSlug(): string { |
| 24 |
return 'shipping-methods'; |
| 25 |
} |
| 26 |
|
| 27 |
public function getSettings(): array { |
| 28 |
$settings = array(); |
| 29 |
|
| 30 |
foreach ( $this->getMethods() as $instanceId => $title ) { |
| 31 |
$settings[] = array( |
| 32 |
'title' => $title, |
| 33 |
'id' => CartRulesSettings::optionId( 'shipping_' . $instanceId ), |
| 34 |
'type' => TPTCheckboxListOption::FIELD_TYPE, |
| 35 |
'display' => 'checkboxes', |
| 36 |
'options' => Roles::getOptions(), |
| 37 |
'default' => '', |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
return $settings; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @return array<int, string> instance id => "Zone: Method", enabled methods only |
| 46 |
*/ |
| 47 |
protected function getMethods(): array { |
| 48 |
if ( ! class_exists( WC_Shipping_Zones::class ) ) { |
| 49 |
return array(); |
| 50 |
} |
| 51 |
|
| 52 |
$zones = array(); |
| 53 |
|
| 54 |
foreach ( WC_Shipping_Zones::get_zones() as $zone ) { |
| 55 |
$zones[] = new WC_Shipping_Zone( (int) $zone['id'] ); |
| 56 |
} |
| 57 |
|
| 58 |
$zones[] = new WC_Shipping_Zone( 0 ); // locations not covered by the other zones |
| 59 |
|
| 60 |
$methods = array(); |
| 61 |
|
| 62 |
foreach ( $zones as $zone ) { |
| 63 |
foreach ( $zone->get_shipping_methods( true ) as $method ) { |
| 64 |
$methods[ (int) $method->get_instance_id() ] = wp_strip_all_tags( $zone->get_zone_name() . ': ' . $method->get_title() ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $methods; |
| 69 |
} |
| 70 |
} |
| 71 |
|