custom/plugins/imnxxNWBUserLogin/src/Subscriber/MySubscriber.php line 981

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace imnxxNWBUserLogin\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  11. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  12. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  13. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  14. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  15. use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
  16. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  19. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  20. use Doctrine\DBAL\Connection;
  21. use PDO;
  22. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  23. class MySubscriber implements EventSubscriberInterface {
  24.     private CartService $cartService;
  25.     private Connection $connection;
  26.     public function __construct(CartService $cartServiceConnection $connection) {
  27.         $this->cartService $cartService;
  28.         $this->connection $connection;
  29.     }
  30.     public static function getSubscribedEvents(): array {
  31.         return [
  32.             SearchPageLoadedEvent::class => 'onSearchPageLoaded',
  33.             GenericPageLoadedEvent::class => 'onPageLoaded',
  34.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  35.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded',
  36.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  37.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  38.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoaded',
  39.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoaded',
  40.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoaded',
  41.             AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoaded',
  42.             AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoaded',
  43.             CustomerRegisterEvent::class => 'onCustomerRegisterEvent',
  44.             LineItemAddedEvent::class => 'onLineItemAdded',
  45.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  46.         ];
  47.     }
  48.     public function onCustomerRegisterEvent(CustomerRegisterEvent $event) {
  49.         if ($_POST['nwb_user_account']) {
  50.             $data json_decode($_POST['nwb_user_account']);
  51.             // update user data - AccountID and SessionID
  52.             $url 'https://' $_SERVER['SERVER_NAME'] . '/login_api/imn_nwb_set_customer_account_id.php';
  53.             $curl curl_init($url);
  54.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  55.             curl_setopt($curlCURLOPT_POSTtrue);
  56.             curl_setopt($curlCURLOPT_POSTFIELDS$data);
  57.             $response curl_exec($curl);
  58.             unset($_SESSION['nwb_user_account']);
  59.             unset($_SESSION['test']);
  60.         }
  61.     }
  62.     public function onSearchPageLoaded(SearchPageLoadedEvent $event) {
  63.     }
  64.     public function onPageLoaded(GenericPageLoadedEvent $event) {
  65.         $nwbService $event
  66.             ->getSalesChannelContext()
  67.             ->getSalesChannel()
  68.             ->getCustomFields()['bis_url'];
  69.         $cookieBisNameUser $event
  70.             ->getSalesChannelContext()
  71.             ->getSalesChannel()
  72.             ->getCustomFields()['cookie_bis'];
  73.         if (isset($_GET['DokumentenId']) && $_GET['DokumentenId'] != '') {
  74.             if (!file_exists(dirname(__FILE__) . '/../document_logs/' date('Y') . '/' date('Y-m'))) {
  75.                 mkdir(dirname(__FILE__) . '/../document_logs/' date('Y') . '/' date('Y-m'), 0777true);
  76.             }
  77.             $t microtime(true);
  78.             $micro sprintf('%06d', ($t floor($t)) * 1000000);
  79.             $documentIdLogName =
  80.                 dirname(__FILE__) .
  81.                 '/../document_logs/' .
  82.                 date('Y') .
  83.                 '/' .
  84.                 date('Y-m') .
  85.                 '/document_id_log_' .
  86.                 date('Y-m-d') .
  87.                 '.txt';
  88.             $documentLogContent =
  89.                 "\n\n" .
  90.                 '----------------------------------------+----------------------------------------+----------------------------------------' .
  91.                 "\n";
  92.             $documentLogContent =
  93.                 $documentLogContent .
  94.                 "\n" .
  95.                 date('Y-m-d H:i:s.'time()) .
  96.                 $micro .
  97.                 "\n" .
  98.                 'Request documentid: ' .
  99.                 $_GET['DokumentenId'] .
  100.                 "\n";
  101.             if (
  102.                 isset($_SESSION['_sf2_attributes']['sw-context-token']) &&
  103.                 $_SESSION['_sf2_attributes']['sw-context-token'] != ''
  104.             ) {
  105.                 $this->cartService->deleteCart($event->getSalesChannelContext());
  106.             }
  107.             $url =
  108.                 'https://' .
  109.                 $nwbService .
  110.                 '/customercare/api/order/getbestellinformationen?bisDocumentId=' .
  111.                 $_GET['DokumentenId'];
  112.             $curl curl_init($url);
  113.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  114.             $response curl_exec($curl);
  115.             curl_close($curl);
  116.             $json json_decode($response);
  117.             if ($json->Success) {
  118.                 $json->Bestellinformationen->DokumentenId $_GET['DokumentenId'];
  119.                 $_SESSION['bestellinformationen'] = $json->Bestellinformationen;
  120.                 $_SESSION['DokumentenId'] = $_GET['DokumentenId'];
  121.                 setcookie('DokumentenId_productnumber_for_login'$_GET['DokumentenId']);
  122.                 $add_product['nwb_add_product'] = [
  123.                     'nwb_product_number' => $json->Bestellinformationen->Bestellnummer,
  124.                 ];
  125.                 $event->getPage()->assign($add_product);
  126.                 $_SESSION['nwb_add_product_next_url'] = '/checkout/confirm';
  127.                 $documentLogContent =
  128.                     $documentLogContent .
  129.                     'After request DocumentId: ' .
  130.                     $json->Bestellinformationen->DokumentenId .
  131.                     "\n";
  132.                 $logHandle fopen($documentIdLogName'a+');
  133.                 fwrite($logHandle$documentLogContent);
  134.                 fclose($logHandle);
  135.                 unset($documentLogContent);
  136.                 return true;
  137.             }
  138.             $documentLogContent =
  139.                 $documentLogContent 'After request DocumentId: ' 'NO REQUEST MADE' "\n";
  140.             $logHandle fopen($documentIdLogName'a+');
  141.             fwrite($logHandle$documentLogContent);
  142.             fclose($logHandle);
  143.             unset($documentLogContent);
  144.         }
  145.         if (
  146.             (!isset($_SESSION['GET_ADDRESSES']) ||
  147.                 (!str_contains($_SERVER['REQUEST_URI'], '/checkout/') &&
  148.                     !str_contains($_SERVER['REQUEST_URI'], '/account/'))) &&
  149.             !(isset($_SESSION['login_timestamp']) && $_SESSION['login_timestamp'] + 10 >= time())
  150.         ) {
  151.             if (isset($_GET['test']) && $_GET['test'] == 'session') {
  152.                 echo '<pre>$_SESSION<br>';
  153.                 var_dump($_SESSION);
  154.                 echo '</pre>';
  155.                 die();
  156.             }
  157.             if (isset($_GET['url_test']) && $_GET['url_test'] == 'url_test') {
  158.                 echo '<pre>$_SERVER<br>';
  159.                 var_dump($_SERVER);
  160.                 echo '</pre>';
  161.                 die();
  162.             }
  163.             if (isset($_GET['logout']) && $_GET['logout'] == 'logout') {
  164.                 $customer $event->getSalesChannelContext()->getCustomer();
  165.                 if ($customer != null) {
  166.                     // Clear cart
  167.                     $this->cartService->deleteCart($event->getSalesChannelContext());
  168.                 }
  169.                 if (isset($_SESSION['stay_on_confirm'])) {
  170.                     unset($_SESSION['stay_on_confirm']);
  171.                 }
  172.                 header('Location: https://' $_SERVER['SERVER_NAME'] . '/account/logout');
  173.                 exit();
  174.             }
  175.             $customer $event->getSalesChannelContext()->getCustomer();
  176.             // send partnerkonto back to confirm page - musste ausgeklammert werden wegen fehlern
  177.             //        if( isset($_GET['test']) && $_GET['test'] == 'session') {
  178.             //            echo('<pre>stay_on_confirm<br>');var_dump($_SESSION);echo('</pre>');
  179.             //        }
  180.             //        if( isset($_GET['test']) && $_GET['test'] == 'server') {
  181.             //            echo('<pre>REQUEST_URI<br>');var_dump($_SERVER["REQUEST_URI"]);echo('</pre>');
  182.             //            if(
  183.             //                isset($_SESSION['stay_on_confirm']) &&
  184.             //                $_SESSION['stay_on_confirm'] == 'true' &&
  185.             //                !str_contains($_SERVER["REQUEST_URI"], '/checkout/confirm') &&
  186.             //                !str_contains($_SERVER["REQUEST_URI"], '/checkout/finish') &&
  187.             //                $customer != null
  188.             //            ) {
  189.             //                echo('thats true!');
  190.             //            }
  191.             //            echo('is it true?');
  192.             //        }
  193.             //            if(
  194.             //                isset($_SESSION['stay_on_confirm']) &&
  195.             //                $_SESSION['stay_on_confirm'] == 'true' &&
  196.             //                !str_contains($_SERVER["REQUEST_URI"], '/checkout/confirm') &&
  197.             //                !str_contains($_SERVER["REQUEST_URI"], '/checkout/finish') &&
  198.             //                $customer != null
  199.             //            ) {
  200.             //                header('Location: https://shop.nwb.de/checkout/confirm/');
  201.             //                exit;
  202.             //            }
  203.             //        if(
  204.             //            isset($_SESSION['stay_on_confirm']) &&
  205.             //            $_SESSION['stay_on_confirm'] == 'true' &&
  206.             //            $_SERVER["REQUEST_URI"] != "/checkout/finish" &&
  207.             //            $customer != null
  208.             //        ) {
  209.             //            header('Location: https://shop.nwb.de/checkout/confirm/');
  210.             //            exit;
  211.             //Test
  212.             //        }
  213.             if (!is_null($event->getPage()->getHeader())) {
  214.                 $active_category $event
  215.                     ->getPage()
  216.                     ->getHeader()
  217.                     ->getNavigation()
  218.                     ->getActive();
  219.                 if (isset($active_category->getCustomFields()['hide_lp_header'])) {
  220.                     $hide_lp_header['hide_lp_header'] = $active_category->getCustomFields()['hide_lp_header'];
  221.                     $event->getPage()->assign($hide_lp_header);
  222.                 }
  223.                 if (isset($active_category->getCustomFields()['navi_entry_point'])) {
  224.                     $navi_entry_point['navi_entry_point'] = $active_category->getCustomFields()[
  225.                         'navi_entry_point'
  226.                     ];
  227.                     $event->getPage()->assign($navi_entry_point);
  228.                 }
  229.             }
  230.             if (isset($_GET['send']) && $_GET['send'] == 'success') {
  231.                 $landingpage_form_send['landingpage_form_send'] = 'success';
  232.                 $event->getPage()->assign($landingpage_form_send);
  233.             }
  234.             $recently_ordered['recently_ordered'] = false;
  235.             if (isset($_SESSION['checkout_finish_timestamp'])) {
  236.                 if (time() < $_SESSION['checkout_finish_timestamp'] + 60) {
  237.                     $recently_ordered['recently_ordered'] = true;
  238.                 } else {
  239.                     unset($_SESSION['checkout_finish_timestamp']);
  240.                 }
  241.             }
  242.             $event->getPage()->assign($recently_ordered);
  243.             $old_browser['old_browser'] = 0;
  244.             if (
  245.                 strpos($_SERVER['HTTP_USER_AGENT'], 'Edge') !== false ||
  246.                 strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false
  247.             ) {
  248.                 $old_browser['old_browser'] = 1;
  249.             }
  250.             $event->getPage()->assign($old_browser);
  251.             if (isset($_GET['et_sub']) && $_GET['et_sub'] != '') {
  252.                 $_SESSION['moorl-order-notice.order_target_group_number'] = $_GET['et_sub'];
  253.                 $session = new Session();
  254.                 $session->set('moorl-order-notice.order_target_group_number'$_GET['et_sub']);
  255.             }
  256.             if (
  257.                 isset($_SESSION['moorl-order-notice.order_target_group_number']) &&
  258.                 $_SESSION['moorl-order-notice.order_target_group_number'] != ''
  259.             ) {
  260.                 $et_sub['et_sub'] = $_SESSION['moorl-order-notice.order_target_group_number'];
  261.                 $event->getPage()->assign($et_sub);
  262.                 if (
  263.                     (!isset($_GET['et_sub']) || $_GET['et_sub'] == '') &&
  264.                     !str_contains($_SERVER['REQUEST_URI'], '/checkout/confirm') &&
  265.                     !str_contains($_SERVER['REQUEST_URI'], '/checkout/finish') &&
  266.                     !str_contains($_SERVER['REQUEST_URI'], '/suggest') &&
  267.                     !str_contains($_SERVER['REQUEST_URI'], '/widget')
  268.                 ) {
  269.                     $header_location 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  270.                     if( str_contains($_SERVER['REQUEST_URI'],'?') ) {
  271.                         $request_url_parts explode('?'$_SERVER['REQUEST_URI'], 2);
  272.                         if($request_url_parts[1]!='') {
  273.                             $header_location $header_location '&et_sub=' $et_sub['et_sub'];
  274.                         } else {
  275.                             $header_location $header_location 'et_sub=' $et_sub['et_sub'];
  276.                         }
  277.                     } else {
  278.                         $header_location $header_location '?et_sub=' $et_sub['et_sub'];
  279.                     }
  280.                     header('Location: ' $header_location );
  281.                     die();
  282.                 }
  283.             }
  284.             if (isset($_GET['product_to_cart']) && $_GET['product_to_cart'] != '') {
  285.                 $add_product['nwb_add_product'] = [
  286.                     'nwb_product_number' => $_GET['product_to_cart'],
  287.                 ];
  288.                 $event->getPage()->assign($add_product);
  289.             }
  290.             if (isset($_SESSION['logout_redirect']) && $_SESSION['logout_redirect'] != '') {
  291.                 $logout_redirect $_SESSION['logout_redirect'];
  292.                 unset($_SESSION['logout_redirect']);
  293.                 unset($_SESSION['login_timestamp']);
  294.                 header('Location: ' $logout_redirect);
  295.                 exit();
  296.             }
  297.             if (
  298.                 !isset($_COOKIE[$cookieBisNameUser]) &&
  299.                 isset($_COOKIE['sw-states']) &&
  300.                 preg_match('/logged-in/i'$_COOKIE['sw-states']) &&
  301.                 $customer !== null &&
  302.                 !$customer->getGuest()
  303.             ) {
  304.                 $_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
  305.                 header('Location: https://' $_SERVER['SERVER_NAME'] . '/account/logout');
  306.                 exit();
  307.             }
  308.         }
  309.     }
  310.     public function onProductPageLoaded(ProductPageLoadedEvent $event) {
  311.         $page $event->getPage();
  312.         $product $page->getProduct();
  313.         if (isset($_GET['testen']) && $_GET['testen'] == 'testen') {
  314.             $cross_selling_elements $page->getCrossSellings()->getElements();
  315.             foreach ($cross_selling_elements as $cross_selling_element) {
  316.                 echo '<pre>$cross_selling_element<br>';
  317.                 print_r($cross_selling_element->getCrossSelling());
  318.                 echo '</pre>';
  319.             }
  320.             //            echo('<pre>product<br>');print_r($product);echo('</pre>');
  321.         }
  322.         $custom_fields $product->getCustomFields();
  323.         // todo: get varianten price
  324.         //        echo('<pre>');print_r($_SERVER);echo('</pre>');
  325.         //        echo('<pre>');print_r($product->getParentId());echo('</pre>');
  326.         //        if( $product->getParentId() != '' ) {
  327.         //            $url = '/product_api/get_products_by_parent_id.php';
  328.         //            $curl = curl_init($url);
  329.         //            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  330.         //            $response = curl_exec($curl);
  331.         //            echo('<pre>response');var_dump($response);echo('</pre>');
  332.         //        }
  333.         $education_proof['need_education_proof'] = false;
  334.         foreach ($product->getOptions()->getElements() as $option) {
  335.             if ($option->getGroup()->getName() == 'Preisart') {
  336.                 if ($option->getName() == 'Ausbildungspreis') {
  337.                     $education_proof['need_education_proof'] = $option->getName();
  338.                 }
  339.             }
  340.         }
  341.         $event->getPage()->assign($education_proof);
  342.         //wurde ausgelagert ins authorProductList Plugin
  343.         if (
  344.             isset($custom_fields['custom_product_attributs_autor_ids']) &&
  345.             $custom_fields['custom_product_attributs_autor_ids'] != ''
  346.         ) {
  347.             $result $this->connection->executeQuery(
  348.                 'SELECT *, HEX(cbax_lexicon_entry.id) as author_sw_id ' .
  349.                     'From cbax_lexicon_entry_translation ' .
  350.                     'INNER JOIN cbax_lexicon_entry ON cbax_lexicon_entry_translation.cbax_lexicon_entry_id=cbax_lexicon_entry.id ' .
  351.                     'WHERE cbax_lexicon_entry_translation.attribute6 IN (' .
  352.                     $custom_fields['custom_product_attributs_autor_ids'] .
  353.                     ')',
  354.             );
  355.             $unsorted_authors = [];
  356.             $authors['authors'] = [];
  357.             while ($row $result->fetch(PDO::FETCH_ASSOC)) {
  358.                 $unsorted_authors[$row['attribute6']] = $row;
  359.             }
  360.             $author_ids explode(','$custom_fields['custom_product_attributs_autor_ids']);
  361.             foreach ($author_ids as $author_id) {
  362.                 if(isset($unsorted_authors[$author_id]))
  363.                 $authors['authors'][$author_id] = $unsorted_authors[$author_id];
  364.             }
  365.             if (isset($_GET['test']) && $_GET['test'] == 'author') {
  366.                 echo '<pre>$authors<br>';
  367.                 print_r($authors);
  368.                 echo '</pre>';
  369.             }
  370.             $event->getPage()->assign($authors);
  371.         }
  372.     }
  373.     public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event) {
  374.         $page $event->getPage();
  375.         $cart $page->getCart();
  376.         $lineitems $cart->getLineItems()->getFlat();
  377.         $cart_price $cart->getPrice();
  378.         $this->changeCartPriceForAboProducts($cart_price$lineitems);
  379.     }
  380.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event) {
  381.         $cookieBisNameUser $event
  382.             ->getSalesChannelContext()
  383.             ->getSalesChannel()
  384.             ->getCustomFields()['cookie_bis'];
  385.         $cookieBisNameToBis $event
  386.             ->getSalesChannelContext()
  387.             ->getSalesChannel()
  388.             ->getCustomFields()['cookie_bis_sending'];
  389.         $nwbService $event
  390.             ->getSalesChannelContext()
  391.             ->getSalesChannel()
  392.             ->getCustomFields()['bis_url'];
  393.         if (isset($_COOKIE[$cookieBisNameUser]) && $_COOKIE[$cookieBisNameUser] != '') {
  394.             $url 'https://' $nwbService '/customercare/api/accounts/info';
  395.             $curl curl_init($url);
  396.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  397.             curl_setopt($curlCURLOPT_COOKIE$cookieBisNameToBis '=' $_COOKIE[$cookieBisNameUser]);
  398.             $response curl_exec($curl);
  399.             // Close cURL session
  400.             curl_close($curl);
  401.             $user_data json_decode($response);
  402.             if (isset($_SESSION['dokument_kauf']) && isset($user_data->Partnerkonto)) {
  403.                 if ($user_data->Partnerkonto && $_SESSION['dokument_kauf'] == 'true') {
  404.                     $_SESSION['stay_on_confirm'] = 'true';
  405.                     unset($_SESSION['dokument_kauf']);
  406.                 }
  407.             }
  408.         }
  409.         $page $event->getPage();
  410.         $cart $page->getCart();
  411.         //musste ebenfalls ausgeklammert werden wegen fehler
  412.         //        if( isset($_SESSION['stay_on_confirm']) && $_SESSION['stay_on_confirm'] == 'true' ) {
  413.         //            $event->getPage()->assign( ['stay_on_confirm' => true] );
  414.         //        }
  415.         //
  416.         //        if( isset($_GET['stay_on_confirm']) && $_GET['stay_on_confirm'] == 'true' ) {
  417.         //            $event->getPage()->assign( ['stay_on_confirm' => true] );
  418.         //        }
  419.         $lineitems $cart->getLineItems()->getFlat();
  420.         $cart_price $cart->getPrice();
  421.         $this->changeCartPriceForAboProducts($cart_price$lineitems);
  422.         $crossselling_return get_cross_selling_items($lineitems);
  423.         $education_proof['need_education_proof'] = false;
  424.         $download_products = [];
  425.         $hasDocumentIdProductInCart false;
  426.         $libreka_download_links '';
  427.         foreach ($lineitems as $lineitem) {
  428.             if (
  429.                 isset($lineitem->getPayload()['customFields']['custom_product_attributs_dokumenten_id']) &&
  430.                 $lineitem->getPayload()['customFields']['custom_product_attributs_dokumenten_id'] != ''
  431.             ) {
  432.                 $hasDocumentIdProductInCart true;
  433.             }
  434.             if (
  435.                 isset($lineitem->getPayload()['customFields']['custom_product_attributs_email_gruppe']) &&
  436.                 $lineitem->getPayload()['customFields']['custom_product_attributs_email_gruppe'] ==
  437.                     'E-Book-Email-Gruppe'
  438.             ) {
  439.                 if (
  440.                     $lineitem->getPayload()['customFields'][
  441.                         'custom_product_attributs_libreka_download_format'
  442.                     ] == 'EPUB'
  443.                 ) {
  444.                     $librekaDownloadFormat '029';
  445.                 } elseif (
  446.                     $lineitem->getPayload()['customFields'][
  447.                         'custom_product_attributs_libreka_download_format'
  448.                     ] == 'PDF'
  449.                 ) {
  450.                     $librekaDownloadFormat '002';
  451.                 }
  452.                 $url =
  453.                     'https://' .
  454.                     $nwbService .
  455.                     '/customercare/api/order/GetLibrekaDownloadLink/?accountId=' .
  456.                     $user_data->AccountID .
  457.                     '&isbn=' .
  458.                     $lineitem->getPayload()['customFields']['custom_product_attributs_isbn'] .
  459.                     '&librekaDownloadFormat=' .
  460.                     $librekaDownloadFormat .
  461.                     '&idBestellposition=11';
  462.                 $curl curl_init($url);
  463.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  464.                 $response curl_exec($curl);
  465.                 curl_close($curl);
  466.                 if ($libreka_download_links != '') {
  467.                     $libreka_download_links $libreka_download_links '|';
  468.                 }
  469.                 $libreka_download_links =
  470.                     $libreka_download_links .
  471.                     $lineitem->getPayload()['productNumber'] .
  472.                     '=>' .
  473.                     json_decode($response)->Url;
  474.             }
  475.             foreach ($lineitem->getPayload()['options'] as $option) {
  476.                 if ($option['group'] == 'Preisart') {
  477.                     if ($option['option'] == 'Ausbildungspreis') {
  478.                         $education_proof['need_education_proof'] = $option['option'];
  479.                     }
  480.                 }
  481.             }
  482.         }
  483.         $_SESSION['prevent_order_export'] = false;
  484.         $session = new Session();
  485.         $session->set('moorl-order-notice.libreka_download_links'$libreka_download_links);
  486.         $advertising_permission['advertising_permission'] = $session->get(
  487.             'moorl-order-notice.advertising_permission',
  488.         );
  489.         if (is_null($advertising_permission['advertising_permission'])) {
  490.             $advertising_permission['advertising_permission'] = false;
  491.         }
  492.         $event->getPage()->assign($advertising_permission);
  493.         $event->getPage()->assign($education_proof);
  494.         $event->getPage()->assign($crossselling_return);
  495.         // if(!$hasDocumentIdProductInCart && isset($_SESSION['bestellinformationen'])) {
  496.         //     unset($_SESSION['bestellinformationen']);
  497.         // }
  498.         if (isset($_SESSION['bestellinformationen'])) {
  499.             $bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
  500.             $event->getPage()->assign($bestellinformationen);
  501.             $session = new Session();
  502.             $session->set('moorl-order-notice.bestellinformationen'$_SESSION['bestellinformationen']);
  503.         }
  504.     }
  505.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event) {
  506.         if (isset($_SESSION['bestellinformationen'])) {
  507.             $orderProducts $event
  508.                 ->getPage()
  509.                 ->getOrder()
  510.                 ->getLineItems();
  511.             $documentProductInOrder false;
  512.             /**
  513.              * @var OrderLineItemEntity $product
  514.              */
  515.             foreach ($orderProducts as $product) {
  516.                 if (
  517.                     $_SESSION['bestellinformationen']->Bestellnummer ==
  518.                     $product->getPayload()['productNumber']
  519.                 ) {
  520.                     $documentProductInOrder true;
  521.                     break;
  522.                 }
  523.             }
  524.             if (!$documentProductInOrder) {
  525.                 unset($_SESSION['bestellinformationen']);
  526.             }
  527.         }
  528.         // session setzen zum logout link ausblenden
  529.         $_SESSION['checkout_finish_timestamp'] = time();
  530.         $page $event->getPage();
  531.         $order $page->getOrder();
  532.         $customer $order->getOrderCustomer();
  533.         $order_price $order->getPrice();
  534.         $lineItems $order->getLineItems()->getElements();
  535.         $this->changeCartPriceForAboProducts($order_price$lineItems);
  536.         $crossselling_return get_cross_selling_items($lineItemstrue);
  537.         if (isset($_GET['test']) && $_GET['test'] == 'additem') {
  538.             echo '<pre>$crossselling_return<br>';
  539.             var_dump($crossselling_return);
  540.             echo '</pre>';
  541.             die();
  542.         }
  543.         $checkout_finish_page_url '';
  544.         $education_proof['need_education_proof'] = false;
  545.         foreach ($lineItems as $lineItem) {
  546.             if (
  547.                 isset($lineItem->getPayload()['customFields']['imn_checkout_finish_page_url']) &&
  548.                 $lineItem->getPayload()['customFields']['imn_checkout_finish_page_url'] != ''
  549.             ) {
  550.                 $checkout_finish_page_url =
  551.                     'https://' $_SERVER['HTTP_HOST'] . 
  552.                     $lineItem->getPayload()['customFields']['imn_checkout_finish_page_url'];
  553.                 if ( ($_GET['et_sub'] ?? false) != '' ) {
  554.                     $checkout_finish_page_url $checkout_finish_page_url '?et_sub=' $_GET['et_sub'];
  555.                 } elseif ( ($_SESSION['moorl-order-notice.order_target_group_number'] ?? false) != '' ) {
  556.                     $checkout_finish_page_url $checkout_finish_page_url '?et_sub=' $_SESSION['moorl-order-notice.order_target_group_number'];
  557.                 }
  558.             }
  559.             if (
  560.                 isset($lineItem->getPayload()['customFields']['finish_page_parameter']) &&
  561.                 $lineItem->getPayload()['customFields']['finish_page_parameter'] != ''
  562.             ) {
  563.                 if (
  564.                     isset($_SERVER['QUERY_STRING']) &&
  565.                     $_SERVER['QUERY_STRING'] != '' &&
  566.                     !str_contains(
  567.                         $_SERVER['QUERY_STRING'],
  568.                         $lineItem->getPayload()['customFields']['finish_page_parameter'],
  569.                     )
  570.                 ) {
  571.                     $new_url =
  572.                         'finish?' .
  573.                         $_SERVER['QUERY_STRING'] .
  574.                         '&' .
  575.                         $lineItem->getPayload()['customFields']['finish_page_parameter'];
  576.                 } elseif ($_SERVER['QUERY_STRING'] == '') {
  577.                     $new_url 'finish?' $lineItem->getPayload()['customFields']['finish_page_parameter'];
  578.                 }
  579.                 if (isset($new_url)) {
  580.                     header('Location:' $new_url);
  581.                     exit();
  582.                 }
  583.             }
  584.             foreach ($lineItem->getPayload()['options'] as $option) {
  585.                 if ($option['group'] == 'Preisart') {
  586.                     if ($option['option'] == 'Ausbildungspreis') {
  587.                         $education_proof['need_education_proof'] = $option['option'];
  588.                     }
  589.                 }
  590.             }
  591.         }
  592.         $assign_arr['checkout_finish_page_url'] = $checkout_finish_page_url;
  593.         $event->getPage()->assign($assign_arr);
  594.         $event->getPage()->assign($education_proof);
  595.         $event->getPage()->assign($crossselling_return);
  596.         if (isset($order->getCustomFields()['order_notice_notice'])) {
  597.             $components['components'] = $order->getCustomFields()['order_notice_notice'];
  598.             $event->getPage()->assign($components);
  599.         }
  600.         $data = ['customer_id' => $customer->getCustomerId()];
  601.         // curl für Gastflag
  602.         $url =
  603.             'https://' $_SERVER['SERVER_NAME'] . '/customer_data_api/imn_nwb_get_customer_guest_flag.php';
  604.         $curl curl_init($url);
  605.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  606.         curl_setopt($curlCURLOPT_POSTtrue);
  607.         curl_setopt($curlCURLOPT_POSTFIELDS$data);
  608.         $guest_flag['guest'] = curl_exec($curl);
  609.         curl_close($curl);
  610.         // curl zum absenden der Order an nwb
  611.         //        $url = 'https://shop.nwb.de/_api/order_export.php';
  612.         //        $curl = curl_init($url);
  613.         //        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  614.         //        $result = curl_exec($curl);
  615.         //        curl_close($curl);
  616.         $event
  617.             ->getPage()
  618.             ->getOrder()
  619.             ->getOrderCustomer()
  620.             ->assign($guest_flag);
  621.         if (isset($_SESSION['bestellinformationen'])) {
  622.             $NextUrl['NextUrl'] = $_SESSION['bestellinformationen']->NextUrl;
  623.             $event->getPage()->assign($NextUrl);
  624.         }
  625.         $bestellinformationen['db_order_getter'] = '';
  626.         if (isset($_SESSION['bestellinformationen'])) {
  627.             $bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
  628.             $bestellinformationen['db_order_getter'] =
  629.                 '&titel=' .
  630.                 $_SESSION['bestellinformationen']->Titel .
  631.                 '&product_nr=' .
  632.                 $_SESSION['bestellinformationen']->Bestellnummer .
  633.                 '&next_url=' .
  634.                 $_SESSION['bestellinformationen']->NextUrl .
  635.                 '&document_id=' .
  636.                 $_SESSION['bestellinformationen']->DokumentenId;
  637.             if (isset($_GET['test']) && $_GET['test'] == 'doc') {
  638.                 echo '<pre>$bestellinformationen<br>';
  639.                 var_dump($bestellinformationen);
  640.                 echo '</pre>';
  641.             }
  642.             $event->getPage()->assign($bestellinformationen);
  643.         }
  644.         if (isset($_GET['test']) && $_GET['test'] == 'doc') {
  645.             $bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
  646.             echo '<pre>$bestellinformationen<br>';
  647.             var_dump($bestellinformationen);
  648.             echo '</pre>';
  649.             die();
  650.         }
  651.         if (!isset($_SESSION['prevent_order_export']) || $_SESSION['prevent_order_export'] === false) {
  652.             $_SESSION['prevent_order_export'] = true;
  653.             $url =
  654.                 'https://' .
  655.                 $_SERVER['SERVER_NAME'] .
  656.                 '/_api/order_export.php?order_id=' .
  657.                 $order->getId() .
  658.                 $bestellinformationen['db_order_getter'];
  659.             $url str_replace(' ''%20'$url);
  660.             $curl curl_init($url);
  661.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  662.             $result curl_exec($curl);
  663.             curl_close($curl);
  664.         }
  665.     }
  666.     public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event) {
  667.         $page $event->getPage();
  668.         $cart $page->getCart();
  669.         $lineitems $cart->getLineItems()->getFlat();
  670.         $cart_price $cart->getPrice();
  671.         $this->changeCartPriceForAboProducts($cart_price$lineitems);
  672.         $product_authors = [
  673.             'product_authors' => [],
  674.         ];
  675.         $crossselling_return get_cross_selling_items($lineitems);
  676.         foreach ($lineitems as $lineitem) {
  677.             $line_item_id $lineitem->getId();
  678.             $custom_fields $lineitem->getPayload()['customFields'];
  679.             if (
  680.                 isset($custom_fields['custom_product_attributs_autor_ids']) &&
  681.                 $custom_fields['custom_product_attributs_autor_ids'] != ''
  682.             ) {
  683.                 $result $this->connection->executeQuery(
  684.                     'SELECT * ' .
  685.                         'From cbax_lexicon_entry_translation ' .
  686.                         'WHERE attribute6 IN (' .
  687.                         $custom_fields['custom_product_attributs_autor_ids'] .
  688.                         ')',
  689.                 );
  690.                 $authors['authors'] = [];
  691.                 while ($row $result->fetch(PDO::FETCH_ASSOC)) {
  692.                     $authors[] = $row;
  693.                 }
  694.                 $event->getPage()->assign($authors);
  695.                 $product_authors['product_authors'][$line_item_id] = $authors;
  696.             }
  697.         }
  698.         $event->getPage()->assign($product_authors);
  699.         $event->getPage()->assign($crossselling_return);
  700.         if (isset($_SESSION['bestellinformationen'])) {
  701.             $bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
  702.             $event
  703.                 ->getPage()
  704.                 ->getCart()
  705.                 ->assign($bestellinformationen);
  706.         }
  707.     }
  708.     public function onOffcanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event) {
  709.         $page $event->getPage();
  710.         $cart $page->getCart();
  711.         $lineitems $cart->getLineItems()->getFlat();
  712.         $cart_price $cart->getPrice();
  713.         $this->changeCartPriceForAboProducts($cart_price$lineitems);
  714.     }
  715.     public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event) {
  716.     }
  717.     public function onAddressDetailPageLoaded(AddressDetailPageLoadedEvent $event) {
  718.     }
  719.     public function onAccountOverviewPageLoaded(AccountOverviewPageLoadedEvent $event) {
  720.         $cookieBisNameUser $event
  721.             ->getSalesChannelContext()
  722.             ->getSalesChannel()
  723.             ->getCustomFields()['cookie_bis'];
  724.         $cookieBisNameToBis $event
  725.             ->getSalesChannelContext()
  726.             ->getSalesChannel()
  727.             ->getCustomFields()['cookie_bis_sending'];
  728.         $nwbService $event
  729.             ->getSalesChannelContext()
  730.             ->getSalesChannel()
  731.             ->getCustomFields()['bis_url'];
  732.         if (isset($_SESSION['next_url'])) {
  733.             $next_url $_SESSION['next_url'];
  734.             if (isset($_COOKIE[$cookieBisNameUser]) && $_COOKIE[$cookieBisNameUser] != '') {
  735.                 $url 'https://' $nwbService '/customercare/api/accounts/info';
  736.                 $curl curl_init($url);
  737.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  738.                 curl_setopt($curlCURLOPT_COOKIE$cookieBisNameToBis '=' $_COOKIE[$cookieBisNameUser]);
  739.                 $response curl_exec($curl);
  740.                 curl_close($curl);
  741.                 $user_data json_decode($response);
  742.             }
  743.             $next_url str_replace('widgets/checkout/info''account'$next_url);
  744.             unset($_SESSION['next_url']);
  745.             header('Location: ' $next_url);
  746.             exit();
  747.         }
  748.     }
  749.     public function onLineItemAdded(LineItemAddedEvent $event): void {
  750.         $lineItem $event->getLineItem();
  751.         $lineItem->setLabel('Mein Custom Design');
  752.     }
  753.     private function changeCartPriceForAboProducts($cart_price$lineitems) {
  754.         $abo_price 0;
  755.         $abo_net 0;
  756.         $abo_prices = [
  757.             '7' => 0,
  758.             '19' => 0,
  759.         ];
  760.         $abo_tax = [
  761.             '7' => 0,
  762.             '19' => 0,
  763.         ];
  764.         if (count($lineitems) > 0) {
  765.             foreach ($lineitems as $lineitem) {
  766.                 if (
  767.                     isset($lineitem->getPayload()['customFields']['nwb_free_test_product']) &&
  768.                     $lineitem->getPayload()['customFields']['nwb_free_test_product']
  769.                 ) {
  770.                     $lineitem->getPrice()->getTotalPrice();
  771.                     $net 0;
  772.                     if (
  773.                         isset(
  774.                             $lineitem
  775.                                 ->getPrice()
  776.                                 ->getCalculatedTaxes()
  777.                                 ->getElements()[19],
  778.                         )
  779.                     ) {
  780.                         $abo_tax[19] =
  781.                             $abo_tax[19] +
  782.                             $lineitem
  783.                                 ->getPrice()
  784.                                 ->getCalculatedTaxes()
  785.                                 ->getElements()[19]
  786.                                 ->getTax();
  787.                         $abo_prices[19] = $abo_prices[19] + $lineitem->getPrice()->getTotalPrice();
  788.                         $net =
  789.                             $lineitem->getPrice()->getTotalPrice() -
  790.                             $lineitem
  791.                                 ->getPrice()
  792.                                 ->getCalculatedTaxes()
  793.                                 ->getElements()[19]
  794.                                 ->getTax();
  795.                     }
  796.                     if (
  797.                         isset(
  798.                             $lineitem
  799.                                 ->getPrice()
  800.                                 ->getCalculatedTaxes()
  801.                                 ->getElements()[7],
  802.                         )
  803.                     ) {
  804.                         $abo_tax[7] =
  805.                             $abo_tax[7] +
  806.                             $lineitem
  807.                                 ->getPrice()
  808.                                 ->getCalculatedTaxes()
  809.                                 ->getElements()[7]
  810.                                 ->getTax();
  811.                         $abo_prices[7] = $abo_prices[7] + $lineitem->getPrice()->getTotalPrice();
  812.                         $net =
  813.                             $lineitem->getPrice()->getTotalPrice() -
  814.                             $lineitem
  815.                                 ->getPrice()
  816.                                 ->getCalculatedTaxes()
  817.                                 ->getElements()[7]
  818.                                 ->getTax();
  819.                     }
  820.                     $abo_price $abo_price $lineitem->getPrice()->getTotalPrice();
  821.                     $abo_net $abo_net $net;
  822.                 }
  823.             }
  824.             if (isset($cart_price->getCalculatedTaxes()->getElements()[19])) {
  825.                 $old_tax $cart_price
  826.                     ->getCalculatedTaxes()
  827.                     ->getElements()[19]
  828.                     ->getTax();
  829.                 $old_price $cart_price
  830.                     ->getCalculatedTaxes()
  831.                     ->getElements()[19]
  832.                     ->getPrice();
  833.                 $new_cart_tax $old_tax $abo_tax[19];
  834.                 $new_price $old_price $abo_prices[19];
  835.                 $cart_price
  836.                     ->getCalculatedTaxes()
  837.                     ->getElements()[19]
  838.                     ->setTax($new_cart_tax);
  839.                 $cart_price
  840.                     ->getCalculatedTaxes()
  841.                     ->getElements()[19]
  842.                     ->setPrice($new_price);
  843.             }
  844.             if (isset($cart_price->getCalculatedTaxes()->getElements()[7])) {
  845.                 $old_tax $cart_price
  846.                     ->getCalculatedTaxes()
  847.                     ->getElements()[7]
  848.                     ->getTax();
  849.                 $old_price $cart_price
  850.                     ->getCalculatedTaxes()
  851.                     ->getElements()[7]
  852.                     ->getPrice();
  853.                 $new_cart_tax $old_tax $abo_tax[7];
  854.                 $new_price $old_price $abo_prices[7];
  855.                 $cart_price
  856.                     ->getCalculatedTaxes()
  857.                     ->getElements()[7]
  858.                     ->setTax($new_cart_tax);
  859.                 $cart_price
  860.                     ->getCalculatedTaxes()
  861.                     ->getElements()[7]
  862.                     ->setPrice($new_price);
  863.             }
  864.             $new_cart_price round($cart_price->getTotalPrice() - $abo_price2);
  865.             $new_raw_total_price round($cart_price->getRawTotal() - $abo_price2);
  866.             $new_position_price round($cart_price->getPositionPrice() - $abo_price2);
  867.             $new_cart_net $cart_price->getNetPrice() - floatval($abo_net);
  868.             if ($new_cart_net 0) {
  869.                 $new_cart_net 0;
  870.             }
  871.             $cart_price->assign([
  872.                 'netPrice' => $new_cart_net,
  873.                 'totalPrice' => $new_cart_price,
  874.                 'positionPrice' => $new_position_price,
  875.                 'totalAboPrice' => $abo_price,
  876.                 'rawTotal' => $new_raw_total_price,
  877.             ]);
  878.         }
  879.     }
  880.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event) {
  881.     }
  882. }
  883. function get_cross_selling_items($lineitems$is_finish_page false) {
  884.     $crossselling_return = [
  885.         'additional_items' => [],
  886.     ];
  887.     $line_item_ids = [];
  888.     foreach ($lineitems as $lineitem) {
  889.         if ($is_finish_page) {
  890.             $line_item_id $lineitem->getProductId();
  891.         } else {
  892.             $line_item_id $lineitem->getId();
  893.         }
  894.         $line_item_ids[] = $line_item_id;
  895.         if (!isset($_SESSION['cross_selling_items'][$line_item_id])) {
  896.             $url 'https://' $_SERVER['SERVER_NAME'] . '/product_api/get_cross_selling_by_prdouct_id.php';
  897.             $curl curl_init($url);
  898.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  899.             curl_setopt($curlCURLOPT_POSTtrue);
  900.             curl_setopt($curlCURLOPT_POSTFIELDS, ['line_item_id' => $line_item_id]);
  901.             $additional_items curl_exec($curl);
  902.             curl_close($curl);
  903.             $_SESSION['cross_selling_items'][$line_item_id] = json_decode($additional_items);
  904.             $crossselling_return['additional_items'][$line_item_id] = json_decode($additional_items);
  905.             if (isset($_GET['test']) && $_GET['test'] == 'additem') {
  906.                 echo '<pre>hier<br>';
  907.                 var_dump($lineitem);
  908.                 echo '</pre>';
  909.                 echo '<pre>hier<br>';
  910.                 var_dump($url);
  911.                 echo '</pre>';
  912.                 echo '<pre>hier<br>';
  913.                 var_dump($line_item_id);
  914.                 echo '</pre>';
  915.                 echo '<pre>hier<br>';
  916.                 var_dump($crossselling_return);
  917.                 echo '</pre>';
  918.                 die();
  919.             }
  920.         } else {
  921.             $crossselling_return['additional_items'][$line_item_id] =
  922.                 $_SESSION['cross_selling_items'][$line_item_id];
  923.         }
  924.     }
  925.     if (isset($_SESSION['cross_selling_items'])) {
  926.         foreach ($_SESSION['cross_selling_items'] as $item_id => $item) {
  927.             if (!in_array($item_id$line_item_ids)) {
  928.                 unset($_SESSION['cross_selling_items'][$item_id]);
  929.             }
  930.         }
  931.     }
  932.     if (isset($crossselling_return['additional_items'])) {
  933.         foreach ($crossselling_return['additional_items'] as $item_id => $additional_items) {
  934.             if (!is_null($additional_items)) {
  935.                 foreach ($additional_items as $add_item_id => $additional_item) {
  936.                     if (in_array($additional_item->id$line_item_ids)) {
  937.                         unset($crossselling_return['additional_items'][$item_id][$add_item_id]);
  938.                     }
  939.                 }
  940.             }
  941.         }
  942.     }
  943.     return $crossselling_return;
  944. }