in my case I have a controller and an Ajax data sending, but when I check the code the AJAX doesn't send anything and Magento doesn't enter to my controller, is it a problem of routing or what, this is my code :
Custom/Sales/etc/frontend/routes.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<!--Use router 'standard' for frontend route-->
<router id="standard">
<!--Define a custom route with id and frontName-->
<route frontName="customsales" id="customsales">
<!--The module which this route match to-->
<module name="Custom_Sales"/>
</route>
</router>
</config>
my js file
define([
'jquery',
'mage/utils/wrapper',
'Magento_CheckoutAgreements/js/model/agreements-assigner',
'Magento_Checkout/js/model/quote',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/url-builder',
'mage/url',
'Magento_Checkout/js/model/error-processor',
'uiRegistry'
], function (
$,
wrapper,
agreementsAssigner,
quote,
customer,
urlBuilder,
urlFormatter,
errorProcessor,
registry
) {
'use strict';
return function (placeOrderAction) {
/** Override default place order action and add agreement_ids to request */
return wrapper.wrap(placeOrderAction, function (originalAction, paymentData, messageContainer) {
agreementsAssigner(paymentData);
var isCustomer = customer.isLoggedIn();
var quoteId = quote.getQuoteId();
var url = urlFormatter.build('customsales/quote/save');
var bookingDate = $('[name="booking_date"]').val();
if (bookingDate) {
var payload = {
'cartId': quoteId,
'booking_date': bookingDate,
'is_customer': isCustomer
};
if (!payload.booking_date) {
return true;
}
var result = true;
$.ajax({
url: url,
data: payload,
dataType: 'text',
type: 'POST',
}).done(
function (response) {
result = true;
}
).fail(
function (response) {
result = false;
errorProcessor.process(response);
}
);
}
return originalAction(paymentData, messageContainer);
});
};
});
update : controller code
<?php
namespace Custom\Sales\Controller\Quote;
use Magento\Framework\Exception\NoSuchEntityException;
class Save extends \Magento\Framework\App\Action\Action
{
protected $quoteIdMaskFactory;
protected $quoteRepository;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
parent::__construct($context);
$this->quoteRepository = $quoteRepository;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}
/**
* @return \Magento\Framework\Controller\Result\Raw
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
if ($post) {
$cartId = $post['cartId'];
$bookingDate = $post['booking_date'];
$loggin = $post['is_customer'];
if ($loggin === 'false') {
$cartId = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getQuoteId();
}
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain booking', $cartId));
}
$quote->setData('booking_date', $bookingDate);
$this->quoteRepository->save($quote);
}
}
}