0

I have created a module in which created some fields and save data in Magneto 2 but there is some issue regarding to save that custom data for guest user.

my file app/code/Reg/Checkout/Api/Data/SimpleNoteInterface.php

<?php

namespace Reg\Checkout\Api\Data;
/**
 * Interface SimpleNoteInterface
 */
interface SimpleNoteInterface
{
/**
 * Get Simple Note
 *
 * @return string
 */
public function getSimpleNote();
/**
 * Set Simple Note
 *
 * @param string $simpleNote
 *
 * @return void
 */
public function setSimpleNote($simpleNote);
}

my app/code/Reg/Checkout/Api/SimpleNoteGuestManagementInterface.php

<?php
/**
 * Checkout custom fields guest repository interface
 *
 * @package   Bodak\CheckoutCustomForm
 * @author    Slawomir Bodak <slawek.bodak@gmail.com>
 * @copyright © 2017 Slawomir Bodak
 * @license   See LICENSE file for license details.
 */

declare(strict_types=1);

namespace Reg\Checkout\Api;

use Magento\Sales\Model\Order;
use Reg\Checkout\Api\Data\SimpleNoteInterface;

/**
 * Interface SimpleNoteGuestManagementInterface
 *
 * @category Api/Interface
 * @package  Bodak\CheckoutCustomForm\Api
 */
interface SimpleNoteGuestManagementInterface
{
/**
 * Save checkout custom fields
 *
 * @param string                                                   $cartId       Guest Cart id
 * @param \Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface $customFields Custom fields
 *
 * @return \Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface
 */
public function saveSimpleNote(
    $cartId,
    \Reg\Checkout\Api\Data\SimpleNoteInterface $simpleNote
): SimpleNoteInterface;
}

my app/code/Reg/Checkout/Model/SimpleNoteGuestManagement.php

<?php
namespace Reg\Checkout\Model;

use Reg\Checkout\Api\Data\SimpleNoteInterface;
use Reg\Checkout\Api\SimpleNoteGuestManagementInterface;
use Reg\Checkout\Setup\SchemaInformation;
use Exception;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;

/**
 * Class SimpleNoteManagement
 */
class SimpleNoteGuestManagement implements SimpleNoteGuestManagementInterface
{
/**
 * Quote repository.
 *
 * @var CartRepositoryInterface
 */
protected $quoteRepository;
protected $quoteIdMaskFactory;

/**
 * @var CustomFieldsRepositoryInterface
 */
protected $customFieldsRepository;

/**
 * SimpleNoteManagement constructor.
 *
 * @param CartRepositoryInterface $quoteRepository
 */
public function __construct(
    CartRepositoryInterface $quoteRepository,
    quoteIdMaskFactory $quoteIdMaskFactory
)
{
    $this->quoteRepository = $quoteRepository;
    $this->quoteIdMaskFactory     = $quoteIdMaskFactory;
}

/**
 * Save simple note number in the quote
 *
 * @param int $cartId
 * @param SimpleNoteInterface $simpleNote
 *
 * @return null|string
 *
 * @throws CouldNotSaveException
 * @throws NoSuchEntityException
 */

public function saveSimpleNote(
    $cartId,
    SimpleNoteInterface $simpleNote
): SimpleNoteInterface {
    $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
    return $this->customFieldsRepository->saveSimpleNote($quoteIdMask->getQuoteId(), $simpleNote);
}
}

my app/code/Reg/Checkout/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Reg\Checkout\Api\Data\SimpleNoteInterface" 
type="Reg\Checkout\Model\Data\SimpleNote" />
<preference for="Reg\Checkout\Api\Data\SimpleVehicleInterface" 
type="Reg\Checkout\Model\Data\SimpleAge" />
<preference for="Reg\Checkout\Api\Data\SimpleRegisteredInterface" 
type="Reg\Checkout\Model\Data\SimpleKeeper" />
<preference for="Reg\Checkout\Api\SimpleNoteManagementInterface" 
type="Reg\Checkout\Model\SimpleNoteManagement" />
<preference for="Reg\Checkout\Api\SimpleNoteGuestManagementInterface" 
type="Reg\Checkout\Model\SimpleNoteGuestManagement" />
<preference for="Reg\Checkout\Api\VehicleAgeManagementInterface" 
type="Reg\Checkout\Model\VehicleAgeManagement" />
<preference for="Reg\Checkout\Api\RegisterKeeperManagementInterface" 
type="Reg\Checkout\Model\RegisteredKeeperManagement" />
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" 
type="Magento\Sales\Model\ResourceModel\Grid">
<arguments>
    <argument name="columns" xsi:type="array">
        <item name="simple_note" xsi:type="string">sales_order.simple_note</item>
        <item name="vehicle_age" xsi:type="string">sales_order.vehicle_age</item>
        <item name="registered_keeper" xsi:type="string">sales_order.registered_keeper</item>
    </argument>
</arguments>

my app/code/Reg/Checkout/view/frontend/web/js/action/save-order-simple-note.js

define(
[
    'jquery',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/url-builder',
    'Magento_Checkout/js/model/error-processor',
    'Magento_Customer/js/model/customer',
    'mage/url'
],
function ($, quote, urlBuilder, customer, errorProcessor, urlFormatter) {
'use strict';

return {
    /**
     * Save SimpleNote ibn the quote
     *
     * @param simpleNote
     */
    save: function (simpleNote) {
        if (simpleNote) {
            var quoteId = quote.getQuoteId();
            var url;

            url = urlBuilder.createUrl('/guest-carts/:cartId/set-simple-note', {cartId: quoteId});

            var payload = {
                cartId: quoteId,
                simpleNote: {
                    simpleNote: simpleNote
                }
            };

            if (!payload.simpleNote.simpleNote) {

                return true;
            }

            var result = true;

            $.ajax({
                url: urlFormatter.build(url),
                data: JSON.stringify(payload),
                global: false,
                contentType: 'application/json',
                type: 'PUT',
                async: false
            }).done(
                function (response) {
                    result = true;
                }
            ).fail(
                function (response) {
                    result = false;
                    errorProcessor.process(response);
                }
            );

            return result;
        }
    }
};
});

it shows me error on network


Fatal error: Uncaught Error: Call to a member function saveSimpleNote() on null in /var/www/html/reg-dealers/app/code/Reg/Checkout/Model/SimpleNoteGuestManagement.php:81 Stack trace: #0 [internal function]: Reg\Checkout\Model\SimpleNoteGuestManagement->saveSimpleNote('HVrCgLwTO9Gsue4...', Object(Reg\Checkout\Model\Data\SimpleNote)) #1 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(95): call_user_func_array(Array, Array) #2 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy)) #3 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http)) #4 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array) #5 /var/www/h in /var/www/html/reg-dealers/app/code/Reg/Checkout/Model/SimpleNoteGuestManagement.php on line 81
{"messages":{"error":[{"code":500,"message":"Fatal Error: 'Uncaught Error: Call to a member function saveSimpleNote() on null in \/var\/www\/html\/reg-dealers\/app\/code\/Reg\/Checkout\/Model\/SimpleNoteGuestManagement.php:81\nStack trace:\n#0 [internal function]: Reg\\Checkout\\Model\\SimpleNoteGuestManagement->saveSimpleNote('HVrCgLwTO9Gsue4...', Object(Reg\\Checkout\\Model\\Data\\SimpleNote))\n#1 \/var\/www\/html\/reg-dealers\/vendor\/magento\/module-webapi\/Controller\/Rest\/SynchronousRequestProcessor.php(95): call_user_func_array(Array, Array)\n#2 \/var\/www\/html\/reg-dealers\/vendor\/magento\/module-webapi\/Controller\/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process(Object(Magento\\Framework\\Webapi\\Rest\\Request\\Proxy))\n#3 \/var\/www\/html\/reg-dealers\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4 \/var\/www\/html\/reg-dealers\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#5 \/var\/www\/h' in '\/var\/www\/html\/reg-dealers\/app\/code\/Reg\/Checkout\/Model\/SimpleNoteGuestManagement.php' on line 81","trace":"Trace is not available."}]}}

Any help would be appreciated.

2
  • have you not set value protected $customFieldsRepository; in the constructor into that one. Aug 20 '20 at 9:05
  • Will you please tell me the file which are u talking about. Actually this is new to me so it will be helpfull@DhirenVasoya
    – Prits
    Aug 20 '20 at 9:32

Your Answer

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

Browse other questions tagged or ask your own question.