PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Shipping / BaseShippingMethod.php

BaseShippingMethod.php in Packeta trunk, at src/Packetery/Module/Shipping/BaseShippingMethod.php

207 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Shipping;
6
7 use Packetery\Latte\Engine;
8 use Packetery\Module\Carrier;
9 use Packetery\Module\Checkout\ShippingRateFactory;
10 use Packetery\Module\CompatibilityBridge;
11 use Packetery\Module\Framework\WcAdapter;
12 use Packetery\Module\Framework\WpAdapter;
13 use Packetery\Nette\DI\Container;
14
15 use function is_array;
16 use function sprintf;
17
18 abstract class BaseShippingMethod extends \WC_Shipping_Method {
19 public const PACKETA_METHOD_PREFIX = 'packeta_method_';
20
21 // Will be overwritten.
22 public const CARRIER_ID = '';
23
24 /**
25 * @var null|false|array<string, string|null>
26 */
27 private $options;
28
29 /**
30 * @var Container
31 */
32 private $container;
33
34 /**
35 * @var ShippingRateFactory
36 */
37 private $shippingRateFactory;
38
39 /**
40 * @var Carrier\EntityRepository|null
41 */
42 protected $carrierRepository;
43
44 /**
45 * @var WpAdapter
46 */
47 private $wpAdapter;
48
49 /**
50 * @var WcAdapter
51 */
52 private $wcAdapter;
53
54 /**
55 * @param int $instanceId Shipping method instance id.
56 */
57 public function __construct( int $instanceId = 0 ) {
58 parent::__construct();
59
60 $this->container = CompatibilityBridge::getContainer();
61 $this->carrierRepository = $this->container->getByType( Carrier\EntityRepository::class );
62 $this->wpAdapter = $this->container->getByType( WpAdapter::class );
63 $this->wcAdapter = $this->container->getByType( WcAdapter::class );
64
65 $this->id = static::getShippingMethodId();
66 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
67 $this->instance_id = $this->wpAdapter->absint( $instanceId );
68 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
69 $this->method_title = __( 'Packeta', 'packeta' );
70 $this->title = __( 'Packeta', 'packeta' );
71 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
72 $this->method_description = __( 'Allows to choose one of Packeta delivery methods', 'packeta' );
73
74 $carrier = $this->carrierRepository->getAnyById( static::CARRIER_ID, true );
75 if ( $carrier !== null ) {
76 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
77 $this->method_title = $carrier->getName();
78 $this->title = $carrier->getName();
79 // translators: %s is carrier name.
80 $this->method_description = sprintf( __( 'Allows customers to use %s carrier delivery', 'packeta' ), $carrier->getName() ); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
81 }
82
83 $this->enabled = 'yes'; // This can be added as a setting.
84 $this->supports = [
85 'shipping-zones',
86 'instance-settings',
87 'instance-settings-modal',
88 ];
89 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
90 $this->tax_status = 'taxable';
91
92 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
93 if ( $this->instance_id !== 0 ) {
94 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
95 $this->options = $this->wpAdapter->getOption( sprintf( 'woocommerce_%s_%s_settings', $this->id, $this->instance_id ) );
96 }
97
98 $this->init();
99 $this->shippingRateFactory = $this->container->getByType( ShippingRateFactory::class );
100 }
101
102 /**
103 * Init user set variables. Derived from WC_Shipping_Flat_Rate.
104 */
105 public function init(): void {
106 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
107 $this->instance_form_fields = $this->get_instance_form_fields();
108 $this->title = $this->get_option( 'title' );
109 }
110
111 public function get_admin_options_html(): string {
112 $settingsHtml = $this->generate_settings_html( $this->get_instance_form_fields(), false );
113
114 return '<table class="form-table">' . $settingsHtml . "</table>\n";
115 }
116
117 /**
118 * Function to calculate shipping fee.
119 * Triggered by cart contents change, country change.
120 *
121 * @param array $package Order information.
122 *
123 * @return void
124 */
125 public function calculate_shipping( $package = [] ): void {
126 $allowedCarrierNames = [];
127 $zone = $this->wcAdapter->shippingZonesGetZoneMatchingPackage( $package );
128 $shippingMethods = $zone->get_shipping_methods( true );
129 if ( is_array( $shippingMethods ) && count( $shippingMethods ) > 0 ) {
130 foreach ( $shippingMethods as $shippingMethod ) {
131 if ( $shippingMethod instanceof self ) {
132 $allowedCarrierNames[ $shippingMethod::CARRIER_ID ] = $shippingMethod->options['title'] ?? $shippingMethod->get_title();
133 }
134 }
135 }
136
137 $customRates = $this->shippingRateFactory->createShippingRates(
138 $allowedCarrierNames,
139 $this->id,
140 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
141 $this->instance_id
142 );
143 foreach ( $customRates as $customRate ) {
144 $this->add_rate( $customRate );
145 }
146 }
147
148 /**
149 * Derived from settings-flat-rate.php.
150 *
151 * @return array
152 */
153 public function get_instance_form_fields(): array {
154 $latteEngine = $this->container->getByType( Engine::class );
155
156 $carrierSettingsLinkBase = $this->wpAdapter->addQueryArg(
157 [
158 'page' => Carrier\OptionsPage::SLUG,
159 Carrier\OptionsPage::PARAMETER_CARRIER_ID => static::CARRIER_ID,
160 ],
161 $this->wpAdapter->getAdminUrl( null, 'admin.php' )
162 );
163
164 $latteParams = [
165 'carrierSettingsLink' => $carrierSettingsLinkBase,
166 'translations' => [
167 'carrierSettingsLinkText' => __( 'Configure selected carrier', 'packeta' ),
168 ],
169 ];
170
171 $settings = [
172 'title' => [
173 'title' => __( 'Method title', 'packeta' ),
174 'type' => 'text',
175 'description' => __( 'This controls the title which the user sees during checkout.', 'packeta' ),
176 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
177 'default' => $this->method_title,
178 'desc_tip' => true,
179 ],
180 'tax_status' => [
181 'type' => 'hidden',
182 'default' => 'taxable',
183 ],
184 'custom_html' => [
185 'title' => '',
186 'type' => 'title',
187 'default' => '',
188 'description' => $latteEngine->renderToString(
189 PACKETERY_PLUGIN_DIR . '/template/carrier/carrier-modal-fragment.latte',
190 $latteParams
191 ),
192 ],
193 ];
194
195 return $settings;
196 }
197
198 /**
199 * Returns WooCommerce identificator of carrier shipping method.
200 *
201 * @return string
202 */
203 public static function getShippingMethodId(): string {
204 return self::PACKETA_METHOD_PREFIX . static::CARRIER_ID;
205 }
206 }
207