Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(settings): hide delivery options position setting when blocks checkout is enabled #1188

Merged
merged 11 commits into from
Nov 14, 2024
Merged
15 changes: 10 additions & 5 deletions src/Contract/WooCommerceServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

interface WooCommerceServiceInterface
{
/**
* Whether HPOS is enabled in WooCommerce.
*/
public function isUsingHpos(): bool;

/**
* @return string
*/
Expand All @@ -20,4 +15,14 @@ public function getVersion(): string;
* @return bool
*/
public function isActive(): bool;

/**
* @return bool
*/
public function isUsingBlocksCheckout(): bool;

/**
* Whether HPOS is enabled in WooCommerce.
*/
public function isUsingHpos(): bool;
}
13 changes: 12 additions & 1 deletion src/Pdk/WcPdkBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Settings\Model\CheckoutSettings;
use MyParcelNL\WooCommerce\Integration\DeliveryOptionsBlocksIntegration;
use MyParcelNL\WooCommerce\Service\WooCommerceService;
use function DI\factory;
use function DI\value;

Expand Down Expand Up @@ -194,13 +195,23 @@ protected function getAdditionalConfig(
* Settings defaults
*/

'defaultSettings' => value([
'defaultSettings' => value([
CheckoutSettings::ID => [
CheckoutSettings::ALLOWED_SHIPPING_METHODS => ['flat_rate:0', 'free_shipping:0'],
CheckoutSettings::DELIVERY_OPTIONS_POSITION => 'woocommerce_after_checkout_billing_form',
],
]),

'disabledSettings' => factory(function () {
$disabledSettings = [];
if (Pdk::get(WooCommerceService::class)
->isUsingBlocksCheckout()) {
$disabledSettings[CheckoutSettings::ID][] = CheckoutSettings::DELIVERY_OPTIONS_POSITION;
}

return $disabledSettings;
}),

/**
* @see https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Service/WooCommerceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Automattic\WooCommerce\Utilities\OrderUtil;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\WooCommerce\Contract\WooCommerceServiceInterface;
use WC_Blocks_Utils;

/**
* @see /config/pdk.php
Expand All @@ -21,6 +22,15 @@ public function getVersion(): string
return Pdk::get('wooCommerceVersion');
}

/**
* @return bool
* @see https://stackoverflow.com/a/77950175
*/
public function isUsingBlocksCheckout(): bool
{
return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout');
}

/**
* @return bool
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Mock/MockWcBlocksUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\WooCommerce\Tests\Mock;

use WP_Post;

class MockWcBlocksUtils extends MockWcClass
{
/**
* Check if a given page contains a particular block.
*
* @param int|WP_Post $page Page post ID or post object.
* @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`.
*
* @return bool Boolean value if the page contains the block or not. Null in case the page does not exist.
*/
public static function has_block_in_page($page, $block_name)

Check warning on line 19 in tests/Mock/MockWcBlocksUtils.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcBlocksUtils.php#L19

Avoid unused parameters such as '$block_name'.

Check notice on line 19 in tests/Mock/MockWcBlocksUtils.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcBlocksUtils.php#L19

The method has_block_in_page is not named in camelCase.

Check notice on line 19 in tests/Mock/MockWcBlocksUtils.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcBlocksUtils.php#L19

The parameter $block_name is not named in camelCase.
{
//todo: extend this function when we want to check for specific blocks in tests
return MockWpCache::get($page, 'pages')['hasBlocks'];
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Pdk/WcPdkBootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace MyParcelNL\WooCommerce\Pdk;

use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Settings\Model\CheckoutSettings;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpCache;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpUser;
use MyParcelNL\WooCommerce\Tests\Uses\UsesMockWcPdkInstance;
use function MyParcelNL\Pdk\Tests\usesShared;
Expand Down Expand Up @@ -65,3 +67,23 @@
'expected' => true,
],
]);

it('enables or disables delivery options position setting', function ($pageId, $group, $data, $result) {
MockWpCache::add($pageId, $data, $group);
$disabledSettings = Pdk::get('disabledSettings');

expect($disabledSettings)->toEqual($result);
})->with([
'Blocks checkout enabled' => [
'pageId' => '7',
'group' => 'pages',
'data' => ['pageName' => 'checkout', 'hasBlocks' => true],
'result' => [CheckoutSettings::ID => [CheckoutSettings::DELIVERY_OPTIONS_POSITION]],
],
'Classic checkout enabled' => [
'pageId' => '7',
'group' => 'pages',
'data' => ['pageName' => 'checkout', 'hasBlocks' => false],
'result' => [],
],
]);
6 changes: 5 additions & 1 deletion tests/mock_class_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
declare(strict_types=1);

use MyParcelNL\WooCommerce\Tests\Mock\MockWc;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcBlocksUtils;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcCart;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcCartRepository;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcClass;
Expand All @@ -17,10 +18,10 @@
use MyParcelNL\WooCommerce\Tests\Mock\MockWcShippingMethodFlatRateClass;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcShippingZone;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcShippingZonesClass;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpTerm;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpRestRequest;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpRestResponse;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpRestServer;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpTerm;

/** @see \MyParcelNL\WooCommerce\bootPdk() */
const PEST = true;
Expand Down Expand Up @@ -83,3 +84,6 @@

/** @see \WP_REST_Response */
class WP_REST_Response extends MockWpRestResponse { }

/** @see \WC_Blocks_Utils */
class WC_Blocks_Utils extends MockWcBlocksUtils { }

Check notice on line 89 in tests/mock_class_map.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/mock_class_map.php#L89

Only one class is allowed in a file

Check notice on line 89 in tests/mock_class_map.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/mock_class_map.php#L89

Only one object structure is allowed in a file

Check notice on line 89 in tests/mock_class_map.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/mock_class_map.php#L89

The class WC_Blocks_Utils is not named in CamelCase.
29 changes: 29 additions & 0 deletions tests/mock_wc_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use MyParcelNL\WooCommerce\Tests\Mock\MockWc;
use MyParcelNL\WooCommerce\Tests\Mock\MockWcData;
use MyParcelNL\WooCommerce\Tests\Mock\MockWpCache;

/** @see \get_woocommerce_currency() */
function get_woocommerce_currency(): string
Expand Down Expand Up @@ -84,3 +85,31 @@
{
return MockWc::getInstance();
}

/**
* @param string $page Page slug.
*
* @return int
* @see \wc_get_page_id()
* Retrieve page ids - used for myaccount, edit_address, shop, cart, checkout, pay, view_order, terms. returns -1
* if no page is found.
*/
function wc_get_page_id(string $page)
{
if ('pay' === $page || 'thanks' === $page) {
$page = 'checkout';
}
if ('change_password' === $page || 'edit_address' === $page || 'lost_password' === $page) {
$page = 'myaccount';
}

$allPages = MockWpCache::$cache['pages'];

Check notice on line 106 in tests/mock_wc_functions.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/mock_wc_functions.php#L106

Avoid using undefined variables such as '$cache' which will lead to PHP notices.

foreach ($allPages as $pageId => $singlePage) {
if ($singlePage['data']['pageName'] === $page) {
return $pageId;
}
}

return -1;
}