A Complete List of all events in Magento 2

Magento 2 Complete guide to the list of available events that will be used in daily routine as a Magento developer to trigger event-specific tasks at a time of Development.

You can use any required events from the Core module to accomplish your requirements. Define events in the events.xml file, Create an observer.php file to do specific actions for an event.

You can also create your own custom event to accomplish your task by link, Create Custom Events. Continue reading “A Complete List of all events in Magento 2”

Magento 2: Get details of shopping cart all items, subtotal, grand total, billing and shipping address.

We can fetch details of all shopping cart items, subtotal and billing/shipping address from the current session.
Create Block file, ShoppingCart.php

<?php
namespace Rbj\Customer\Block;

class ShoppingCart extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;;
        parent::__construct($context, $data);
    }

    /**
     * Get quote object associated with cart. By default it is current customer session quote
     *
     * @return \Magento\Quote\Model\Quote
     */
    public function getQuoteData()
    {
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }
}

Call in template(.phtml) file, 

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();

foreach($quote->getAllVisibleItems() as $_item) {
    //echo "<pre>";print_r($_item->debug());
    echo 'ID: '.$_item->getProductId().'<br/>';
    echo 'Name: '.$_item->getName().'<br/>';
    echo 'Sku: '.$_item->getSku().'<br/>';
    echo 'Quantity: '.$_item->getQty().'<br/>';
    echo 'Price: '.$_item->getPrice().'<br/>';
    echo 'Product Type: '.$_item->getProductType().'<br/>';
    echo 'Discount: '.$_item->getDiscountAmount();echo "<br/>";
    echo "<br/>";
}
// Get total items and total quantity in current cart
echo $totalItems = $quote->getItemsCount();echo "<br/>";
echo $totalQuantity = $quote->getItemsQty();echo "<br/>";

//Get subtotal and grand total of customer current cart
echo $subTotal = $quote->getSubtotal();echo "<br/>";
echo $grandTotal = $quote->getGrandTotal();echo "<br/>";

//Get billing and shipping addresses of current cart
$billingAddress = $quote->getBillingAddress();
echo "<pre>";print_r($billingAddress->getData());
$shippingAddress = $quote->getShippingAddress();
echo "<pre>";print_r($billingAddress->getData());

call function getQuoteData() and based on that we can get getAllVisibleItems() of cart.

Using getAllVisibleItems() we can get all the visible items data. For configurable product only main item data will be available, Child item data will be not display in for loop using getAllVisibleItems method.

If you want to get all the items, like for a configurable product, the main item with child item data, will be displayed if you use $quote->getAllItems() function. getAllItems() returns all the quote item data.

 

Different types of Cancel events for Orders in Magento 2

There are many events are available in Native Magento 2 but some of the difficult events of cancel action of payment, invoice or order are as below,

1. order_cancel_after

This event is used after order canceled. If you want to do something after an order is canceled you can use this event.
Check file, vendor/magento/module-sales/Model/Order.php at cancel() function.

$this->_eventManager->dispatch('order_cancel_after', ['order' => $this]);

2. sales_order_invoice_cancel

Allows you to cancel the order invoice when you canceled the order.
Check event called at, file, vendor/magento/module-sales/Model/Order/Invoice.php at cancel() function.

$this->_eventManager->dispatch('sales_order_invoice_cancel', [$this->_eventObject => $this]);

3. sales_order_payment_cancel_invoice

Allows you to cancel the order invoice when you canceled the order.
Check event called at, file, vendor/magento/module-sales/Model/Order/Invoice.php at cancel() function.

$this->_eventManager->dispatch('sales_order_invoice_cancel', [$this->_eventObject => $this]);

4. sales_order_payment_cancel_creditmemo

Event defined under vendor/magento/module-sales/Model/Order/Payment.php using cancelCreditmemo() function but this function is not called at any file. Might be used in future developments. This event contains payment and specified creditmemo object.

$this->_eventManager->dispatch(
            'sales_order_payment_cancel_creditmemo',
            ['payment' => $this, 'creditmemo' => $creditmemo]
        );

5.sales_order_payment_cancel_invoice

Cancel specified invoice from order and update self-total from order invoice.
Event defined under vendor/magento/module-sales/Model/Order/Payment.php

$this->_eventManager->dispatch(
            'sales_order_payment_cancel_invoice',
            ['payment' => $this, 'invoice' => $invoice]
        );

This event contains payment and invoice object.

6. sales_order_creditmemo_cancel

When you want to refund from a specific order or specific item from order,
Your best suitable event is, sales_order_creditmemo_cancel

$this->eventManager->dispatch('sales_order_creditmemo_cancel', ['creditmemo' => $creditmemo]);