Do not remove mini-cart location for pay later

This commit is contained in:
Alex P 2023-03-01 16:17:36 +02:00
parent 8c1af84204
commit 2adc0d57e1
No known key found for this signature in database
GPG key ID: 54487A734A204D71
4 changed files with 105 additions and 17 deletions

View file

@ -1338,24 +1338,10 @@ return array(
assert( $settings instanceof Settings ); assert( $settings instanceof Settings );
$button_locations = $container->get( 'wcgateway.button.locations' ); $button_locations = $container->get( 'wcgateway.button.locations' );
unset( $button_locations['mini-cart'] );
$smart_button_selected_locations = $settings->has( 'smart_button_locations' ) ? $settings->get( 'smart_button_locations' ) : array(); $smart_button_selected_locations = $settings->has( 'smart_button_locations' ) ? $settings->get( 'smart_button_locations' ) : array();
$pay_later_button_locations = array();
if ( empty( $smart_button_selected_locations ) ) { return array_intersect_key( $button_locations, array_flip( $smart_button_selected_locations ) );
return $pay_later_button_locations;
}
foreach ( $button_locations as $location_key => $location ) {
if ( ! in_array( $location_key, $smart_button_selected_locations, true ) ) {
continue;
}
$pay_later_button_locations[ $location_key ] = $location;
}
return $pay_later_button_locations;
}, },
'wcgateway.ppcp-gateways' => static function ( ContainerInterface $container ): array { 'wcgateway.ppcp-gateways' => static function ( ContainerInterface $container ): array {
return array( return array(

View file

@ -9,7 +9,6 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Helper; namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/** /**
@ -74,7 +73,9 @@ class SettingsStatus {
* @return bool true if is enabled, otherwise false. * @return bool true if is enabled, otherwise false.
*/ */
public function is_pay_later_button_enabled_for_location( string $location ): bool { public function is_pay_later_button_enabled_for_location( string $location ): bool {
return $this->is_pay_later_button_enabled() && $this->is_enabled_for_location( 'pay_later_button_locations', $location ); return $this->is_pay_later_button_enabled() &&
( $this->is_enabled_for_location( 'pay_later_button_locations', $location ) ||
( 'product' === $location && $this->is_enabled_for_location( 'pay_later_button_locations', 'mini-cart' ) ) );
} }
/** /**

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Helper;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
class SettingsStub extends Settings
{
/**
* @var array
*/
protected $data;
/**
* @param array $data
*/
public function __construct(array $data) {
$this->data = $data;
}
public function get($id) {
if ( ! $this->has( $id ) ) {
throw new NotFoundException();
}
return $this->data[$id];
}
public function has($id) {
return array_key_exists( $id, $this->data );
}
public function set($id, $value) {
$this->data[$id] = $value;
}
public function persist() {
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use WooCommerce\PayPalCommerce\Helper\SettingsStub;
use WooCommerce\PayPalCommerce\ModularTestCase;
class LocationsTest extends ModularTestCase
{
private $appContainer;
private $settings;
public function setUp(): void {
parent::setUp();
$this->settings = new SettingsStub([]);
$this->appContainer = $this->bootstrapModule([
'wcgateway.settings' => function () {
return $this->settings;
},
]);
}
/**
* @dataProvider payLaterButtonLocationsData
*/
public function testPayLaterButtonLocations(array $selectedLocations, array $expectedResult) {
$this->settings->set('smart_button_locations', $selectedLocations);
$result = $this->appContainer->get('wcgateway.settings.pay-later.button-locations');
self::assertEquals($expectedResult, $result);
}
public function payLaterButtonLocationsData()
{
yield [
['product', 'cart', 'checkout', 'mini-cart'],
[
'product' => 'Single Product',
'cart' => 'Cart',
'checkout' => 'Checkout',
'mini-cart' => 'Mini Cart',
],
];
yield [
['cart', 'checkout'],
[
'cart' => 'Cart',
'checkout' => 'Checkout',
],
];
yield [
[],
[],
];
}
}