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 / ShippingMethod.php

ShippingMethod.php in Packeta trunk, at src/Packetery/Module/ShippingMethod.php

87 lines 2.1 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;
6
7 use Packetery\Module\Checkout\ShippingRateFactory;
8 use Packetery\Module\Exception\ProductNotFoundException;
9 use WC_Shipping_Method;
10
11 class ShippingMethod extends WC_Shipping_Method {
12
13 public const PACKETERY_METHOD_ID = 'packetery_shipping_method';
14
15 /**
16 * @var ShippingRateFactory
17 */
18 private $shippingRateFactory;
19
20 public function __construct( int $instanceId = 0 ) {
21 parent::__construct();
22
23 $this->id = self::PACKETERY_METHOD_ID;
24 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
25 $this->instance_id = absint( $instanceId );
26 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
27 $this->method_title = __( 'Packeta', 'packeta' );
28 $this->title = __( 'Packeta', 'packeta' );
29 $this->enabled = 'yes'; // This can be added as a setting.
30 $this->supports = [
31 'shipping-zones',
32 ];
33 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
34 $this->tax_status = 'taxable';
35
36 $this->init();
37
38 $container = CompatibilityBridge::getContainer();
39 $this->shippingRateFactory = $container->getByType( ShippingRateFactory::class );
40 }
41
42 /**
43 * Init user set variables. Derived from WC_Shipping_Flat_Rate.
44 */
45 public function init(): void {
46 add_action(
47 'woocommerce_update_options_shipping_' . $this->id,
48 function () {
49 $this->process_admin_options();
50 }
51 );
52 }
53
54 public function get_admin_options_html(): string {
55 return '';
56 }
57
58 /**
59 * @param array<string|int, mixed> $package
60 *
61 * @return void
62 * @throws ProductNotFoundException
63 */
64 public function calculate_shipping( $package = [] ): void {
65 $allowedCarrierNames = null;
66
67 $customRates = $this->shippingRateFactory->createShippingRates(
68 $allowedCarrierNames,
69 $this->id,
70 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
71 $this->instance_id
72 );
73 foreach ( $customRates as $customRate ) {
74 $this->add_rate( $customRate );
75 }
76 }
77
78 /**
79 * Derived from settings-flat-rate.php.
80 *
81 * @return array<string|int, mixed>
82 */
83 public function get_instance_form_fields(): array {
84 return [];
85 }
86 }
87