96 lines
3.3 KiB
PHP
96 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\HannahBundle\Ordering;
|
|
|
|
use KupShop\AdminBundle\Util\ActivityLog;
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\OrderingBundle\Entity\Purchase\ProductPurchaseItem;
|
|
|
|
class Cart extends \KupShop\OrderingBundle\Cart
|
|
{
|
|
public function updateAddresses($invoice, $delivery)
|
|
{
|
|
$error = parent::updateAddresses($invoice, $delivery);
|
|
|
|
if (!$error) {
|
|
// fix delivery zip - napr. u Zasilkovny zip je ve formatu "543 01"
|
|
// a updateAddresses vrati chybu "PSČ nemá správný formát" kvuli mezere
|
|
if (!empty($this->delivery['zip']) && in_array($this->delivery['country'], ['CZ', 'SK'])) {
|
|
$this->delivery['zip'] = preg_replace('/[^0-9]/', '', $this->delivery['zip']);
|
|
}
|
|
// force check na fakturacni udaje - vyplnuje se komplet i u osobnich odberu
|
|
if ($error = $this->user->updateAddresses($invoice, $delivery)) {
|
|
addActivityLog(
|
|
ActivityLog::SEVERITY_ERROR,
|
|
ActivityLog::TYPE_COMMUNICATION,
|
|
'Nebyly vyplněny všechny potřebné údaje v košíku!',
|
|
['errorId' => $error]
|
|
);
|
|
}
|
|
}
|
|
|
|
return $error;
|
|
}
|
|
|
|
public function addCoupon($coupon)
|
|
{
|
|
// vyhodnoceni slev pro SK mutaci
|
|
if (Contexts::get(CurrencyContext::class)->getActiveId() !== 'CZK') {
|
|
return parent::addCoupon($coupon);
|
|
}
|
|
|
|
$coupon = trim($coupon);
|
|
|
|
$virtualProducts = array_filter($this->getPurchaseState()->getProducts(),
|
|
fn ($x) => $x instanceof ProductPurchaseItem && $x->getProduct()->isVirtual());
|
|
if ($virtualProducts) {
|
|
return false;
|
|
}
|
|
|
|
if (!empty($this->coupons[$coupon])) {
|
|
return true;
|
|
}
|
|
|
|
$this->coupons[$coupon] = $coupon;
|
|
|
|
$this->invalidatePurchaseState();
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function insertItemsFromPurchaseState(\Order $order): void
|
|
{
|
|
$this->withProductsOriginalTitles($order, function () use ($order) {
|
|
parent::insertItemsFromPurchaseState($order);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Tohle je kvuli barve v nazvu produktu - pro FE tu barvu v nazvu zobrazovat nechci, takze je predefinovanej ProductList
|
|
* kterej tu barvu dava pryc, ale v adminu ji k nazvu produktu v objednavce propsat chci, tak musim udelat tohle prohozeni.
|
|
*/
|
|
private function withProductsOriginalTitles(\Order $order, callable $callback): void
|
|
{
|
|
$titles = [];
|
|
// nastavim do product title hodnotu z originalTitle
|
|
foreach ($order->getPurchaseState()->getProducts() as $item) {
|
|
// ulozim si hodnotu v title
|
|
$titles[$item->getId()] = $item->getProduct()->title;
|
|
// do title hodim hodnotu z originalTitle
|
|
$item->getProduct()->title = $item->getProduct()->originalTitle ?? $item->getProduct()->title;
|
|
}
|
|
|
|
$callback();
|
|
|
|
// restornu to zpatky, aby v title byla puvodni hodnota
|
|
foreach ($order->getPurchaseState()->getProducts() as $item) {
|
|
if ($titles[$item->getId()] ?? false) {
|
|
$item->getProduct()->title = $titles[$item->getId()];
|
|
}
|
|
}
|
|
}
|
|
}
|