0

i have different payment Methods on my Checkout page i have also Purchase Oder payment Method on my checkout page i want purchase order payment method for just login customer how i achieve this in magento2

  • You need to restrict payment method by code using observer – Biren Patel Oct 17 at 10:49
0

you can create a custom module and create an event observer [ For Event "payment_method_is_active" ] to make Purchase Order Payment disable for Guest user.

I assume your custom module name is Company_MyModule

step 1)

create events.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">        
    <event name="payment_method_is_active">
        <observer name="MyModule_Pobycustomer" instance="Company\MyModule\Observer\DisablePoPayguest" />
    </event>
</config>

step 2)

Create the observer class DisablePoPayguest.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Observer

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Company\MyModule\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\Framework\App\RequestInterface;
use Magento\Customer\Model\CustomerFactory;



class DisablePoPayguest implements ObserverInterface
{

    protected $logger;

    protected $_customerSession;


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

    public function __construct(\Psr\Log\LoggerInterface $loggerInterface, \Magento\Customer\Model\Session $session, CustomerFactory $_customerFactory)
    {
        $this->logger           = $loggerInterface;
        $this->_customerSession = $session;
        $this->customerFactory  = $_customerFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        if ($method_instance->getCode() == 'purchaseorder') {            
            if (!$this->_customerSession->isLoggedIn()) {       
                $result->setData('is_available', false);
            } 
        }
    }
}

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.