Last active
January 15, 2021 15:13
-
-
Save martinlipp/83a145b1b3faa0ed6f6f5d74462cbc37 to your computer and use it in GitHub Desktop.
Cart hook to prefill billing address with data from frontend user (TYPO3 10.4, extcode/cart)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (TYPO3_MODE === 'FE') { | |
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['showCartActionAfterCartWasLoaded'][] = \Acme\Demo\Hooks\FrontendUserHook::class . '->showCartActionAfterCartWasLoaded'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Demo\Hooks; | |
use Extcode\Cart\Domain\Model\Order\BillingAddress; | |
use TYPO3\CMS\Core\Context\Context; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser; | |
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository; | |
use TYPO3\CMS\Extbase\Object\ObjectManager; | |
/** | |
* FrontendUserHook | |
*/ | |
class FrontendUserHook | |
{ | |
/** | |
* @param array &$parameters | |
*/ | |
public function showCartActionAfterCartWasLoaded(&$parameters, $refObj) | |
{ | |
$billingAddress = $parameters['billingAddress']; | |
$request = $parameters['request']; | |
if ($request && $request->getOriginalRequest() && $request->getOriginalRequest()->getArguments()) { | |
return; | |
} | |
$context = GeneralUtility::makeInstance( | |
Context::class | |
); | |
$feUserUid = $context->getPropertyFromAspect('frontend.user', 'id'); | |
$objectManager = GeneralUtility::makeInstance( | |
ObjectManager::class | |
); | |
$frontendUserRepository = $objectManager->get( | |
FrontendUserRepository::class | |
); | |
$frontenUser = $frontendUserRepository->findByUid($feUserUid); | |
if ($frontenUser instanceof FrontendUser) { | |
$billingAddress = $objectManager->get( | |
BillingAddress::class | |
); | |
$billingAddress->setEmail($frontenUser->getEmail()); | |
$billingAddress->setTitle($frontenUser->getTitle()); | |
$billingAddress->setFirstName($frontenUser->getFirstName()); | |
$billingAddress->setLastName($frontenUser->getLastName()); | |
$billingAddress->setCompany($frontenUser->getCompany()); | |
$billingAddress->setStreet($frontenUser->getAddress()); | |
$billingAddress->setZip($frontenUser->getZip()); | |
$billingAddress->setCity($frontenUser->getCity()); | |
$parameters['billingAddress'] = $billingAddress; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfect, many thanks !