Skip to content

Commit

Permalink
Merge pull request #2107 from woocommerce/PCP-2811-pay-later-messagin…
Browse files Browse the repository at this point in the history
…g-configurator-improvements

Pay later messaging configurator improvements (2811)
  • Loading branch information
Dinamiko authored Apr 3, 2024
2 parents bb8734d + b4e1ce3 commit d011689
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 36 deletions.
7 changes: 3 additions & 4 deletions modules/ppcp-paylater-block/resources/js/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,11 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
help={ __( 'Used for the analytics dashboard in the merchant account.', 'woocommerce-paypal-payments' ) }
options={ [
{ label: __( 'Detect automatically', 'woocommerce-paypal-payments' ), value: 'auto' },
{ label: __( 'Product Page', 'woocommerce-paypal-payments' ), value: 'product' },
{ label: __( 'Cart', 'woocommerce-paypal-payments' ), value: 'cart' },
{ label: __( 'Payment', 'woocommerce-paypal-payments' ), value: 'payment' },
{ label: __( 'Product', 'woocommerce-paypal-payments' ), value: 'product' },
{ label: __( 'Product list', 'woocommerce-paypal-payments' ), value: 'product-list' },
{ label: __( 'Checkout', 'woocommerce-paypal-payments' ), value: 'checkout' },
{ label: __( 'Home', 'woocommerce-paypal-payments' ), value: 'home' },
{ label: __( 'Category', 'woocommerce-paypal-payments' ), value: 'category' },
{ label: __( 'Shop', 'woocommerce-paypal-payments' ), value: 'shop' },
] }
value={ placement }
onChange={ ( value ) => setAttributes( { placement: value } ) }
Expand Down
2 changes: 1 addition & 1 deletion modules/ppcp-paylater-block/src/PayLaterBlockModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function is_module_loading_required(): bool {
* @return bool true if the block is enabled, otherwise false.
*/
public static function is_block_enabled( SettingsStatus $settings_status ): bool {
return self::is_module_loading_required() && $settings_status->is_pay_later_messaging_enabled_for_location( 'woocommerceBlock' );
return self::is_module_loading_required() && $settings_status->is_pay_later_messaging_enabled_for_location( 'custom_placement' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ document.addEventListener( 'DOMContentLoaded', () => {
setTimeout(() => {
saveChangesButton.click(); // Trigger click event on saveChangesButton
isSaving = false; // Reset flag when saving is complete
}, 500); // Adjust the delay as needed
}, 1000); // Adjust the delay as needed
}
});


merchantConfigurators.Messaging({
config: PcpPayLaterConfigurator.config,
merchantClientId: PcpPayLaterConfigurator.merchantClientId,
partnerClientId: PcpPayLaterConfigurator.partnerClientId,
partnerName: 'WooCommerce',
bnCode: 'Woo_PPCP',
placements: ['cart', 'checkout', 'product', 'shop', 'home'],
custom_placement:[{
message_reference: 'woocommerceBlock',
}],
placements: ['cart', 'checkout', 'product', 'shop', 'home', 'custom_placement'],
styleOverrides: {
button: publishButtonClassName,
header: PcpPayLaterConfigurator.headerClassName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ private function save_config( array $config ): void {
$this->settings->set( 'pay_later_messaging_enabled', true );

$enabled_locations = array();

foreach ( $config as $placement => $data ) {
$this->save_config_for_location( $data, $placement );

if ( $placement === 'custom_placement' ) {
$data = $data[0] ?? array();
}

if ( $data['status'] === 'enabled' ) {
$enabled_locations[] = $placement;
}
Expand All @@ -129,6 +132,7 @@ private function save_config_for_location( array $config, string $location ): vo
$this->set_value_if_present( $config, 'logo-type', "pay_later_{$location}_message_logo" );
$this->set_value_if_present( $config, 'logo-color', "pay_later_{$location}_message_color" );
$this->set_value_if_present( $config, 'text-size', "pay_later_{$location}_message_text_size" );
$this->set_value_if_present( $config, 'text-color', "pay_later_{$location}_message_color" );
}

/**
Expand Down
106 changes: 82 additions & 24 deletions modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function from_settings( Settings $settings ): array {
'product' => $this->for_location( $settings, 'product' ),
'shop' => $this->for_location( $settings, 'shop' ),
'home' => $this->for_location( $settings, 'home' ),
'woocommerceBlock' => $this->for_location( $settings, 'woocommerceBlock' ),
'custom_placement' => array( $this->for_location( $settings, 'woocommerceBlock' ) ),
);
}

Expand All @@ -40,29 +40,87 @@ public function from_settings( Settings $settings ): array {
private function for_location( Settings $settings, string $location ): array {
$selected_locations = $settings->has( 'pay_later_messaging_locations' ) ? $settings->get( 'pay_later_messaging_locations' ) : array();

if ( in_array( $location, array( 'shop', 'home' ), true ) ) {
$config = array(
'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'flex' ),
'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black' ),
'ratio' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_ratio", '8x1' ),
);
} elseif ( $location !== 'woocommerceBlock' ) {
$config = array(
'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'text' ),
'logo-position' => $this->get_or_default( $settings, "pay_later_{$location}_message_position", 'left' ),
'logo-type' => $this->get_or_default( $settings, "pay_later_{$location}_message_logo", 'inline' ),
'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_color", 'black' ),
'text-size' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_size", '12' ),

);
switch ( $location ) {
case 'shop':
case 'home':
$config = $this->for_shop_or_home( $settings, $location, $selected_locations );
break;
case 'woocommerceBlock':
$config = $this->for_woocommerce_block( $selected_locations );
break;
default:
$config = $this->for_default_location( $settings, $location, $selected_locations );
break;
}

return array_merge(
array(
'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled',
'placement' => $location,
),
$config ?? array()
return $config;
}

/**
* Returns the configurator config for shop, home locations.
*
* @param Settings $settings The settings.
* @param string $location The location.
* @param string[] $selected_locations The list of selected locations.
* @return array{
* layout: string,
* color: string,
* ratio: string,
* status: "disabled"|"enabled",
* placement: string
* } The configurator config map.
*/
private function for_shop_or_home( Settings $settings, string $location, array $selected_locations ): array {
return array(
'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'flex' ),
'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black' ),
'ratio' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_ratio", '8x1' ),
'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled',
'placement' => $location,
);
}

/**
* Returns the configurator config for woocommerceBlock location.
*
* @param array $selected_locations The list of selected locations.
* @return array{
* status: "disabled"|"enabled",
* message_reference: string
* } The configurator config map.
*/
private function for_woocommerce_block( array $selected_locations ): array {
return array(
'status' => in_array( 'custom_placement', $selected_locations, true ) ? 'enabled' : 'disabled',
'message_reference' => 'woocommerceBlock',
);
}

/**
* Returns the configurator config for default locations.
*
* @param Settings $settings The settings.
* @param string $location The location.
* @param string[] $selected_locations The list of selected locations.
* @return array{
* layout: string,
* logo-position: string,
* logo-type: string,
* text-color: string,
* text-size: string,
* status: "disabled"|"enabled",
* placement: string
* } The configurator config map.
*/
private function for_default_location( Settings $settings, string $location, array $selected_locations ): array {
return array(
'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'text' ),
'logo-position' => $this->get_or_default( $settings, "pay_later_{$location}_message_position", 'left' ),
'logo-type' => $this->get_or_default( $settings, "pay_later_{$location}_message_logo", 'inline' ),
'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_color", 'black' ),
'text-size' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_size", '12' ),
'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled',
'placement' => $location,
);
}

Expand All @@ -73,9 +131,9 @@ private function for_location( Settings $settings, string $location ): array {
* @param string $key The key.
* @param mixed $default The default value.
* @param array|null $allowed_values The list of allowed values, or null if all values are allowed.
* @return mixed
* @return string
*/
private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ) {
private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ): string {
if ( $settings->has( $key ) ) {
$value = $settings->get( $key );
if ( ! $allowed_values || in_array( $value, $allowed_values, true ) ) {
Expand Down

0 comments on commit d011689

Please sign in to comment.