cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 1.9.2.3 - How to include Guest customer in Observer?

Magento 1.9.2.3 - How to include Guest customer in Observer?

My code for payment method only  whose customer group 9, 

 

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
	public function paymenter(Varien_Event_Observer $observer)
	{
		$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
	
			if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9)) 
			{
				$result = $observer->getResult();
				$result->isAvailable = false;
			}
	}
}

Here how can i include the guest customers in if condition?

 

 

Workout :

 

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
	public function paymenter(Varien_Event_Observer $observer)
	{
		$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
		$checkcustomer = Mage::getSingleton('customer/session')->isLoggedIn();
	
			if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) && $checkcustomer  ) 
			{
				$result = $observer->getResult();
				$result->isAvailable = false;
			}
	}
}

Any help thanks

10 REPLIES 10

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

Hello @Aveeva 

 

The guest customer's group ID is 0.

 

So, you can use OR condition to add the required condition.

 

I hope it helps.
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva  Is it correct

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
public function paymenter(Varien_Event_Observer $observer)
{
$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
// $Isloggedin = Mage::getSingleton('customer/session')->isLoggedIn();

if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) && ($currentCustomerGroup == 0) )
{
$result = $observer->getResult();
$result->isAvailable = false;
}

}
}

 

My test severs getting the issue, can't able to test, that's what asking, is it right?

 

 

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva 

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
	public function paymenter(Varien_Event_Observer $observer)
	{
		$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
		// $Isloggedin = Mage::getSingleton('customer/session')->isLoggedIn();
    
			if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) && ($currentCustomerGroup == 0) ) 
			{
				$result = $observer->getResult();
				$result->isAvailable = false;
			}
		
	}
}

Not working

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva  If i use or (||) condition, guest customer can see only PayTm, i need guest customers see only remaining payment method. Any help thanks

 

 

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
	public function paymenter(Varien_Event_Observer $observer)
	{
		$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
		// $Isloggedin = Mage::getSingleton('customer/session')->isLoggedIn();
    
			if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) || ($currentCustomerGroup == 0)) 
			{
				$result = $observer->getResult();
				$result->isAvailable = false;
			}
		
	}
}

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva Any help to prepare if condition?

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

Hello again @Aveeva 

 

Replace the condition:

$currentCustomerGroup == 0

with:

$currentCustomerGroup != 0

Thank you.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva  Code :

 

<?php
class Gta_FilterPaymentGateway_Model_Observer
{
	public function paymenter(Varien_Event_Observer $observer)
	{
		$currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
   
			if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) || ($currentCustomerGroup != 0)) 
			{
				$result = $observer->getResult();
				$result->isAvailable = false;
			}	
	}
}

Still, PayTm Payment showing to the guest customer? How to restrict guest customer.

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

Hello @Aveeva 

 

Check this:

 

if (($observer->getMethodInstance()->getCode() == 'paytm_cc') && ($currentCustomerGroup !== 9) && ($currentCustomerGroup != 0))

Thanks.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Magento 1.9.2.3 - How to include Guest customer in Observer?

@Sanjay Jethva  First thank you, one more help pls. Just guide not code help.  I am facing  PayPal error,

 

Error : 

Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/html/test/app/code/Magento/Paypal/Model/Api/Nvp.php:1524

 

File Location :

app/code/Magento/Paypal/Model/Api

 

And my function :

 

 protected function _applyStreetAndRegionWorkarounds(DataObject $address)
    {
        // merge street addresses into 1
        if ($address->getData('street2') !== null) {
            $address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
            $address->unsetData('street2');
        }
        // attempt to fetch region_id from directory
        if ($address->getCountryId() && $address->getRegion()) {
            $regions = $this->_countryFactory->create()->loadByCode(
                $address->getCountryId()
            )->getRegionCollection()->addRegionCodeOrNameFilter(
                $address->getRegion()
            )->setPageSize(
                1
            );
            $regionItems = $regions->getItems();
            $region = array_shift($regionItems);
            $address->setRegionId($region->getId());
            $address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
        }
    }

The point is, for the issue i want to update the following code,

 

protected function _applyStreetAndRegionWorkarounds(DataObject $address)
{
// merge street addresses into 1
if ($address->getData('street2') !== null) {
$address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
$address->unsetData('street2');
}
// attempt to fetch region_id from directory
if ($address->getCountryId() && $address->getRegion()) {
$regions = $this->_countryFactory->create()
->loadByCode($address->getCountryId())
->getRegionCollection()
->addRegionCodeOrNameFilter($address->getRegion())
->setPageSize(1);
if ($regions->count()) {
$regionItems = $regions->getItems();
$region = array_shift($regionItems);
$address->setRegionId($region->getId());
$address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
}
}
}

Complete Code : https://paste.ofcode.org/3TGVGN4kij4JEau5nVdqW8

 

What is the best way to overwrite?

 

Post : https://community.magento.com/t5/Magento-2-x-Technical-Issues/Magento-2-What-is-the-proper-way-to-ed...