<?php declare(strict_types=1);
namespace imnxxNWBUserLogin\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Symfony\Component\HttpFoundation\Session\Session;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Doctrine\DBAL\Connection;
use PDO;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
class MySubscriber implements EventSubscriberInterface {
private CartService $cartService;
private Connection $connection;
public function __construct(CartService $cartService, Connection $connection) {
$this->cartService = $cartService;
$this->connection = $connection;
}
public static function getSubscribedEvents(): array {
return [
SearchPageLoadedEvent::class => 'onSearchPageLoaded',
GenericPageLoadedEvent::class => 'onPageLoaded',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoaded',
OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoaded',
AddressListingPageLoadedEvent::class => 'onAddressListingPageLoaded',
AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoaded',
AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoaded',
CustomerRegisterEvent::class => 'onCustomerRegisterEvent',
LineItemAddedEvent::class => 'onLineItemAdded',
CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
];
}
public function onCustomerRegisterEvent(CustomerRegisterEvent $event) {
if ($_POST['nwb_user_account']) {
$data = json_decode($_POST['nwb_user_account']);
// update user data - AccountID and SessionID
$url = 'https://' . $_SERVER['SERVER_NAME'] . '/login_api/imn_nwb_set_customer_account_id.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
unset($_SESSION['nwb_user_account']);
unset($_SESSION['test']);
}
}
public function onSearchPageLoaded(SearchPageLoadedEvent $event) {
}
public function onPageLoaded(GenericPageLoadedEvent $event) {
$nwbService = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['bis_url'];
$cookieBisNameUser = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis'];
if (isset($_GET['DokumentenId']) && $_GET['DokumentenId'] != '') {
if (!file_exists(dirname(__FILE__) . '/../document_logs/' . date('Y') . '/' . date('Y-m'))) {
mkdir(dirname(__FILE__) . '/../document_logs/' . date('Y') . '/' . date('Y-m'), 0777, true);
}
$t = microtime(true);
$micro = sprintf('%06d', ($t - floor($t)) * 1000000);
$documentIdLogName =
dirname(__FILE__) .
'/../document_logs/' .
date('Y') .
'/' .
date('Y-m') .
'/document_id_log_' .
date('Y-m-d') .
'.txt';
$documentLogContent =
"\n\n" .
'----------------------------------------+----------------------------------------+----------------------------------------' .
"\n";
$documentLogContent =
$documentLogContent .
"\n" .
date('Y-m-d H:i:s.', time()) .
$micro .
"\n" .
'Request documentid: ' .
$_GET['DokumentenId'] .
"\n";
if (
isset($_SESSION['_sf2_attributes']['sw-context-token']) &&
$_SESSION['_sf2_attributes']['sw-context-token'] != ''
) {
$this->cartService->deleteCart($event->getSalesChannelContext());
}
$url =
'https://' .
$nwbService .
'/customercare/api/order/getbestellinformationen?bisDocumentId=' .
$_GET['DokumentenId'];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response);
if ($json->Success) {
$json->Bestellinformationen->DokumentenId = $_GET['DokumentenId'];
$_SESSION['bestellinformationen'] = $json->Bestellinformationen;
$_SESSION['DokumentenId'] = $_GET['DokumentenId'];
setcookie('DokumentenId_productnumber_for_login', $_GET['DokumentenId']);
$add_product['nwb_add_product'] = [
'nwb_product_number' => $json->Bestellinformationen->Bestellnummer,
];
$event->getPage()->assign($add_product);
$_SESSION['nwb_add_product_next_url'] = '/checkout/confirm';
$documentLogContent =
$documentLogContent .
'After request DocumentId: ' .
$json->Bestellinformationen->DokumentenId .
"\n";
$logHandle = fopen($documentIdLogName, 'a+');
fwrite($logHandle, $documentLogContent);
fclose($logHandle);
unset($documentLogContent);
return true;
}
$documentLogContent =
$documentLogContent . 'After request DocumentId: ' . 'NO REQUEST MADE' . "\n";
$logHandle = fopen($documentIdLogName, 'a+');
fwrite($logHandle, $documentLogContent);
fclose($logHandle);
unset($documentLogContent);
}
if (
(!isset($_SESSION['GET_ADDRESSES']) ||
(!str_contains($_SERVER['REQUEST_URI'], '/checkout/') &&
!str_contains($_SERVER['REQUEST_URI'], '/account/'))) &&
!(isset($_SESSION['login_timestamp']) && $_SESSION['login_timestamp'] + 10 >= time())
) {
if (isset($_GET['test']) && $_GET['test'] == 'session') {
echo '<pre>$_SESSION<br>';
var_dump($_SESSION);
echo '</pre>';
die();
}
if (isset($_GET['url_test']) && $_GET['url_test'] == 'url_test') {
echo '<pre>$_SERVER<br>';
var_dump($_SERVER);
echo '</pre>';
die();
}
if (isset($_GET['logout']) && $_GET['logout'] == 'logout') {
$customer = $event->getSalesChannelContext()->getCustomer();
if ($customer != null) {
// Clear cart
$this->cartService->deleteCart($event->getSalesChannelContext());
}
if (isset($_SESSION['stay_on_confirm'])) {
unset($_SESSION['stay_on_confirm']);
}
header('Location: https://' . $_SERVER['SERVER_NAME'] . '/account/logout');
exit();
}
$customer = $event->getSalesChannelContext()->getCustomer();
// send partnerkonto back to confirm page - musste ausgeklammert werden wegen fehlern
// if( isset($_GET['test']) && $_GET['test'] == 'session') {
// echo('<pre>stay_on_confirm<br>');var_dump($_SESSION);echo('</pre>');
// }
// if( isset($_GET['test']) && $_GET['test'] == 'server') {
// echo('<pre>REQUEST_URI<br>');var_dump($_SERVER["REQUEST_URI"]);echo('</pre>');
// if(
// isset($_SESSION['stay_on_confirm']) &&
// $_SESSION['stay_on_confirm'] == 'true' &&
// !str_contains($_SERVER["REQUEST_URI"], '/checkout/confirm') &&
// !str_contains($_SERVER["REQUEST_URI"], '/checkout/finish') &&
// $customer != null
// ) {
// echo('thats true!');
// }
// echo('is it true?');
// }
// if(
// isset($_SESSION['stay_on_confirm']) &&
// $_SESSION['stay_on_confirm'] == 'true' &&
// !str_contains($_SERVER["REQUEST_URI"], '/checkout/confirm') &&
// !str_contains($_SERVER["REQUEST_URI"], '/checkout/finish') &&
// $customer != null
// ) {
// header('Location: https://shop.nwb.de/checkout/confirm/');
// exit;
// }
// if(
// isset($_SESSION['stay_on_confirm']) &&
// $_SESSION['stay_on_confirm'] == 'true' &&
// $_SERVER["REQUEST_URI"] != "/checkout/finish" &&
// $customer != null
// ) {
// header('Location: https://shop.nwb.de/checkout/confirm/');
// exit;
//Test
// }
if (!is_null($event->getPage()->getHeader())) {
$active_category = $event
->getPage()
->getHeader()
->getNavigation()
->getActive();
if (isset($active_category->getCustomFields()['hide_lp_header'])) {
$hide_lp_header['hide_lp_header'] = $active_category->getCustomFields()['hide_lp_header'];
$event->getPage()->assign($hide_lp_header);
}
if (isset($active_category->getCustomFields()['navi_entry_point'])) {
$navi_entry_point['navi_entry_point'] = $active_category->getCustomFields()[
'navi_entry_point'
];
$event->getPage()->assign($navi_entry_point);
}
}
if (isset($_GET['send']) && $_GET['send'] == 'success') {
$landingpage_form_send['landingpage_form_send'] = 'success';
$event->getPage()->assign($landingpage_form_send);
}
$recently_ordered['recently_ordered'] = false;
if (isset($_SESSION['checkout_finish_timestamp'])) {
if (time() < $_SESSION['checkout_finish_timestamp'] + 3 * 60) {
$recently_ordered['recently_ordered'] = true;
} else {
unset($_SESSION['checkout_finish_timestamp']);
}
}
$event->getPage()->assign($recently_ordered);
$old_browser['old_browser'] = 0;
if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Edge') !== false ||
strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false
) {
$old_browser['old_browser'] = 1;
}
$event->getPage()->assign($old_browser);
if (isset($_GET['et_sub']) && $_GET['et_sub'] != '') {
$_SESSION['moorl-order-notice.order_target_group_number'] = $_GET['et_sub'];
$session = new Session();
$session->set('moorl-order-notice.order_target_group_number', $_GET['et_sub']);
}
if (
isset($_SESSION['moorl-order-notice.order_target_group_number']) &&
$_SESSION['moorl-order-notice.order_target_group_number'] != ''
) {
$et_sub['et_sub'] = $_SESSION['moorl-order-notice.order_target_group_number'];
$event->getPage()->assign($et_sub);
if (
(!isset($_GET['et_sub']) || $_GET['et_sub'] == '') &&
!str_contains($_SERVER['REQUEST_URI'], '/checkout/confirm') &&
!str_contains($_SERVER['REQUEST_URI'], '/checkout/finish') &&
!str_contains($_SERVER['REQUEST_URI'], '/suggest') &&
!str_contains($_SERVER['REQUEST_URI'], '/widget')
) {
$header_location = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if( str_contains($_SERVER['REQUEST_URI'],'?') ) {
$request_url_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
if($request_url_parts[1]!='') {
$header_location = $header_location . '&et_sub=' . $et_sub['et_sub'];
} else {
$header_location = $header_location . 'et_sub=' . $et_sub['et_sub'];
}
} else {
$header_location = $header_location . '?et_sub=' . $et_sub['et_sub'];
}
header('Location: ' . $header_location );
die();
}
}
if (isset($_GET['product_to_cart']) && $_GET['product_to_cart'] != '') {
$add_product['nwb_add_product'] = [
'nwb_product_number' => $_GET['product_to_cart'],
];
$event->getPage()->assign($add_product);
}
if (isset($_SESSION['logout_redirect']) && $_SESSION['logout_redirect'] != '') {
$logout_redirect = $_SESSION['logout_redirect'];
unset($_SESSION['logout_redirect']);
unset($_SESSION['login_timestamp']);
header('Location: ' . $logout_redirect);
exit();
}
if (
!isset($_COOKIE[$cookieBisNameUser]) &&
isset($_COOKIE['sw-states']) &&
preg_match('/logged-in/i', $_COOKIE['sw-states']) &&
$customer !== null &&
!$customer->getGuest()
) {
$_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
header('Location: https://' . $_SERVER['SERVER_NAME'] . '/account/logout');
exit();
}
}
}
public function onProductPageLoaded(ProductPageLoadedEvent $event) {
$page = $event->getPage();
$product = $page->getProduct();
if (isset($_GET['testen']) && $_GET['testen'] == 'testen') {
$cross_selling_elements = $page->getCrossSellings()->getElements();
foreach ($cross_selling_elements as $cross_selling_element) {
echo '<pre>$cross_selling_element<br>';
print_r($cross_selling_element->getCrossSelling());
echo '</pre>';
}
// echo('<pre>product<br>');print_r($product);echo('</pre>');
}
$custom_fields = $product->getCustomFields();
// todo: get varianten price
// echo('<pre>');print_r($_SERVER);echo('</pre>');
// echo('<pre>');print_r($product->getParentId());echo('</pre>');
// if( $product->getParentId() != '' ) {
// $url = '/product_api/get_products_by_parent_id.php';
// $curl = curl_init($url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($curl);
// echo('<pre>response');var_dump($response);echo('</pre>');
// }
$education_proof['need_education_proof'] = false;
foreach ($product->getOptions()->getElements() as $option) {
if ($option->getGroup()->getName() == 'Preisart') {
if ($option->getName() == 'Ausbildungspreis') {
$education_proof['need_education_proof'] = $option->getName();
}
}
}
$event->getPage()->assign($education_proof);
//wurde ausgelagert ins authorProductList Plugin
if (
isset($custom_fields['custom_product_attributs_autor_ids']) &&
$custom_fields['custom_product_attributs_autor_ids'] != ''
) {
$result = $this->connection->executeQuery(
'SELECT *, HEX(cbax_lexicon_entry.id) as author_sw_id ' .
'From cbax_lexicon_entry_translation ' .
'INNER JOIN cbax_lexicon_entry ON cbax_lexicon_entry_translation.cbax_lexicon_entry_id=cbax_lexicon_entry.id ' .
'WHERE cbax_lexicon_entry_translation.attribute6 IN (' .
$custom_fields['custom_product_attributs_autor_ids'] .
')',
);
$unsorted_authors = [];
$authors['authors'] = [];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$unsorted_authors[$row['attribute6']] = $row;
}
$author_ids = explode(',', $custom_fields['custom_product_attributs_autor_ids']);
foreach ($author_ids as $author_id) {
if(isset($unsorted_authors[$author_id]))
$authors['authors'][$author_id] = $unsorted_authors[$author_id];
}
if (isset($_GET['test']) && $_GET['test'] == 'author') {
echo '<pre>$authors<br>';
print_r($authors);
echo '</pre>';
}
$event->getPage()->assign($authors);
}
}
public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event) {
$page = $event->getPage();
$cart = $page->getCart();
$lineitems = $cart->getLineItems()->getFlat();
$cart_price = $cart->getPrice();
$this->changeCartPriceForAboProducts($cart_price, $lineitems);
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event) {
$cookieBisNameUser = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis'];
$cookieBisNameToBis = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis_sending'];
$nwbService = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['bis_url'];
if (isset($_COOKIE[$cookieBisNameUser]) && $_COOKIE[$cookieBisNameUser] != '') {
$url = 'https://' . $nwbService . '/customercare/api/accounts/info';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIE, $cookieBisNameToBis . '=' . $_COOKIE[$cookieBisNameUser]);
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
$user_data = json_decode($response);
if (isset($_SESSION['dokument_kauf']) && isset($user_data->Partnerkonto)) {
if ($user_data->Partnerkonto && $_SESSION['dokument_kauf'] == 'true') {
$_SESSION['stay_on_confirm'] = 'true';
unset($_SESSION['dokument_kauf']);
}
}
}
$page = $event->getPage();
$cart = $page->getCart();
//musste ebenfalls ausgeklammert werden wegen fehler
// if( isset($_SESSION['stay_on_confirm']) && $_SESSION['stay_on_confirm'] == 'true' ) {
// $event->getPage()->assign( ['stay_on_confirm' => true] );
// }
//
// if( isset($_GET['stay_on_confirm']) && $_GET['stay_on_confirm'] == 'true' ) {
// $event->getPage()->assign( ['stay_on_confirm' => true] );
// }
$lineitems = $cart->getLineItems()->getFlat();
$cart_price = $cart->getPrice();
$this->changeCartPriceForAboProducts($cart_price, $lineitems);
$crossselling_return = get_cross_selling_items($lineitems);
$education_proof['need_education_proof'] = false;
$download_products = [];
$hasDocumentIdProductInCart = false;
$libreka_download_links = '';
foreach ($lineitems as $lineitem) {
if (
isset($lineitem->getPayload()['customFields']['custom_product_attributs_dokumenten_id']) &&
$lineitem->getPayload()['customFields']['custom_product_attributs_dokumenten_id'] != ''
) {
$hasDocumentIdProductInCart = true;
}
if (
isset($lineitem->getPayload()['customFields']['custom_product_attributs_email_gruppe']) &&
$lineitem->getPayload()['customFields']['custom_product_attributs_email_gruppe'] ==
'E-Book-Email-Gruppe'
) {
if (
$lineitem->getPayload()['customFields'][
'custom_product_attributs_libreka_download_format'
] == 'EPUB'
) {
$librekaDownloadFormat = '029';
} elseif (
$lineitem->getPayload()['customFields'][
'custom_product_attributs_libreka_download_format'
] == 'PDF'
) {
$librekaDownloadFormat = '002';
}
$url =
'https://' .
$nwbService .
'/customercare/api/order/GetLibrekaDownloadLink/?accountId=' .
$user_data->AccountID .
'&isbn=' .
$lineitem->getPayload()['customFields']['custom_product_attributs_isbn'] .
'&librekaDownloadFormat=' .
$librekaDownloadFormat .
'&idBestellposition=11';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
if ($libreka_download_links != '') {
$libreka_download_links = $libreka_download_links . '|';
}
$libreka_download_links =
$libreka_download_links .
$lineitem->getPayload()['productNumber'] .
'=>' .
json_decode($response)->Url;
}
foreach ($lineitem->getPayload()['options'] as $option) {
if ($option['group'] == 'Preisart') {
if ($option['option'] == 'Ausbildungspreis') {
$education_proof['need_education_proof'] = $option['option'];
}
}
}
}
$_SESSION['prevent_order_export'] = false;
$session = new Session();
$session->set('moorl-order-notice.libreka_download_links', $libreka_download_links);
$advertising_permission['advertising_permission'] = $session->get(
'moorl-order-notice.advertising_permission',
);
if (is_null($advertising_permission['advertising_permission'])) {
$advertising_permission['advertising_permission'] = false;
}
$event->getPage()->assign($advertising_permission);
$event->getPage()->assign($education_proof);
$event->getPage()->assign($crossselling_return);
// if(!$hasDocumentIdProductInCart && isset($_SESSION['bestellinformationen'])) {
// unset($_SESSION['bestellinformationen']);
// }
if (isset($_SESSION['bestellinformationen'])) {
$bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
$event->getPage()->assign($bestellinformationen);
$session = new Session();
$session->set('moorl-order-notice.bestellinformationen', $_SESSION['bestellinformationen']);
}
}
public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event) {
if (isset($_SESSION['bestellinformationen'])) {
$orderProducts = $event
->getPage()
->getOrder()
->getLineItems();
$documentProductInOrder = false;
/**
* @var OrderLineItemEntity $product
*/
foreach ($orderProducts as $product) {
if (
$_SESSION['bestellinformationen']->Bestellnummer ==
$product->getPayload()['productNumber']
) {
$documentProductInOrder = true;
break;
}
}
if (!$documentProductInOrder) {
unset($_SESSION['bestellinformationen']);
}
}
// session setzen zum logout link ausblenden
$_SESSION['checkout_finish_timestamp'] = time();
$page = $event->getPage();
$order = $page->getOrder();
$customer = $order->getOrderCustomer();
$order_price = $order->getPrice();
$lineItems = $order->getLineItems()->getElements();
$this->changeCartPriceForAboProducts($order_price, $lineItems);
$crossselling_return = get_cross_selling_items($lineItems, true);
if (isset($_GET['test']) && $_GET['test'] == 'additem') {
echo '<pre>$crossselling_return<br>';
var_dump($crossselling_return);
echo '</pre>';
die();
}
$checkout_finish_page_url = '';
$education_proof['need_education_proof'] = false;
foreach ($lineItems as $lineItem) {
if (
isset($lineItem->getPayload()['customFields']['imn_checkout_finish_page_url']) &&
$lineItem->getPayload()['customFields']['imn_checkout_finish_page_url'] != ''
) {
$checkout_finish_page_url =
'https://' . $_SERVER['HTTP_HOST'] .
$lineItem->getPayload()['customFields']['imn_checkout_finish_page_url'];
if ( ($_GET['et_sub'] ?? false) != '' ) {
$checkout_finish_page_url = $checkout_finish_page_url . '?et_sub=' . $_GET['et_sub'];
} elseif ( ($_SESSION['moorl-order-notice.order_target_group_number'] ?? false) != '' ) {
$checkout_finish_page_url = $checkout_finish_page_url . '?et_sub=' . $_SESSION['moorl-order-notice.order_target_group_number'];
}
}
if (
isset($lineItem->getPayload()['customFields']['finish_page_parameter']) &&
$lineItem->getPayload()['customFields']['finish_page_parameter'] != ''
) {
if (
isset($_SERVER['QUERY_STRING']) &&
$_SERVER['QUERY_STRING'] != '' &&
!str_contains(
$_SERVER['QUERY_STRING'],
$lineItem->getPayload()['customFields']['finish_page_parameter'],
)
) {
$new_url =
'finish?' .
$_SERVER['QUERY_STRING'] .
'&' .
$lineItem->getPayload()['customFields']['finish_page_parameter'];
} elseif ($_SERVER['QUERY_STRING'] == '') {
$new_url = 'finish?' . $lineItem->getPayload()['customFields']['finish_page_parameter'];
}
if (isset($new_url)) {
header('Location:' . $new_url);
exit();
}
}
foreach ($lineItem->getPayload()['options'] as $option) {
if ($option['group'] == 'Preisart') {
if ($option['option'] == 'Ausbildungspreis') {
$education_proof['need_education_proof'] = $option['option'];
}
}
}
}
$assign_arr['checkout_finish_page_url'] = $checkout_finish_page_url;
$event->getPage()->assign($assign_arr);
$event->getPage()->assign($education_proof);
$event->getPage()->assign($crossselling_return);
if (isset($order->getCustomFields()['order_notice_notice'])) {
$components['components'] = $order->getCustomFields()['order_notice_notice'];
$event->getPage()->assign($components);
}
$data = ['customer_id' => $customer->getCustomerId()];
// curl für Gastflag
$url =
'https://' . $_SERVER['SERVER_NAME'] . '/customer_data_api/imn_nwb_get_customer_guest_flag.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$guest_flag['guest'] = curl_exec($curl);
curl_close($curl);
// curl zum absenden der Order an nwb
// $url = 'https://shop.nwb.de/_api/order_export.php';
// $curl = curl_init($url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// $result = curl_exec($curl);
// curl_close($curl);
$event
->getPage()
->getOrder()
->getOrderCustomer()
->assign($guest_flag);
if (isset($_SESSION['bestellinformationen'])) {
$NextUrl['NextUrl'] = $_SESSION['bestellinformationen']->NextUrl;
$event->getPage()->assign($NextUrl);
}
$bestellinformationen['db_order_getter'] = '';
if (isset($_SESSION['bestellinformationen'])) {
$bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
$bestellinformationen['db_order_getter'] =
'&titel=' .
$_SESSION['bestellinformationen']->Titel .
'&product_nr=' .
$_SESSION['bestellinformationen']->Bestellnummer .
'&next_url=' .
$_SESSION['bestellinformationen']->NextUrl .
'&document_id=' .
$_SESSION['bestellinformationen']->DokumentenId;
if (isset($_GET['test']) && $_GET['test'] == 'doc') {
echo '<pre>$bestellinformationen<br>';
var_dump($bestellinformationen);
echo '</pre>';
}
$event->getPage()->assign($bestellinformationen);
}
if (isset($_GET['test']) && $_GET['test'] == 'doc') {
$bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
echo '<pre>$bestellinformationen<br>';
var_dump($bestellinformationen);
echo '</pre>';
die();
}
if (!isset($_SESSION['prevent_order_export']) || $_SESSION['prevent_order_export'] === false) {
$_SESSION['prevent_order_export'] = true;
$url =
'https://' .
$_SERVER['SERVER_NAME'] .
'/_api/order_export.php?order_id=' .
$order->getId() .
$bestellinformationen['db_order_getter'];
$url = str_replace(' ', '%20', $url);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
}
}
public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event) {
$page = $event->getPage();
$cart = $page->getCart();
$lineitems = $cart->getLineItems()->getFlat();
$cart_price = $cart->getPrice();
$this->changeCartPriceForAboProducts($cart_price, $lineitems);
$product_authors = [
'product_authors' => [],
];
$crossselling_return = get_cross_selling_items($lineitems);
foreach ($lineitems as $lineitem) {
$line_item_id = $lineitem->getId();
$custom_fields = $lineitem->getPayload()['customFields'];
if (
isset($custom_fields['custom_product_attributs_autor_ids']) &&
$custom_fields['custom_product_attributs_autor_ids'] != ''
) {
$result = $this->connection->executeQuery(
'SELECT * ' .
'From cbax_lexicon_entry_translation ' .
'WHERE attribute6 IN (' .
$custom_fields['custom_product_attributs_autor_ids'] .
')',
);
$authors['authors'] = [];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$authors[] = $row;
}
$event->getPage()->assign($authors);
$product_authors['product_authors'][$line_item_id] = $authors;
}
}
$event->getPage()->assign($product_authors);
$event->getPage()->assign($crossselling_return);
if (isset($_SESSION['bestellinformationen'])) {
$bestellinformationen['bestellinformationen'] = $_SESSION['bestellinformationen'];
$event
->getPage()
->getCart()
->assign($bestellinformationen);
}
}
public function onOffcanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event) {
$page = $event->getPage();
$cart = $page->getCart();
$lineitems = $cart->getLineItems()->getFlat();
$cart_price = $cart->getPrice();
$this->changeCartPriceForAboProducts($cart_price, $lineitems);
}
public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event) {
}
public function onAddressDetailPageLoaded(AddressDetailPageLoadedEvent $event) {
}
public function onAccountOverviewPageLoaded(AccountOverviewPageLoadedEvent $event) {
$cookieBisNameUser = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis'];
$cookieBisNameToBis = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis_sending'];
$nwbService = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['bis_url'];
if (isset($_SESSION['next_url'])) {
$next_url = $_SESSION['next_url'];
if (isset($_COOKIE[$cookieBisNameUser]) && $_COOKIE[$cookieBisNameUser] != '') {
$url = 'https://' . $nwbService . '/customercare/api/accounts/info';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIE, $cookieBisNameToBis . '=' . $_COOKIE[$cookieBisNameUser]);
$response = curl_exec($curl);
curl_close($curl);
$user_data = json_decode($response);
}
$next_url = str_replace('widgets/checkout/info', 'account', $next_url);
unset($_SESSION['next_url']);
header('Location: ' . $next_url);
exit();
}
}
public function onLineItemAdded(LineItemAddedEvent $event): void {
$lineItem = $event->getLineItem();
$lineItem->setLabel('Mein Custom Design');
}
private function changeCartPriceForAboProducts($cart_price, $lineitems) {
$abo_price = 0;
$abo_net = 0;
$abo_prices = [
'7' => 0,
'19' => 0,
];
$abo_tax = [
'7' => 0,
'19' => 0,
];
if (count($lineitems) > 0) {
foreach ($lineitems as $lineitem) {
if (
isset($lineitem->getPayload()['customFields']['nwb_free_test_product']) &&
$lineitem->getPayload()['customFields']['nwb_free_test_product']
) {
$lineitem->getPrice()->getTotalPrice();
$net = 0;
if (
isset(
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[19],
)
) {
$abo_tax[19] =
$abo_tax[19] +
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[19]
->getTax();
$abo_prices[19] = $abo_prices[19] + $lineitem->getPrice()->getTotalPrice();
$net =
$lineitem->getPrice()->getTotalPrice() -
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[19]
->getTax();
}
if (
isset(
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[7],
)
) {
$abo_tax[7] =
$abo_tax[7] +
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[7]
->getTax();
$abo_prices[7] = $abo_prices[7] + $lineitem->getPrice()->getTotalPrice();
$net =
$lineitem->getPrice()->getTotalPrice() -
$lineitem
->getPrice()
->getCalculatedTaxes()
->getElements()[7]
->getTax();
}
$abo_price = $abo_price + $lineitem->getPrice()->getTotalPrice();
$abo_net = $abo_net + $net;
}
}
if (isset($cart_price->getCalculatedTaxes()->getElements()[19])) {
$old_tax = $cart_price
->getCalculatedTaxes()
->getElements()[19]
->getTax();
$old_price = $cart_price
->getCalculatedTaxes()
->getElements()[19]
->getPrice();
$new_cart_tax = $old_tax - $abo_tax[19];
$new_price = $old_price - $abo_prices[19];
$cart_price
->getCalculatedTaxes()
->getElements()[19]
->setTax($new_cart_tax);
$cart_price
->getCalculatedTaxes()
->getElements()[19]
->setPrice($new_price);
}
if (isset($cart_price->getCalculatedTaxes()->getElements()[7])) {
$old_tax = $cart_price
->getCalculatedTaxes()
->getElements()[7]
->getTax();
$old_price = $cart_price
->getCalculatedTaxes()
->getElements()[7]
->getPrice();
$new_cart_tax = $old_tax - $abo_tax[7];
$new_price = $old_price - $abo_prices[7];
$cart_price
->getCalculatedTaxes()
->getElements()[7]
->setTax($new_cart_tax);
$cart_price
->getCalculatedTaxes()
->getElements()[7]
->setPrice($new_price);
}
$new_cart_price = round($cart_price->getTotalPrice() - $abo_price, 2);
$new_raw_total_price = round($cart_price->getRawTotal() - $abo_price, 2);
$new_position_price = round($cart_price->getPositionPrice() - $abo_price, 2);
$new_cart_net = $cart_price->getNetPrice() - floatval($abo_net);
if ($new_cart_net < 0) {
$new_cart_net = 0;
}
$cart_price->assign([
'netPrice' => $new_cart_net,
'totalPrice' => $new_cart_price,
'positionPrice' => $new_position_price,
'totalAboPrice' => $abo_price,
'rawTotal' => $new_raw_total_price,
]);
}
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event) {
}
}
function get_cross_selling_items($lineitems, $is_finish_page = false) {
$crossselling_return = [
'additional_items' => [],
];
$line_item_ids = [];
foreach ($lineitems as $lineitem) {
if ($is_finish_page) {
$line_item_id = $lineitem->getProductId();
} else {
$line_item_id = $lineitem->getId();
}
$line_item_ids[] = $line_item_id;
if (!isset($_SESSION['cross_selling_items'][$line_item_id])) {
$url = 'https://' . $_SERVER['SERVER_NAME'] . '/product_api/get_cross_selling_by_prdouct_id.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, ['line_item_id' => $line_item_id]);
$additional_items = curl_exec($curl);
curl_close($curl);
$_SESSION['cross_selling_items'][$line_item_id] = json_decode($additional_items);
$crossselling_return['additional_items'][$line_item_id] = json_decode($additional_items);
if (isset($_GET['test']) && $_GET['test'] == 'additem') {
echo '<pre>hier<br>';
var_dump($lineitem);
echo '</pre>';
echo '<pre>hier<br>';
var_dump($url);
echo '</pre>';
echo '<pre>hier<br>';
var_dump($line_item_id);
echo '</pre>';
echo '<pre>hier<br>';
var_dump($crossselling_return);
echo '</pre>';
die();
}
} else {
$crossselling_return['additional_items'][$line_item_id] =
$_SESSION['cross_selling_items'][$line_item_id];
}
}
if (isset($_SESSION['cross_selling_items'])) {
foreach ($_SESSION['cross_selling_items'] as $item_id => $item) {
if (!in_array($item_id, $line_item_ids)) {
unset($_SESSION['cross_selling_items'][$item_id]);
}
}
}
if (isset($crossselling_return['additional_items'])) {
foreach ($crossselling_return['additional_items'] as $item_id => $additional_items) {
if (!is_null($additional_items)) {
foreach ($additional_items as $add_item_id => $additional_item) {
if (in_array($additional_item->id, $line_item_ids)) {
unset($crossselling_return['additional_items'][$item_id][$add_item_id]);
}
}
}
}
}
return $crossselling_return;
}