Init project
Future fixes: -Remove logic for showing current order reference in ajax.js. Instead grab from database using cart ID
This commit is contained in:
63
controllers/front/ajax.php
Normal file
63
controllers/front/ajax.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Module\WsOrderreference\Entity\OrderReference;
|
||||
use Module\WsOrderreference\Repository\OrderReferenceRepository;
|
||||
|
||||
if (file_exists(__DIR__ . '../../vendor/autoload.php')) {
|
||||
require_once __DIR__ . '../../vendor/autoload.php';
|
||||
}
|
||||
|
||||
class ws_orderreferenceAjaxModuleFrontController extends ModuleFrontController
|
||||
{
|
||||
public function initContent()
|
||||
{
|
||||
$orderReference = Tools::getValue('order_reference_input');
|
||||
$idCart = $this->context->cart->id;
|
||||
|
||||
if (!$idCart) {
|
||||
$this->ajaxRender(json_encode([
|
||||
'success' => false,
|
||||
'message' => $this->module->l('No cart found.', 'ajax')
|
||||
]));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!preg_match('/^[a-zA-Z0-9_\- ]+$/', $orderReference)) {
|
||||
$this->ajaxRender(json_encode([
|
||||
'success' => false,
|
||||
'message' => $this->module->l('Please enter a valid order reference. Characters allowed: a-z, 0-9, -, _ and space', 'ajax')
|
||||
]));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var \Doctrine\ORM\EntityManagerInterface $entityManager */
|
||||
$entityManager = $this->get('doctrine.orm.default_entity_manager');
|
||||
|
||||
/** @var OrderReferenceRepository $repository */
|
||||
$repository = $entityManager->getRepository(OrderReference::class);
|
||||
|
||||
$orderRef = $repository->findByCartId($idCart);
|
||||
|
||||
if (!$orderRef) {
|
||||
$orderRef = new OrderReference();
|
||||
$orderRef->setIdCart($idCart);
|
||||
}
|
||||
|
||||
$orderRef->setOrderReference($orderReference);
|
||||
|
||||
$entityManager->persist($orderRef);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->ajaxRender(json_encode([
|
||||
'success' => true,
|
||||
'message' => $this->module->l('Order reference received!', 'ajax')
|
||||
]));
|
||||
} catch (Exception $e) {
|
||||
$this->ajaxRender(json_encode([
|
||||
'success' => false,
|
||||
'message' => $this->module->l('Could not save order reference.', 'ajax')
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user