Hi i have 2 payment method available in my magento2 store . One is cash on delivery and other is custom payment gateway . I install custom payment gateway extension and it is working fine .
Now i have some conditions if that condition is ok then only i need to make active that custom payment gateway extension .
My products have a product attribute called ‘otherthancod’ . If
‘otherthancod’ is active then only show the custom
payment gateway in checkout page. For that i write the following code .
$items = $cart->getItems();
$flag = 0;
$count=0;
foreach($items as $item){
$attribute1 = $item->getProduct()->getData('otherthancod');
if($attribute1){
$flag++;
$count++;
}else{
$flag--;
}
}
if($flag == $count){
$checkResult = $observer->getEvent()->getResult();
$checkResult->setData('is_available', true);
}else{
$checkResult = $observer->getEvent()->getResult();
$checkResult->setData('is_available', false);
}
Now i want to know where i need to put this code ? I don’t want to create another extension for that .
Please help.
In my custom payment extension i have seen the following page
app/code/custompaymentgaetway/custom/Gateway/Config/config.php
class Config extends MagentoPaymentGatewayConfigConfig{
}
can i add if condition before this class ? I think this class is activating payment gateway .
I can see that in frontend template of my payment gateway is
viewfrontendwebtemplatecustompaymentgaetway.html. Actually i want to hide this frontend once the condition is false .
2
Answers
Don’t hesitate to create your own module in
app/code/YourNamespace
.Basically you just need a
registration.php
file and anetc/module.xml
:https://devdocs.magento.com/videos/fundamentals/create-a-new-module/
Also see the following example showing how to declare the Observer for this event:
https://magento.stackexchange.com/a/188367/27460
You should not afraid of
setup:upgrade
command as it is OOTB Magento command it should work fine, and If something break then please fix it might be some permission issue on server sideFirst you will need to create
events.xml
file underapp/code/Company/Module/etc/.
Then write“payment_method_is_active”
event in it. This is the event that hits on the checkout page for payment method availability.Now create
PaymentMethodAvailable.php
underCompany/Module/Observer/
and write the following code in the file. I am disabling the check money order payment method, you can change the payment method code according to your need.Now the payment method Check Money Order is disabled from checkout page .
For reference please check this link