add a filter to load the service later and only if it is needed.

This commit is contained in:
Daniel Hüsken 2024-11-27 12:18:28 +01:00
parent d6c5d78482
commit d61561f913
No known key found for this signature in database
GPG key ID: 9F732DA37FA709E8
5 changed files with 20 additions and 43 deletions

View file

@ -70,8 +70,7 @@ return array(
$container->get( 'api.shop.currency.getter' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'wcgateway.url' ),
$container->get( 'axo.supported-country-card-type-matrix' ),
$container->get( 'axo.shipping-wc-enabled-locations' )
$container->get( 'axo.supported-country-card-type-matrix' )
);
},
@ -329,33 +328,23 @@ return array(
);
},
'axo.shipping-wc-enabled-locations' => static function ( ContainerInterface $container ): array {
'axo.shipping-wc-enabled-locations' => static function ( ContainerInterface $container ) {
$default_zone = new \WC_Shipping_Zone( 0 );
$is_method_enabled = fn( \WC_Shipping_Method $method): bool => $method->enabled === 'yes';
$is_default_zone_enabled = ! empty(
array_filter(
$default_zone->get_shipping_methods(),
$is_method_enabled
)
);
if ( $is_default_zone_enabled ) {
if ( ! empty( $default_zone->get_shipping_methods( true ) ) ) {
return array();
}
$shipping_zones = \WC_Shipping_Zones::get_zones();
$get_zone_locations = fn( \WC_Shipping_Zone $zone): array =>
! empty( array_filter( $zone->get_shipping_methods(), $is_method_enabled ) )
! empty( $zone->get_shipping_methods( true ) )
? array_map(
fn( object $location): string => $location->code,
$zone->get_zone_locations()
)
: array();
$enabled_locations = array_unique(
return array_unique(
array_merge(
...array_map(
$get_zone_locations,
@ -367,7 +356,5 @@ return array(
)
)
);
return $enabled_locations;
},
);

View file

@ -99,12 +99,6 @@ class AxoManager {
* @var array
*/
private array $supported_country_card_type_matrix;
/**
* The list of WooCommerce enabled shipping locations.
*
* @var array
*/
private array $enabled_shipping_locations;
/**
* AxoManager constructor.
@ -120,7 +114,6 @@ class AxoManager {
* @param LoggerInterface $logger The logger.
* @param string $wcgateway_module_url The WcGateway module URL.
* @param array $supported_country_card_type_matrix The supported country card type matrix for Axo.
* @param array $enabled_shipping_locations The list of WooCommerce enabled shipping locations.
*/
public function __construct(
string $module_url,
@ -133,8 +126,7 @@ class AxoManager {
CurrencyGetter $currency,
LoggerInterface $logger,
string $wcgateway_module_url,
array $supported_country_card_type_matrix,
array $enabled_shipping_locations
array $supported_country_card_type_matrix
) {
$this->module_url = $module_url;
@ -147,7 +139,6 @@ class AxoManager {
$this->currency = $currency;
$this->logger = $logger;
$this->wcgateway_module_url = $wcgateway_module_url;
$this->enabled_shipping_locations = $enabled_shipping_locations;
$this->supported_country_card_type_matrix = $supported_country_card_type_matrix;
}
@ -203,7 +194,7 @@ class AxoManager {
return $data; } )( $this->insights_data ),
'allowed_cards' => $this->supported_country_card_type_matrix,
'disable_cards' => $this->settings->has( 'disable_cards' ) ? (array) $this->settings->get( 'disable_cards' ) : array(),
'enabled_shipping_locations' => $this->enabled_shipping_locations,
'enabled_shipping_locations' => apply_filters( 'woocommerce_paypal_payments_axo_shipping_wc_enabled_locations', array() ),
'style_options' => array(
'root' => array(
'backgroundColor' => $this->settings->has( 'axo_style_root_bg_color' ) ? $this->settings->get( 'axo_style_root_bg_color' ) : '',

View file

@ -229,6 +229,16 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
}
);
/**
* Late loading locations because of trouble with some shipping plugins
*/
add_filter(
'woocommerce_paypal_payments_axo_shipping_wc_enabled_locations',
function ( array $locations ) use ( $c ): array {
return array_merge( $locations, $c->get( 'axo.shipping-wc-enabled-locations' ) );
}
);
/**
* Param types removed to avoid third-party issues.
*