Last active
May 25, 2021 17:59
-
-
Save dzhuryn/3a197c5686ba75cfffeab20622a6a64a to your computer and use it in GitHub Desktop.
sale.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (empty($user)) { | |
return; | |
} | |
$totalPrice = $commerce->getCart()->getTotal(); | |
$sales = []; | |
$sale = $totalPrice * 20 / 100; | |
$sales['first_order'] = [ | |
'title' => 'Скидка за первый заказ 1', | |
'price' => -$sale, | |
'conflict' => ['birthday', 'pickup'], | |
'applied' => function () use ($user) { | |
return $user['orders_count'] == 0; | |
} | |
]; | |
$sale = $totalPrice * 20 / 100; | |
$sales['birthday'] = [ | |
'title' => 'Скидка за день рождения 2', | |
'price' => -$sale, | |
'conflict' => ['first_order', 'pickup', 'pizza_gift'], | |
'applied' => function () use ($user) { | |
$today = DateTime::createFromFormat('d.m.Y H:i:s', date('d.m.Y 00:00:00')); | |
$years = DateTime::createFromFormat('d.m.Y H:i:s', date('d.m.' . date('Y') . ' 00:00:00', strtotime($user['years']))); | |
$years2 = DateTime::createFromFormat('d.m.Y H:i:s', date('d.m.' . date('Y') . ' 00:00:00', strtotime($user['years']))); | |
return $years >= $today && $years->diff($today)->days <= 6 || $years2 >= $today && $years2->diff($today)->days <= 6; | |
} | |
]; | |
$sale = $totalPrice * 10 / 100; | |
$sales['pickup'] = [ | |
'title' => 'Скидка за замовывоз 3', | |
'price' => -$sale, | |
'conflict' => ['first_order', 'birthday'], | |
'applied' => function () use ($orderProcessor, $modx) { | |
return $orderProcessor->getCurrentDelivery() === 'pickup' && (isset($_REQUEST['hashes']['form']) || in_array($modx->documentIdentifier, [12, 4])); | |
} | |
]; | |
$sales['pizza_gift'] = [ | |
'title' => 'Пицца в подарок 4', | |
'price' => 0, | |
'conflict' => ['birthday'], | |
'applied' => function () use ($totalPrice) { | |
return $totalPrice > 1900; | |
}, | |
'resolve' => function () use ($modx) { | |
$commerce = $modx->commerce; | |
$cartManager = ci()->carts; | |
/** @var \Commerce\Carts\ProductsCart $productCart */ | |
$productCart = $cartManager->getCart('products'); | |
$products = $productCart->getItems(); | |
$hasSaleProduct = false; | |
foreach ($products as $product) { | |
if (isset($product['meta']['addedFromSale'])) { | |
$hasSaleProduct = true; | |
break; | |
} | |
} | |
if (!$hasSaleProduct) { | |
$id = 42; | |
$item = [ | |
'id' => $id, | |
'name' => $modx->runSnippet('DocInfo', ['field' => 'pagetitle', 'docid' => $id]), | |
'price' => 0, | |
'meta' => [ | |
'addedFromSale' => true | |
], | |
'count' => 1, | |
]; | |
$hash = $productCart->makeHash($item); | |
$row = $commerce->generateRandomString(16); | |
$item = array_merge($item, [ | |
'hash' => $hash, | |
'row' => $row | |
]); | |
$products[$row] = $item; | |
} | |
$productCart->setItems($products); | |
}, | |
'reject' => function () use ($modx) { | |
$commerce = $modx->commerce; | |
$cartManager = ci()->carts; | |
/** @var \Commerce\Carts\ProductsCart $productCart */ | |
$productCart = $cartManager->getCart('products'); | |
$products = $productCart->getItems(); | |
$saleProduct = false; | |
foreach ($products as $key => $product) { | |
if (isset($product['meta']['addedFromSale'])) { | |
$saleProduct = $key; | |
break; | |
} | |
} | |
if ($saleProduct !== false) { | |
unset($products[$saleProduct]); | |
$productCart->setItems($products); | |
} | |
} | |
]; | |
$appliedSales = []; | |
foreach ($sales as $key => $sale) { | |
if ($sale['applied']() === true) { | |
$appliedSales[$key] = $sale; | |
} else if (isset($sale['reject'])) { | |
$sale['reject'](); | |
} | |
} | |
$confirmedSales = []; | |
foreach ($appliedSales as $key => $sale) { | |
$conflict = false; | |
foreach ($sale['conflict'] as $saleKey) { | |
if (in_array($saleKey, array_keys($confirmedSales))) { | |
$conflict = true; | |
break; | |
} | |
} | |
if ($conflict === false) { | |
$confirmedSales[$key] = $sale; | |
if (isset($sale['resolve'])) { | |
$sale['resolve'](); | |
} | |
} else if (isset($sale['resolve'])) { | |
$sale['reject'](); | |
} | |
} | |
if ($modx->event->name === 'OnCollectSubtotals') { | |
foreach ($confirmedSales as $key => $confirmedSale) { | |
// добавляем строку с описанием скидки в раздел дополнительных сборов заказа | |
$params['rows'][$key] = [ | |
'title' => $confirmedSale['title'], | |
'price' => $confirmedSale['price'], | |
]; | |
// вычитаем размер скидки из итоговой суммы заказа | |
$params['total'] += $confirmedSale['price']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment