| 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 |
|