0

I want to use event sales_quote_collect_totals_before observer in Magento2.

  • Hi, Please don't edit your question direct. If you get any error when apply any solution so please comment it. – Vithal Bariya May 10 '18 at 6:27
  • Run this command php bin/magento setup:upgrad php bin/magento setup:di:compile php bin/magento cache:flush – Vithal Bariya May 10 '18 at 6:29
  • @VithalBariya same error after running commands – Chandra Kumar May 10 '18 at 6:32
  • Clear your var/di folder and again run the same command – Jeeva Chezhiyan May 10 '18 at 6:42
0

Create events.xml file to catch event.

app/code/Module Name Space/Module Name/etc/events.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_collect_totals_before">
        <observer name="change_sales_quote_collect_totals_before" instance="Module Name Space\Module Name\Observer\Totalsbefore" />
    </event>
 </config>

Create Observer file to catch event.

app/code/Module Name Space/Module Name/Observer/Totalsbefore.php

<?php
namespace Module Name Space\Module Name\Observer;

class Totalsbefore implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       //Put your code here
    }
}
  • getting error check question again. – Chandra Kumar May 10 '18 at 6:26
0

etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_collect_totals_before">
        <observer name="custom_sales_quote_collect_totals_before" instance="Vendorname\Modulename\Observer\SalesQuoteCollectTotalsBefore" />
    </event>
</config>

Observer/SalesQuoteCollectTotalsBefore.php

<?php
namespace Vendorname\Modulename\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SalesQuoteCollectTotalsBefore implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $quote = $observer->getEvent()->getQuote();

        //you can do your custom code here..

        return $this;
    }
}
  • getting error check question again. – Chandra Kumar May 10 '18 at 6:28
0

Create events.xml at below path of custom module.

Vendor/Module/etc/frontend/events.xml

<event name="sales_quote_collect_totals_before">
    <observer name="your_event_name" instance="Vendor\Module\Observer\ObserverName" />
</event>

Vendor/Module/Observer/ObserverName.php

 namespace Vendor\Module\Observer;

 use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\Event\Observer;
 use Magento\Framework\App\Filesystem\DirectoryList;


 class ObserverName implements ObserverInterface
 {

protected $request;

public function __construct(
    ......
    \Magento\Framework\App\RequestInterface $request,

) {

    $this->_request = $request;
    .....
}

/**
 * Manages redirect
 */
public function execute(Observer $observer)
{
   $quote = $observer->getQuote(); 
    echo $quote->getGrandTotal(); exit;
    // Prints your Grand Total

}
}
  • getting error check question again. – Chandra Kumar May 10 '18 at 6:26
  • @ChandraKumar, You can use quote object to get order details, Check updated answer. – user67337 May 10 '18 at 6:55
0

you can add etc/events.xml:

<event name="sales_quote_collect_totals_before">
<observer name="event_name" instance="Package\Module\Observer\Observer" />
</event>

And You have to Add Ovserver.php:-

  public function execute(\Magento\Framework\Event\Observer $observer)
   {
        $quote = $observer->getQuote();               
   }
  • getting error check question again. – Chandra Kumar May 10 '18 at 6:26
  • Run php bin/magento c:f, php bin/magneto setup:di:compile – Divyesh May 10 '18 at 6:32
  • same error after running commands – Chandra Kumar May 10 '18 at 6:32
  • It should be events.xml, not event.xml – Shawn Abramson May 21 at 1:37

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.