mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Axo: Add support for shipping locations limiting
This commit is contained in:
parent
53af77897f
commit
407f526064
7 changed files with 166 additions and 63 deletions
|
@ -0,0 +1,21 @@
|
|||
import { useMemo } from '@wordpress/element';
|
||||
|
||||
/**
|
||||
* Custom hook returning the allowed shipping locations based on configuration.
|
||||
*
|
||||
* @param {Object} axoConfig - The AXO configuration object.
|
||||
* @param {Array|undefined} axoConfig.enabled_shipping_locations - The list of enabled shipping locations.
|
||||
* @return {Array} The final list of allowed shipping locations.
|
||||
*/
|
||||
const useAllowedLocations = ( axoConfig ) => {
|
||||
return useMemo( () => {
|
||||
const enabledShippingLocations =
|
||||
axoConfig.enabled_shipping_locations || [];
|
||||
|
||||
return Array.isArray( enabledShippingLocations )
|
||||
? enabledShippingLocations
|
||||
: [];
|
||||
}, [ axoConfig.enabled_shipping_locations ] );
|
||||
};
|
||||
|
||||
export default useAllowedLocations;
|
|
@ -3,6 +3,7 @@ import { useSelect } from '@wordpress/data';
|
|||
import Fastlane from '../../../../ppcp-axo/resources/js/Connection/Fastlane';
|
||||
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
||||
import { useDeleteEmptyKeys } from './useDeleteEmptyKeys';
|
||||
import useAllowedLocations from './useAllowedLocations';
|
||||
import { STORE_NAME } from '../stores/axoStore';
|
||||
|
||||
/**
|
||||
|
@ -30,6 +31,8 @@ const useFastlaneSdk = ( namespace, axoConfig, ppcpConfig ) => {
|
|||
return deleteEmptyKeys( configRef.current.axoConfig.style_options );
|
||||
}, [ deleteEmptyKeys ] );
|
||||
|
||||
const allowedLocations = useAllowedLocations( axoConfig );
|
||||
|
||||
// Effect to initialize Fastlane SDK
|
||||
useEffect( () => {
|
||||
const initFastlane = async () => {
|
||||
|
@ -52,6 +55,9 @@ const useFastlaneSdk = ( namespace, axoConfig, ppcpConfig ) => {
|
|||
await fastlane.connect( {
|
||||
locale: configRef.current.ppcpConfig.locale,
|
||||
styles: styleOptions,
|
||||
shippingAddressOptions: {
|
||||
allowedLocations,
|
||||
},
|
||||
} );
|
||||
|
||||
// Set locale (hardcoded to 'en_us' for now)
|
||||
|
@ -66,7 +72,13 @@ const useFastlaneSdk = ( namespace, axoConfig, ppcpConfig ) => {
|
|||
};
|
||||
|
||||
initFastlane();
|
||||
}, [ fastlaneSdk, styleOptions, isPayPalLoaded, namespace ] );
|
||||
}, [
|
||||
fastlaneSdk,
|
||||
styleOptions,
|
||||
isPayPalLoaded,
|
||||
namespace,
|
||||
allowedLocations,
|
||||
] );
|
||||
|
||||
// Effect to update the config ref when configs change
|
||||
useEffect( () => {
|
||||
|
|
|
@ -37,7 +37,8 @@ return array(
|
|||
$container->get( 'wcgateway.settings' ),
|
||||
$container->get( 'wcgateway.configuration.dcc' ),
|
||||
$container->get( 'onboarding.environment' ),
|
||||
$container->get( 'wcgateway.url' )
|
||||
$container->get( 'wcgateway.url' ),
|
||||
$container->get( 'axo.shipping-wc-enabled-locations' )
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
@ -79,6 +79,13 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
*/
|
||||
private $wcgateway_module_url;
|
||||
|
||||
/**
|
||||
* The list of WooCommerce enabled shipping locations.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private array $enabled_shipping_locations;
|
||||
|
||||
/**
|
||||
* AdvancedCardPaymentMethod constructor.
|
||||
*
|
||||
|
@ -91,6 +98,7 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
* @param DCCGatewayConfiguration $dcc_configuration The DCC gateway settings.
|
||||
* @param Environment $environment The environment object.
|
||||
* @param string $wcgateway_module_url The WcGateway module URL.
|
||||
* @param array $enabled_shipping_locations The list of WooCommerce enabled shipping locations.
|
||||
*/
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
|
@ -100,7 +108,8 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
Settings $settings,
|
||||
DCCGatewayConfiguration $dcc_configuration,
|
||||
Environment $environment,
|
||||
string $wcgateway_module_url
|
||||
string $wcgateway_module_url,
|
||||
array $enabled_shipping_locations
|
||||
) {
|
||||
$this->name = AxoGateway::ID;
|
||||
$this->module_url = $module_url;
|
||||
|
@ -111,7 +120,7 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
$this->dcc_configuration = $dcc_configuration;
|
||||
$this->environment = $environment;
|
||||
$this->wcgateway_module_url = $wcgateway_module_url;
|
||||
|
||||
$this->enabled_shipping_locations = $enabled_shipping_locations;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -207,6 +216,7 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
: null, // Set to null if WC()->cart is null or get_total doesn't exist.
|
||||
),
|
||||
),
|
||||
'enabled_shipping_locations' => $this->enabled_shipping_locations,
|
||||
'style_options' => array(
|
||||
'root' => array(
|
||||
'backgroundColor' => $this->settings->has( 'axo_style_root_bg_color' ) ? $this->settings->get( 'axo_style_root_bg_color' ) : '',
|
||||
|
|
|
@ -85,6 +85,9 @@ class AxoManager {
|
|||
},
|
||||
};
|
||||
|
||||
this.enabledShippingLocations =
|
||||
this.axoConfig.enabled_shipping_locations;
|
||||
|
||||
this.registerEventHandlers();
|
||||
|
||||
this.shippingView = new ShippingView(
|
||||
|
@ -661,6 +664,9 @@ class AxoManager {
|
|||
await this.fastlane.connect( {
|
||||
locale: this.locale,
|
||||
styles: this.styles,
|
||||
shippingAddressOptions: {
|
||||
allowedLocations: this.enabledShippingLocations,
|
||||
},
|
||||
} );
|
||||
|
||||
this.fastlane.setLocale( 'en_us' );
|
||||
|
|
|
@ -67,7 +67,8 @@ return array(
|
|||
$container->get( 'wcgateway.settings.status' ),
|
||||
$container->get( 'api.shop.currency.getter' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'wcgateway.url' )
|
||||
$container->get( 'wcgateway.url' ),
|
||||
$container->get( 'axo.shipping-wc-enabled-locations' )
|
||||
);
|
||||
},
|
||||
|
||||
|
@ -251,4 +252,45 @@ return array(
|
|||
$active_plugins_list
|
||||
);
|
||||
},
|
||||
|
||||
'axo.shipping-wc-enabled-locations' => static function ( ContainerInterface $container ): array {
|
||||
$default_zone = new \WC_Shipping_Zone( 0 );
|
||||
|
||||
$is_method_enabled = fn( $method) => $method->enabled === 'yes';
|
||||
|
||||
$is_default_zone_enabled = ! empty(
|
||||
array_filter(
|
||||
$default_zone->get_shipping_methods(),
|
||||
$is_method_enabled
|
||||
)
|
||||
);
|
||||
|
||||
if ( $is_default_zone_enabled ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$shipping_zones = \WC_Shipping_Zones::get_zones();
|
||||
|
||||
$get_zone_locations = fn($zone) =>
|
||||
!empty(array_filter($zone->get_shipping_methods(), $is_method_enabled))
|
||||
? array_map(
|
||||
fn($location) => $location->code,
|
||||
$zone->get_zone_locations()
|
||||
)
|
||||
: [];
|
||||
|
||||
$enabled_locations = array_unique(
|
||||
array_merge(
|
||||
...array_map(
|
||||
$get_zone_locations,
|
||||
array_map(
|
||||
fn( $zone) => $zone instanceof \WC_Shipping_Zone ? $zone : new \WC_Shipping_Zone( $zone['id'] ),
|
||||
$shipping_zones
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return $enabled_locations;
|
||||
},
|
||||
);
|
||||
|
|
|
@ -87,6 +87,13 @@ class AxoManager {
|
|||
*/
|
||||
private $wcgateway_module_url;
|
||||
|
||||
/**
|
||||
* The list of WooCommerce enabled shipping locations.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private array $enabled_shipping_locations;
|
||||
|
||||
/**
|
||||
* AxoManager constructor.
|
||||
*
|
||||
|
@ -99,6 +106,7 @@ class AxoManager {
|
|||
* @param CurrencyGetter $currency The getter of the 3-letter currency code of the shop.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $wcgateway_module_url The WcGateway module URL.
|
||||
* @param array $enabled_shipping_locations The list of WooCommerce enabled shipping locations.
|
||||
*/
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
|
@ -109,7 +117,8 @@ class AxoManager {
|
|||
SettingsStatus $settings_status,
|
||||
CurrencyGetter $currency,
|
||||
LoggerInterface $logger,
|
||||
string $wcgateway_module_url
|
||||
string $wcgateway_module_url,
|
||||
array $enabled_shipping_locations
|
||||
) {
|
||||
|
||||
$this->module_url = $module_url;
|
||||
|
@ -121,6 +130,7 @@ class AxoManager {
|
|||
$this->currency = $currency;
|
||||
$this->logger = $logger;
|
||||
$this->wcgateway_module_url = $wcgateway_module_url;
|
||||
$this->enabled_shipping_locations = $enabled_shipping_locations;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -183,6 +193,7 @@ class AxoManager {
|
|||
'value' => WC()->cart->get_total( 'numeric' ),
|
||||
),
|
||||
),
|
||||
'enabled_shipping_locations' => $this->enabled_shipping_locations,
|
||||
'style_options' => array(
|
||||
'root' => array(
|
||||
'backgroundColor' => $this->settings->has( 'axo_style_root_bg_color' ) ? $this->settings->get( 'axo_style_root_bg_color' ) : '',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue