Axo: Add support for shipping locations limiting

This commit is contained in:
Daniel Dudzic 2024-10-17 23:56:03 +02:00
parent 53af77897f
commit 407f526064
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
7 changed files with 166 additions and 63 deletions

View file

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

View file

@ -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( () => {

View file

@ -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' )
);
},
);

View file

@ -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,18 +108,19 @@ 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;
$this->version = $version;
$this->gateway = $gateway;
$this->smart_button = $smart_button;
$this->settings = $settings;
$this->dcc_configuration = $dcc_configuration;
$this->environment = $environment;
$this->wcgateway_module_url = $wcgateway_module_url;
$this->name = AxoGateway::ID;
$this->module_url = $module_url;
$this->version = $version;
$this->gateway = $gateway;
$this->smart_button = $smart_button;
$this->settings = $settings;
$this->dcc_configuration = $dcc_configuration;
$this->environment = $environment;
$this->wcgateway_module_url = $wcgateway_module_url;
$this->enabled_shipping_locations = $enabled_shipping_locations;
}
/**
@ -187,13 +196,13 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
}
return array(
'environment' => array(
'environment' => array(
'is_sandbox' => $this->environment->current_environment() === 'sandbox',
),
'widgets' => array(
'widgets' => array(
'email' => 'render',
),
'insights' => array(
'insights' => array(
'enabled' => defined( 'WP_DEBUG' ) && WP_DEBUG,
'client_id' => ( $this->settings->has( 'client_id' ) ? $this->settings->get( 'client_id' ) : null ),
'session_id' =>
@ -207,7 +216,8 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
: null, // Set to null if WC()->cart is null or get_total doesn't exist.
),
),
'style_options' => array(
'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' ) : '',
'errorColor' => $this->settings->has( 'axo_style_root_error_color' ) ? $this->settings->get( 'axo_style_root_error_color' ) : '',
@ -226,23 +236,23 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
'focusBorderColor' => $this->settings->has( 'axo_style_input_focus_border_color' ) ? $this->settings->get( 'axo_style_input_focus_border_color' ) : '',
),
),
'name_on_card' => $this->dcc_configuration->show_name_on_card(),
'woocommerce' => array(
'name_on_card' => $this->dcc_configuration->show_name_on_card(),
'woocommerce' => array(
'states' => array(
'US' => WC()->countries->get_states( 'US' ),
'CA' => WC()->countries->get_states( 'CA' ),
),
),
'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/',
'module_url' => untrailingslashit( $this->module_url ),
'ajax' => array(
'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/',
'module_url' => untrailingslashit( $this->module_url ),
'ajax' => array(
'frontend_logger' => array(
'endpoint' => \WC_AJAX::get_endpoint( FrontendLoggerEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( FrontendLoggerEndpoint::nonce() ),
),
),
'logging_enabled' => $this->settings->has( 'logging_enabled' ) ? $this->settings->get( 'logging_enabled' ) : '',
'wp_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
'logging_enabled' => $this->settings->has( 'logging_enabled' ) ? $this->settings->get( 'logging_enabled' ) : '',
'wp_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
);
}
}