0

In Magento 2.2.5, I need to restrict place order for some customers who assigned to certain group. Is any possibility to restrict place order based on the customer group?

3
  • so is guest user placed an order without login? or do you need to place an order for registered user only ? Commented Feb 1, 2019 at 8:41
  • @AasimGoriya Guest order disabled. Only Registered user with particular user group. Commented Feb 1, 2019 at 9:24
  • please check my ans, and let me know if any issue. Commented Feb 8, 2019 at 11:17

2 Answers 2

0

You could use Payment Restrictions which restricts and doesn't allow any payment method for customer group which you want to block.

0

Try following way, when place order in review page there has checking customer group. Customer groups come from settings or you can hard code the group id.

app/code/SR/MagentoStackExchange/etc/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
        <plugin name="disable-place-order" type="SR\MagentoStackExchange\Plugin\PaymentInformationManagement"/>
    </type>
    <type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
        <plugin name="disable-guest-place-order" type="SR\MagentoStackExchange\Plugin\GuestPaymentInformationManagement"/>
    </type>
</config>

app/code/SR/MagentoStackExchange/Model/Config.php


<?php
namespace SR\MagentoStackExchange\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Api\CartRepositoryInterface;

class Config
{
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var CartRepositoryInterface
     */
    private $cartRepository;

    /**
     * Config constructor.
     *
     * @param ScopeConfigInterface $scopeConfig
     * @param CartRepositoryInterface $cartRepository
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        CartRepositoryInterface $cartRepository
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->cartRepository = $cartRepository;
    }

    public function isAllowPlaceOrder($cartId)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->cartRepository->getActive($cartId);
        $customer = $quote->getCustomer();
        // group ID come from settings or set 1 or 2 or 3 that you want
        $restrictGroup = $this->scopeConfig->getValue('magento_stack_exchange\general\restrict_group');
        if ($restrictGroup == $customer->getGroupId()) {
            return false;
        }

        return true;
    }
}

app/code/SR/MagentoStackExchange/Plugin/PaymentInformationManagement.php


<?php
namespace SR\MagentoStackExchange\Plugin;

use Magento\Checkout\Api\PaymentInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use SR\MagentoStackExchange\Model\Config;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Api\Data\AddressInterface;

/**
 * Class PaymentInformationManagement
 */
class PaymentInformationManagement
{
    /**
     * @var Config
     */
    private $config;

    /**
     * PaymentInformationManagement constructor.
     *
     * @param Config $config
     */
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * Disable order submitting
     *
     * @param PaymentInformationManagementInterface $subject
     * @param int $cartId
     * @param PaymentInterface $paymentMethod
     * @param AddressInterface|null $billingAddress
     * @return void
     * @throws LocalizedException
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function beforeSavePaymentInformationAndPlaceOrder(
        PaymentInformationManagementInterface $subject,
        $cartId,
        PaymentInterface $paymentMethod,
        AddressInterface $billingAddress = null
    ) {
        if ($this->config->isAllowPlaceOrder($cartId)) {
            throw new LocalizedException(__("The order can't be submitted."));
        }
    }
}

app/code/SR/MagentoStackExchange/Plugin/GuestPaymentInformationManagement.php


<?php
namespace SR\MagentoStackExchange\Plugin;

use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use SR\MagentoStackExchange\Model\Config;

/**
 * Class GuestPaymentInformationManagement
 */
class GuestPaymentInformationManagement
{
    /**
     * @var Config
     */
    private $config;

    /**
     * PaymentInformationManagement constructor.
     *
     * @param Config $config
     */
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * Disable order submitting
     *
     * @param GuestPaymentInformationManagementInterface $subject
     * @param string $cartId
     * @param string $email
     * @param PaymentInterface $paymentMethod
     * @param AddressInterface|null $billingAddress
     * @return void
     * @throws LocalizedException
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function beforeSavePaymentInformationAndPlaceOrder(
        GuestPaymentInformationManagementInterface $subject,
        $cartId,
        $email,
        PaymentInterface $paymentMethod,
        AddressInterface $billingAddress = null
    ) {
        if ($this->config->isAllowPlaceOrder($cartId)) {
            throw new LocalizedException(__("The order can't be submitted."));
        }
    }
}

2
  • bhai,Can you please help me with this ? magento.stackexchange.com/questions/260148/… Can't find anything related to it on internet. :(
    – anonymous
    Commented Feb 1, 2019 at 11:16
  • This looks like it'd be perfect, and I even included module.xml and and registration.php so I can load it as a custom module, however it doesn't seem to be triggering on the chosen customer group ($restrictGroup = 4;) ... any ideas?? Thanks!
    – ne0nlight
    Commented Jul 8, 2019 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.