0

I'm trying to get values from the popup model of radio buttons into the database and I'm not getting that, what am I doing wrong?

BLOCK FILE

<?php

namespace vendor\ThreeYearOrders\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Customer\Model\Session;

class Popup extends \Magento\Framework\View\Element\Template
{

     /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     **/

    /**
     * @var customerSession
     */
    protected $_customerSession;

      /**
     * @var ThreeYearOrdersBlockId
     */
    const XML_THREE_YEAR_ORDERS_BLOCK_ID = 'ThreeYearOrders/general/block_id';

    /**
     * @var ScopeConfig
     */
    protected $_scopeConfig;

    /**
     * Popup constructor.
     *
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        Session $customerSession
    ) {
        parent::__construct($context);
        $this->_scopeConfig = $scopeConfig;
        $this->_customerSession = $customerSession;
    }

    /**
     * Get Popup Config
     *
     * @return array|mixed
     */
    public function getThreeYearOdersBlockId()
    {
        return $this->_scopeConfig->getValue(self::XML_THREE_YEAR_ORDERS_BLOCK_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
    /**
     * Check if customer is logged in
     *
     * @return bool
     */

    public function isLoggedIn()
    {
        return $this->_customerSession->isLoggedIn();
    }
    public function getFormAction()
    {
        return $this->getUrl('vendor_threeyearorders/Index/Action', ['_secure' => true]);
    }

}

CONTROLLER FILE

<?php

namespace vendor\ThreeYearOrders\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\CustomerFactory;

class Action extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_pageFactory;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $pageFactory
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
     */
    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Session $customerSession,
        CustomerFactory $customerFactory
    ) {
        $this->_pageFactory = $pageFactory;
        $this->customerSession = $customerSession;
        $this->customerFactory = $customerFactory;
        parent::__construct($context);
    }

    /**
     * View page action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $deleteOrder = $this->getRequest()->getParam('deleteOrder');
        if (in_array($deleteOrder, ['1', '0'])) {
            $customerId = $this->customerSession->getCustomerId();
            $customerData = $this->customerFactory->create()->load($customerId);

            if ($deleteOrder === '1') {
                $customerData->setData('deleteOrder', 1);
            } else {
                $customerData->setData('deleteOrder', 0);
            }

            $customerData->save();
        }
    }
}

SETUP [INSTALLSCHEMA] FILE

<?php

namespace vendor\ThreeYearOrders\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $table = $installer->getTable('customer_entity');

        $installer->getConnection()->addColumn(
            $table,
            'deleteOrder',
            [
                'type' => Table::TYPE_INTEGER,
                'nullable' => true,
                'default' => null,
                'comment' => 'Delete Order'
            ]
        );

        $installer->endSetup();
    }
}

TEMPLATES

<?php
    $block_id = $block->getThreeYearOdersBlockId();
?>

<?php if ($block->isLoggedIn()): ?>

<div id="three-year-orders" class="checkBox-delete-orders">
    <form id="selectedDelect" action="<?= $block->getFormAction() ?>" method="post">
        <?php
        echo $this->getLayout()
            ->createBlock('Magento\Cms\Block\Block')
            ->setBlockId($block_id)
            ->toHtml();
        ?>

        <label class="headingCheckbox">You have orders that are 3 years old. Do you want to delete them? </label> <br />
        <input type="radio" name="deleteOrder" id="deleteOrderYes" value="1">
        <label for="deleteOrderYes" class="deleteOrderYes">Yes</label> |
        <input type="radio" name="deleteOrder" id="deleteOrderNo" value="0">
        <label for="deleteOrderNo" class="deleteOrderNo">No</label>
    </form>
</div>

<?php endif; ?>

<script>
    require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal',
        ],
        function(
            $,
            modal
        ) {
            var options = {
                type: 'popup',
                // responsive: true,
                innerScroll: true,
                title: false,
                modalClass:'modal-pricepopup', // you can replace this class and use codazon class which is "cdz-alert-popup" or leave it as default. 
                buttons: [{
                    text: $.mage.__('Close'),
                    class: '',
                    click: function () {
                        this.closeModal();
                    }
                }, {
                    text: $.mage.__('Submit'),
                    class: '',
                    click: function () {
                        $('#selectedDelect').submit();
                    }
                }]
            };
            $(document).ready(function(){
                var popup = modal(options, $('#three-year-orders'));
                $('#three-year-orders').modal('openModal');
                $('#click-here').on('click', function(){
                    $('#three-year-orders').modal('openModal');
                });
            });
        }
    );
</script>

1 Answer 1

0

you may try to update your action controller as follow.

     /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $customermodel;

public function __construct(
     ..........,
     ...........
     ...........
     ..........,
        \Magento\Customer\Model\Customer $customermodel
    ) {
        ...........
        ...........
        ...........
        $this->customermodel = $customermodel;
        ...........
    }

public function execute()
    {
        $deleteOrder = $this->getRequest()->getParam('deleteOrder');
        if (in_array($deleteOrder, ['1', '0'])) {
            $customerId = $this->customerSession->getCustomerId();
            //$customerData = $this->customerFactory->create()->load($customerId);
            $customerSingle = $this->customermodel->load($customerId);
            $customerData = $customerSingle->getDataModel();
            
            if ($deleteOrder === '1') {
               // $customerData->setData('deleteOrder', 1);
               $customerData->setCustomAttribute('deleteOrder',1);                
            } else {
                //$customerData->setData('deleteOrder', 0);
                $customerData->setCustomAttribute('deleteOrder',0);
            }            
            $customerSingle->updateData($customerData);
            $customerResource = $this->customermodelFactory->create();
            $customerResource->saveAttribute($customerSingle, 'deleteOrder');
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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