-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ShippingMethod.php
88 lines (67 loc) · 2.41 KB
/
ShippingMethod.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace verbb\postie\models;
use verbb\postie\base\ProviderInterface;
use verbb\postie\events\ModifyShippingRuleEvent;
use craft\helpers\UrlHelper;
use craft\commerce\base\ShippingMethod as BaseShippingMethod;
class ShippingMethod extends BaseShippingMethod
{
// Constants
// =========================================================================
public const EVENT_MODIFY_SHIPPING_RULE = 'modifyShippingRule';
// Properties
// =========================================================================
public ?ProviderInterface $provider = null;
public ?float $rate = null;
public ?array $rateOptions = null;
public mixed $shippingMethodCategories = null;
// Public Methods
// =========================================================================
public function getType(): string
{
return $this->provider->name;
}
public function getId(): ?int
{
return null;
}
public function getName(): string
{
return (string)$this->name;
}
public function getHandle(): string
{
return (string)$this->handle;
}
public function getShippingRules(): array
{
$shippingRule = new ShippingRule();
$shippingRule->description = $this->name;
$shippingRule->baseRate = $this->rate;
$shippingRule->provider = $this->provider;
$shippingRule->shippingMethod = $this;
$shippingRule->options = $this->rateOptions;
// Drop any settings for the provider, these are returned with calculation requests
if (property_exists($shippingRule->provider, 'settings')) {
$shippingRule->provider->settings = [];
}
// Allow plugins to modify the rule
$modifyRuleEvent = new ModifyShippingRuleEvent([
'provider' => $this->provider,
'shippingMethod' => $this,
'shippingRule' => $shippingRule,
]);
if ($this->hasEventHandlers(self::EVENT_MODIFY_SHIPPING_RULE)) {
$this->trigger(self::EVENT_MODIFY_SHIPPING_RULE, $modifyRuleEvent);
}
return [$modifyRuleEvent->shippingRule];
}
public function getIsEnabled(): bool
{
return $this->enabled && isset($this->rate);
}
public function getCpEditUrl(): string
{
return UrlHelper::cpUrl('postie/settings/shipping-methods/' . $this->provider->handle . '/' . $this->getHandle());
}
}