0

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);
        }
    }
}


1
  • Check var/report folder. It might be generate report or check exception.log or system.log Oct 6, 2021 at 17:55

2 Answers 2

0

If controller return 404 then check below solution.

Please run below command and check again.

php bin/magento setup:di:compile
3
  • Thank you for your reply, but it gives me error 500 not 404
    – coding mv
    Oct 6, 2021 at 11:26
  • add your cotroller code Oct 6, 2021 at 15:43
  • see my controller code above
    – coding mv
    Oct 6, 2021 at 17:50
0

Update your controller code as below. it's ajax based controller you need to return json response.

<?php
    
    namespace Custom\Sales\Controller\Quote;
    
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Framework\Controller\ResultFactory;
    
    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);
    
                $response = $this->resultFactory
                    ->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
                    ->setData([
                        'status'  => "done",
                        'message' => "data saved successfully"
                    ]);
    
                return $response;
    
            }
        }
    }
1
  • thank you , but it's not working also
    – coding mv
    Oct 8, 2021 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.