cart
cart copied to clipboard
Prefill address with logged in frontend_user
Feature Request
If a frontend user is logged in, the billing address should be filled with the values.
Currently I am using the hook in the CartController
ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['showCartActionAfterCartWasLoaded'][]
= \StudioMitte\Products\Hooks\Cart\PrefillAddress::class . '->run';
and the file itself
<?php
declare(strict_types=1);
namespace StudioMitte\Products\Hooks\Cart;
use Extcode\Cart\Domain\Model\Order\BillingAddress;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Prefill EXT:cart address
*/
class PrefillAddress
{
protected $userMapping = [
'title' => 'title',
'first_name' => 'firstName',
'last_name' => 'lastName',
'email' => 'email',
'address' => 'street',
'zip' => 'zip',
'city' => 'city',
'company' => 'company',
];
public function run(array &$params)
{
$frontendUserRow = $this->getFrontendUserRow();
/** @var BillingAddress $billingAddress */
$billingAddress = $params['billingAddress'];
if ($billingAddress === null && !empty($frontendUserRow)) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$billingAddress = $objectManager->get(BillingAddress::class);
foreach ($this->userMapping as $fieldName => $property) {
if (empty($frontendUserRow[$fieldName])) {
continue;
}
$setter = 'set' . ucfirst($property);
$billingAddress->$setter($frontendUserRow[$fieldName]);
}
$params['billingAddress'] = $billingAddress;
}
}
protected function getFrontendUserRow(): array
{
/** @var TypoScriptFrontendController $tsfe */
$tsfe = $GLOBALS['TSFE'];
return $tsfe->fe_user->user ?? [];
}
}
Is this also working with Cart version 7.0.0?