Magento2 Checkout – How to Trigger Proceed to Checkout Button

cartcheckoutmagento2shopping-cart

I need to execute my custom code on clicking proceed to checkout button.

Can we trigger the action, I need to insert quote Id with some other details to
custom table.

How this can be done, is there any methods like Plugin, event or something else to achieve this?

Best Answer

Here you need to build a module in which you need to use event 'controller_action_predispatch_checkout_index_index'.

Create registration.php

<?php 
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Module_Customize', __DIR__ 
    );

Create module.xml under etc directory

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name = "Module_Customize" setup_version = "1.0.0"></module>
</config>

Create events.xml under etc/frontend directory

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
     <event name="controller_action_predispatch_checkout_index_index">
<observer name="checkcout_page" instance="Module\Customize\Observer\CheckShoppingCartObserver" />

Create CheckShoppingCartObserver.php file under Observer directory.

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Module\Customize\Observer;

use Magento\Framework\Event\ObserverInterface;

class CheckShoppingCartObserver implements ObserverInterface
{
    protected $_request;


    public function __construct(\Magento\Framework\App\RequestInterface $request)
    {
        $this->_request = $request;
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $quote = $observer->getQuote();
        $order = $observer->getOrder();
        $quoteItems = [];

        // Map Quote Item with Quote Item Id
        foreach ($quote->getAllVisibleItems() as $quoteItem) {
            $quoteItems[$quoteItem->getId()] = $quoteItem;
        }
        // write your code according to your quote
        return $this;
    }
}

For deleting your particular quote please try this in your observer file's method

public function deleteQuoteItems(){
    $checkoutSession = $this->getCheckoutSession();
    $allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session
    foreach ($allItems as $item) {
        $itemId = $item->getItemId();//item id of particular item
        $quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id
        $quoteItem->delete();//deletes the item
    }
}
public function getCheckoutSession(){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager 
    $checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');//checkout session
    return $checkoutSession;
}

public function getItemModel(){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager
    $itemModel = $objectManager->create('Magento\Quote\Model\Quote\Item');//Quote item model to load quote item
    return $itemModel;
}
Related Topic