How to apply custom condition before product collection loaded magento 2?

You can apply custom conditions to Product Collection before load by creating a plugin in Magento 2. Some situation in your project need to add some conditions before product collection loaded you can achieve it by below way,

In Magento\Catalog\Model\ResourceModel\Product\Collection file load() method is used for load the product collection globally.

Continue reading “How to apply custom condition before product collection loaded magento 2?”

Magento 2 Add Anchor link to Success or Error Message.

You can add anchor tag link in success message, an error message or any notification message in Magento 2 by addSuccessMessage( ), addErrorMessage( ) and addNoticeMessage( ) respectively.

Let’s add a link to the Shopping cart of a product successfully added to the cart. Continue reading “Magento 2 Add Anchor link to Success or Error Message.”

Customer Logout programmatically in Magento 2.

Magento 2, You can log out customer programmatically by getting current customer session using the Model file, Magento\Customer\Model\Session

You need to call logout() function from above class dependency.
Call below dependency in __construct function,

<?php
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession
) {
    $this->redirect = $context->getRedirect();
    $this->customerSession = $customerSession;
}

public function customerLogout() {
    $customerId = $this->customerSession->getId();
    if($customerId) {
        $this->customerSession->logout()
             ->setBeforeAuthUrl($this->redirect->getRefererUrl())
             ->setLastCustomerId($customerId);
        return "Customer logout successfully";
    } else {
        return "Customer is not login";
    }
}

Call customerLogout() function from above file to programmatically log out a customer from the current session.