@@ -737,31 +737,41 @@ public function hookDisplayGDPRConsent(array $params): string
737
737
/** @var ConsentRepository $consentRepository */
738
738
$ consentRepository = $ this ->get ('PrestaShop\Module\Psgdpr\Repository\ConsentRepository ' );
739
739
740
+ // If no information about a module requesting a consent checkbox was passed, nothing to do
740
741
if (!isset ($ params ['id_module ' ])) {
741
742
return '' ;
742
743
}
743
744
744
745
$ moduleId = (int ) $ params ['id_module ' ];
745
746
747
+ // If this module does not have a consent activated, nothing to do
746
748
if (false === $ consentRepository ->findModuleConsentIsActive ($ moduleId )) {
747
749
return '' ;
748
750
}
749
751
750
752
$ message = $ consentRepository ->findModuleConsentMessage ($ moduleId , $ this ->context ->language ->id );
751
753
$ url = $ this ->context ->link ->getModuleLink ($ this ->name , 'FrontAjaxGdpr ' , [], true );
752
754
753
- $ customerId = $ this ->context ->customer ->id ;
755
+ /*
756
+ * Prepare customer data. The tokens will be used to reverse validate the data in the AJAX request.
757
+ * customer_token is used when the customer is logged in, guest_token when not. Even if both customer
758
+ * and guest IDs are empty, we can still mark the consent by using the IP address.
759
+ */
754
760
$ guestId = 0 ;
755
-
756
- if ($ customerId == null ) {
761
+ $ customerId = 0 ;
762
+ $ secureKey = '' ;
763
+ if (!empty ($ this ->context ->customer ->id )) {
764
+ $ customerId = $ this ->context ->customer ->id ;
765
+ $ secureKey = $ this ->context ->customer ->secure_key ;
766
+ }
767
+ if (!empty ($ this ->context ->cart ->id_guest )) {
757
768
$ guestId = $ this ->context ->cart ->id_guest ;
758
- $ customerId = 0 ;
759
769
}
760
770
761
771
$ this ->context ->smarty ->assign ([
762
772
'psgdpr_id_guest ' => $ guestId ,
763
773
'psgdpr_id_customer ' => $ customerId ,
764
- 'psgdpr_customer_token ' => sha1 ($ this -> context -> customer -> secure_key ),
774
+ 'psgdpr_customer_token ' => sha1 ($ secureKey ),
765
775
'psgdpr_guest_token ' => sha1 ('psgdpr ' . $ guestId . $ _SERVER ['REMOTE_ADDR ' ] . date ('Y-m-d ' )),
766
776
'psgdpr_id_module ' => $ moduleId ,
767
777
'psgdpr_consent_message ' => $ message ,
0 commit comments