| @@ -1,81 +1,86 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Packeta shipping method class. | |
| 4 | - * | |
| 5 | - * @package Packetery | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module; |
| 11 | 6 | |
| 12 | -/** | |
| 13 | - * Packeta shipping method class. | |
| 14 | - */ | |
| 15 | -class ShippingMethod extends \WC_Shipping_Method { | |
| 7 | +use Packetery\Module\Checkout\ShippingRateFactory; | |
| 8 | +use Packetery\Module\Exception\ProductNotFoundException; | |
| 9 | +use WC_Shipping_Method; | |
| 16 | 10 | |
| 11 | +class ShippingMethod extends WC_Shipping_Method { | |
| 12 | + | |
| 17 | 13 | public const PACKETERY_METHOD_ID = 'packetery_shipping_method'; |
| 18 | 14 | |
| 19 | 15 | /** |
| 20 | - * Checkout object. | |
| 21 | - * | |
| 22 | - * @var Checkout | |
| 16 | + * @var ShippingRateFactory | |
| 23 | 17 | */ |
| 24 | - private $checkout; | |
| 18 | + private $shippingRateFactory; | |
| 25 | 19 | |
| 26 | - /** | |
| 27 | - * Constructor for Packeta shipping class | |
| 28 | - * | |
| 29 | - * @param int $instance_id Shipping method instance id. | |
| 30 | - */ | |
| 31 | - public function __construct( int $instance_id = 0 ) { | |
| 20 | + public function __construct( int $instanceId = 0 ) { | |
| 32 | 21 | parent::__construct(); |
| 33 | - $this->id = self::PACKETERY_METHOD_ID; | |
| 34 | - $this->instance_id = absint( $instance_id ); | |
| 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 | |
| 35 | 27 | $this->method_title = __( 'Packeta', 'packeta' ); |
| 36 | 28 | $this->title = __( 'Packeta', 'packeta' ); |
| 37 | 29 | $this->enabled = 'yes'; // This can be added as a setting. |
| 38 | - $this->supports = array( | |
| 30 | + $this->supports = [ | |
| 39 | 31 | 'shipping-zones', |
| 40 | - ); | |
| 32 | + ]; | |
| 33 | + // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps | |
| 34 | + $this->tax_status = 'taxable'; | |
| 35 | + | |
| 41 | 36 | $this->init(); |
| 42 | 37 | |
| 43 | - $container = CompatibilityBridge::getContainer(); | |
| 44 | - $this->checkout = $container->getByType( Checkout::class ); | |
| 38 | + $container = CompatibilityBridge::getContainer(); | |
| 39 | + $this->shippingRateFactory = $container->getByType( ShippingRateFactory::class ); | |
| 45 | 40 | } |
| 46 | 41 | |
| 47 | 42 | /** |
| 48 | - * Init settings. | |
| 49 | - * | |
| 50 | - * @return void | |
| 43 | + * Init user set variables. Derived from WC_Shipping_Flat_Rate. | |
| 51 | 44 | */ |
| 52 | 45 | public function init(): void { |
| 53 | - // todo Load the settings API | |
| 54 | - // $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings | |
| 55 | - // $this->init_settings(); // This is part of the settings API. Loads settings you previously init. | |
| 56 | - | |
| 57 | - // Save settings in admin if you have any defined. | |
| 58 | - \add_action( | |
| 46 | + add_action( | |
| 59 | 47 | 'woocommerce_update_options_shipping_' . $this->id, |
| 60 | - array( | |
| 61 | - $this, | |
| 62 | - 'process_admin_options', | |
| 63 | - ) | |
| 48 | + function () { | |
| 49 | + $this->process_admin_options(); | |
| 50 | + } | |
| 64 | 51 | ); |
| 65 | 52 | } |
| 66 | 53 | |
| 54 | + public function get_admin_options_html(): string { | |
| 55 | + return ''; | |
| 56 | + } | |
| 57 | + | |
| 67 | 58 | /** |
| 68 | - * Function to calculate shipping fee. | |
| 69 | - * Triggered by cart contents change, country change. | |
| 59 | + * @param array<string|int, mixed> $package | |
| 70 | 60 | * |
| 71 | - * @param array $package Order information. | |
| 72 | - * | |
| 73 | 61 | * @return void |
| 62 | + * @throws ProductNotFoundException | |
| 74 | 63 | */ |
| 75 | 64 | public function calculate_shipping( $package = [] ): void { |
| 76 | - $customRates = $this->checkout->getShippingRates(); | |
| 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 | + ); | |
| 77 | 73 | foreach ( $customRates as $customRate ) { |
| 78 | 74 | $this->add_rate( $customRate ); |
| 79 | 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 []; | |
| 80 | 85 | } |
| 81 | 86 | } |