Files
shop-old/modules/website_shoppingcart.php
T

153 lines
4.7 KiB
PHP

<?php
/*
* @package Easyway Shop 5.5
* @copyright Copyright (C) 2005 - 2012 TA-EDV
* @license proprietary
* @author Richard Kammermayer <rk@ta-edv.de>,
* John T. Daly <jd@ta-edv.de>
*
* Easyway Shop is a web e-commerce system
*/
include_once('./core/shoppingcart.class.php');
class website_shoppingcart {
private $base_object;
private $layout_object;
private $item_object;
public function __construct($base_object, $layout_object) {
$this->base_object = $base_object;
$this->layout_object = $layout_object;
$this->item_object = new Item($base_object);
$this->shoppingcart_object = new Shoppingcart($base_object);
}
function run() {
$action = '';
if (isset($_POST['update_item']) && $_POST['update_item']) {
$action = 'update_shoppingcart';
} elseif (isset($_POST['amount']) && $_POST['amount']) {
$action = 'add_item';
}
if ($action == 'update_shoppingcart') {
$this->update_shoppingcart();
} elseif ($action == 'add_item') {
$this->add_item();
}
return $this->show_shoppingcart();
} // end run
private function show_shoppingcart() {
if (isset($_POST['customer_info'])) {
$_SESSION['customer_info'] = trim((string)$_POST['customer_info']);
}
// 18.09.2026 -- Warenkorb ist EIN Formular: "Zur Kasse" und die Express-Knoepfe schicken
// es ab (Mengen + Anmerkung werden so mitgespeichert) und das Ziel steht in cart_goto.
if (isset($_POST['cart_goto']) && $_POST['cart_goto'] !== '') {
$goto = (string)$_POST['cart_goto'];
if ($goto === 'kasse') {
header('Location: /Kasse/');
exit;
}
if (preg_match('/^express:([a-z0-9_]{1,40})$/', $goto, $m)) {
header('Location: /Kasse/?payment_express_start='.$m[1]);
exit;
}
}
// Post-Redirect-Get fuer die neue Ansicht: nach Mengen-/Anmerkungsaenderung per GET neu
// laden, sonst fragt ein Reload "Formular erneut senden" und schickt die Mengen doppelt.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cart_goto']) && isset($_GET['ansicht']) && $_GET['ansicht'] === 'neu') {
header('Location: /Warenkorb/?ansicht=neu');
exit;
}
if (isset($_SESSION['customer_info']) && $_SESSION['customer_info']) {
$this->layout_object->assign('customer_info', $_SESSION['customer_info']);
}
$data = $this->shoppingcart_object->get_data();
$this->layout_object->assign('shoppingcart', $data);
$this->layout_object->assign('shoppingcartVATUsage', $this->base_object->customer_group->show_tax);
// 14.09.2026: Express-Buttons der Zahlungsanbieter ("Direkt mit PayPal bezahlen").
// Angezeigt, wenn der Anbieter Express kann und seine Zahlart aktiv ist.
$this->layout_object->assign('express_providers', $this->express_providers());
// 18.09.2026 -- Entwurf der neuen Warenkorb-Ansicht zum Anschauen: /Warenkorb/?ansicht=neu
if (isset($_GET['ansicht']) && $_GET['ansicht'] === 'neu') {
return $this->layout_object->_fetch('content_shoppingcart_neu.tpl');
}
return $this->layout_object->_fetch('content_shoppingcart.tpl');
} // end show_shoppingcart
/** @return array<int,array{module:string,label:string}> */
private function express_providers() {
$out = array();
$file = $_SERVER['DOCUMENT_ROOT'].'/core/payments/Registry.php';
if (!is_file($file)) {
return $out;
}
include_once $file;
$res = $this->base_object->db->query("SELECT module FROM payment_methods WHERE active=1 AND module<>''");
while ($res && ($row = $res->fetch_object())) {
$module = (string)$row->module;
if (!\Payments\Registry::has($module)) {
continue;
}
try {
$p = \Payments\Registry::get($module, $this->base_object);
if ($p && $p->supportsExpress()) {
$out[] = array('module' => $module, 'label' => $p->label());
}
} catch (Throwable $e) {
error_log('Payments express button '.$module.': '.$e->getMessage());
}
}
return $out;
}
private function update_shoppingcart() {
foreach ($_POST['update_item'] as $item => $quantity) {
if ($quantity > 0) {
$this->shoppingcart_object->update($item, $quantity);
} else {
$this->shoppingcart_object->remove($item);
}
}
} // end update_shoppingcart
private function add_item() {
$this->shoppingcart_object->add($_POST['item_id'], $_POST['amount'], $_POST['variant'][1], $_POST['variant'][2]);
$multiplier = 1;
if ($this->item_object->is_combination_item()) {
$multiplier = (int)$_POST['amount'];
}
if (is_array($_POST['additionalItems'])) {
foreach ($_POST['additionalItems'] as $addItem) {
if (is_array($addItem)) {
$this->shoppingcart_object->add($addItem['id'], ((int)$_POST['amount'] * $multiplier), $addItem['variant_1'], $addItem['variant_2']);
} else {
$this->shoppingcart_object->add($addItem, 1);
}
}
}
} // end add_item
} // end website_shoppingcart
/* EOF */