Skip to content

Commit

Permalink
Merge pull request #12 from FosterCommerce/address-fields
Browse files Browse the repository at this point in the history
Shipping address fields and forms
  • Loading branch information
peteeveleigh authored Aug 19, 2024
2 parents 7b79efd + 676bee0 commit 6e01862
Show file tree
Hide file tree
Showing 15 changed files with 1,340 additions and 134 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A companion plugin to the Foster Checkout Commerce templates.

## Requirements

This plugin requires Craft CMS 5.0.0 or later, and PHP 8.2 or later.
This plugin requires Craft CMS 5.0.0 or later, and PHP 8.0.2 or later.

## Installation

Expand Down
36 changes: 21 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
"craftcms/cms": "^5.0.0",
"craftcms/commerce": "^5.0.15"
},
"require-dev": {
"craftcms/generator": "^2.1",
"fostercommerce/rector": "dev-main",
"fostercommerce/ecs": "dev-main",
"craftcms/phpstan": "dev-main",
"phpstan/phpstan": "^1.11",
"craftcms/rector": "dev-main"
},
"autoload": {
"psr-4": {
"fostercommerce\\craftfostercheckout\\": "src/"
Expand All @@ -34,17 +26,31 @@
},
"config": {
"sort-packages": true,
"platform": {
"php": "8.2"
},
"allow-plugins": {
"yiisoft/yii2-composer": true,
"craftcms/plugin-installer": true
}
},
"scripts": {
"phpstan": "phpstan --memory-limit=1G",
"ecs-check": "ecs check --ansi --memory-limit=1G",
"ecs-fix": "ecs check --ansi --fix --memory-limit=1G",
"rector": "rector process --config rector.php",
"rector-dry-run": "rector process --dry-run --config rector.php"
}
"require-dev": {
"craftcms/generator": "^2.1",
"fostercommerce/rector": "dev-main",
"fostercommerce/ecs": "dev-main",
"craftcms/phpstan": "dev-main",
"phpstan/phpstan": "^1.11",
"craftcms/rector": "dev-main"
},
"scripts": {
"phpstan": [
"@php \"scripts/craft_stubs.php\"",
"phpstan --memory-limit=1G"
],
"ecs-check": "ecs check --ansi --memory-limit=1G",
"ecs-fix": "ecs check --ansi --fix --memory-limit=1G",
"rector": "rector process --config rector.php",
"rector-dry-run": "rector process --dry-run --config rector.php"
}
}

2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ includes:
- vendor/craftcms/phpstan/phpstan.neon

parameters:
scanFiles:
- phpstan_stubs.php
paths:
- src
level: 5
1,130 changes: 1,130 additions & 0 deletions phpstan_stubs.php

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions scripts/craft_stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$class = glob('storage/runtime/compiled_classes/CustomFieldBehavior*.php')[0]??null;
if ($class !== null) {
copy($class, 'phpstan_stubs.php');
}
10 changes: 5 additions & 5 deletions src/FosterCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class FosterCheckout extends Plugin

public bool $hasCpSettings = false;

public function init(): void
public function init()
{
parent::init();

Craft::setAlias('@fostercheckout', __DIR__);

// Defer most setup tasks until Craft is fully initialized
Craft::$app->onInit(function (): void {
Craft::$app->onInit(function () {
$this->registerComponents();
$this->attachEventHandlers();
$this->registerCustomVariables();
Expand Down Expand Up @@ -83,7 +83,7 @@ private function attachEventHandlers(): void
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $e): void {
function (Event $e) {
/** @var CraftVariable $variable */
$variable = $e->sender;

Expand All @@ -96,7 +96,7 @@ function (Event $e): void {
Event::on(
View::class,
View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
function (RegisterTemplateRootsEvent $event): void {
function (RegisterTemplateRootsEvent $event) {
$event->roots['foster-checkout'] = __DIR__ . '/templates';
}
);
Expand All @@ -105,7 +105,7 @@ function (RegisterTemplateRootsEvent $event): void {
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event): void {
function (RegisterUrlRulesEvent $event) {
// Get the paths from the settings
$paths = $this->checkout->paths();
$checkoutPath = $paths['checkout'] ?? 'checkout';
Expand Down
35 changes: 35 additions & 0 deletions src/formatters/CheckoutAddressFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace fostercommerce\craftfostercheckout\formatters;

use CommerceGuys\Addressing\AddressInterface;
use Craft;
use craft\elements\Address;

class CheckoutAddressFormatter extends \CommerceGuys\Addressing\Formatter\DefaultFormatter
{
public function format(AddressInterface $address, array $options = []): string
{
/** @var Address $address */
if (property_exists($address, 'firstName')) {
$address->firstName = null;
}
if (property_exists($address, 'lastName')) {
$address->lastName = null;
}

// if the address is in the US then get the State name, otherwise use the inputted value
//$state = $address->countryCode == 'US' ? Craft::$app->getAddresses()->subdivisionRepository->get($address->administrativeArea, [$address->countryCode])->getName() : $address->administrativeArea;

$addressLines[] = $address->addressLine1;
$addressLines[] = $address->addressLine2;
$addressLines[] = $address->addressLine3;
$addressLines[] = $address->locality;
$addressLines[] = $address->dependentLocality;
$addressLines[] = $address->administrativeArea;
$addressLines[] = $address->postalCode;
$addressLines[] = $address->getCountry()->getName();

return ($address->fullName ? $address->fullName . '<br />' : '') . implode(' / ', array_filter($addressLines));
}
}
17 changes: 8 additions & 9 deletions src/services/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function note($page): ?string

$note = null;

if ($elementHandle && $fieldHandle) {
if ($elementHandle and $fieldHandle) {
$global = GlobalSet::find()->handle($elementHandle)->one() ?? null;
$entry = Entry::find()->section($elementHandle)->one() ?? null;
$element = $global ?? $entry;
Expand All @@ -79,7 +79,7 @@ public function note($page): ?string
} else {
$note = null;
}
} catch (InvalidFieldException) {
} catch (InvalidFieldException $e) {
$note = null;
}
}
Expand Down Expand Up @@ -130,13 +130,13 @@ public function lineItemImage(LineItem $lineItem): ?Asset
if ($fieldInfo['level'] === 'variant') {
try {
$image = $variant->getFieldValue($fieldInfo['handle'])->one();
} catch (InvalidFieldException) {
} catch (InvalidFieldException $e) {
$image = null;
}
} else {
try {
$image = $product->getFieldValue($fieldInfo['handle'])->one();
} catch (InvalidFieldException) {
} catch (InvalidFieldException $e) {
$image = null;
}
}
Expand All @@ -151,7 +151,7 @@ public function getCountries(): array
{
try {
return Plugin::getInstance()->getStore()->getStore()->getCountriesList();
} catch (InvalidConfigException) {
} catch (InvalidConfigException $e) {
return [];
}
}
Expand All @@ -163,7 +163,7 @@ public function getRegions(): array
{
try {
return Plugin::getInstance()->getStore()->getStore()->getAdministrativeAreasListByCountryCode();
} catch (InvalidConfigException) {
} catch (InvalidConfigException $e) {
return [];
}
}
Expand All @@ -175,7 +175,7 @@ public function getDiscounts(): array
{
try {
return Plugin::getInstance()->getDiscounts()->allDiscounts;
} catch (InvalidConfigException) {
} catch (InvalidConfigException $e) {
return [];
}
}
Expand All @@ -187,10 +187,9 @@ public function getGateways(): array
{
try {
$gateways = Plugin::getInstance()->gateways->getAllCustomerEnabledGateways();
} catch (InvalidConfigException) {
} catch (InvalidConfigException $e) {
$gateways = [];
}

$gatewaysArr = [];
foreach ($gateways as $gateway) {
$gatewaysArr[] = [
Expand Down
17 changes: 16 additions & 1 deletion src/templates/_components/app/address-fields.twig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@
value: address ? address.addressLine2 : '',
errors: errors.addressLine2 ?? []
} %}

{% include 'foster-checkout/_components/base/input-text-clearable' with {
context: context,
label: 'Line 3',
hideLabel: true,
id: 'addressLine3',
name: 'addressLine3',
type: 'text',
autocomplete: 'address-line3',
placeholder: '',
value: address ? address.addressLine2 : '',
errors: errors.addressLine2 ?? []
} %}
</div>

<div class="sm:col-span-1">
Expand Down Expand Up @@ -127,10 +140,12 @@
required
>
<option value="" disabled>{{ 'Select'|t('foster-checkout') }}</option>
<option v-cloak v-for="(region, key) in regions[countryCode]" :value="key" :selected="key === administrativeArea">
<option v-cloak v-for="(region, key) in regions[countryCode]" :value="key" :selected="key === administrativeArea" {{ address.administrativeArea == '${key}' ? ' selected' : ''}}>
${region}
</option>
</select>


</div>

<div v-cloak v-else>
Expand Down
15 changes: 15 additions & 0 deletions src/templates/_components/app/formatted-address.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% set address = address ?? null %}
{% if address %}
{% set addressService = craft.app.getAddresses() %}
{% set customFormatter = create(
'fostercommerce\\craftfostercheckout\\formatters\\CheckoutAddressFormatter',
[
addressService.getAddressFormatRepository(),
addressService.getCountryRepository(),
addressService.getSubdivisionRepository(),
]
) %}
{{ address|address({ }, customFormatter) }}
{% else %}
No address to display
{% endif %}
8 changes: 6 additions & 2 deletions src/templates/_components/app/steps-completed.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
<span class="inline-block font-semibold text-black">
{{ 'User'|t('foster-checkout') }}
</span>
{% if not currentUser %}
<a href="#" class="inline-block underline text-[var(--brandColor)] sm:hidden">
{{ 'Edit'|t('foster-checkout') }}
</a>
{% endif %}
</dt>
<dd class="mt-1 sm:flex sm:justify-between sm:items-start sm:gap-4 sm:mt-0">
<span class="text-black">{{ cart.email }}</span>
{% if not currentUser %}
<a class="hidden sm:inline-block sm:underline sm:text-[var(--brandColor)]" href="{{ craft.fostercheckout.getPath('checkout') ~ '/email' }}">
{{ 'Edit'|t('foster-checkout') }}
</a>
{% endif %}
</dd>
</div>
{% endif %}
Expand All @@ -33,9 +37,9 @@
</dt>
<dd class="mt-1 sm:flex sm:justify-between sm:items-start sm:gap-4 sm:mt-0">
<p class="text-black">
John Doe / Ohio, OH 43021 / United States, Columbus / St. 480 E Broad
{% include'foster-checkout/_components/app/formatted-address' with { 'address': cart.shippingAddress} %}
</p>
<a class="hidden sm:inline-block sm:underline sm:text-[var(--brandColor)]" href="#">
<a class="hidden sm:inline-block sm:underline sm:text-[var(--brandColor)]" href="{{ craft.fostercheckout.getPath('checkout') ~ '/address' }}">
{{ 'Edit'|t('foster-checkout') }}
</a>
</dd>
Expand Down
20 changes: 20 additions & 0 deletions src/templates/_layouts/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@
);
@apply border-[var(--brandColor)];
}
.checkbox {
@apply relative;
}
.checkbox label::before {
content: '';
@apply relative inline-block w-5 h-5 flex-shrink-0 border-2 border-gray-500 rounded-sm;
}
.checkbox input[type="checkbox"]:focus-visible + label::before {
@apply ring ring-offset-1;
}
.checkbox input[type="checkbox"]:checked + label::before {
background: var(--brandColor) 10%;
transform: scale(0.5);
@apply border-[var(--brandColor)];
}
.checkbox input[type="checkbox"]:checked + label::after {
content: '';
@apply absolute left-0 w-5 h-5 border-2 border-[var(--brandColor)] rounded-sm;
}
}
</style>
</head>
Expand Down
Loading

0 comments on commit 6e01862

Please sign in to comment.