0

I have some code that programmatically creates an order by building a quote object and then calling:

$this->cartManagementInterface->placeOrder($quote->getId());

For these specific orders I'm looking for a way to disable customer group pricing and just use the regular product pricing.

Does anyone know of a way to do this or where I could potentially hook in with a plugin to prevent customer group pricing from being applied?

1 Answer 1

0

Yes, it is possible to programmatically disable customer group pricing for specific orders in Magento 2. One approach is to use a plugin to modify the quote item prices before the order is placed. You can create a plugin for the CartManagementInterface's placeOrder method to adjust the prices accordingly.

Here is a step-by-step guide to achieving this:

  1. Create a Plugin for CartManagementInterface:

    First, create a plugin that modifies the quote item prices before placing the order.

    • Create the following directory structure in your custom module:
      app/code/[Vendor]/[Module]/etc/di.xml
      app/code/[Vendor]/[Module]/Plugin/DisableCustomerGroupPricing.php
      
  2. Define the Plugin in di.xml:

    In the di.xml file, define the plugin for CartManagementInterface:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Quote\Api\CartManagementInterface">
            <plugin name="disable_customer_group_pricing" type="[Vendor]\[Module]\Plugin\DisableCustomerGroupPricing" />
        </type>
    </config>
    
  3. Create the Plugin Class:

    In the DisableCustomerGroupPricing.php file, create the plugin class that modifies the quote item prices:

    <?php
    namespace [Vendor]\[Module]\Plugin;
    
    use Magento\Quote\Api\CartManagementInterface;
    use Magento\Quote\Model\Quote;
    
    class DisableCustomerGroupPricing
    {
        public function beforePlaceOrder(CartManagementInterface $subject, $cartId)
        {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $quote = $objectManager->create(\Magento\Quote\Model\QuoteRepository::class)->get($cartId);
    
            foreach ($quote->getAllItems() as $item) {
                $product = $item->getProduct();
                $regularPrice = $product->getPrice();
    
                // Set the item price to the regular product price
                $item->setCustomPrice($regularPrice);
                $item->setOriginalCustomPrice($regularPrice);
                $item->getProduct()->setIsSuperMode(true);
            }
    
            return [$cartId];
        }
    }
    
  4. Deploy and Test:

    • Run the following commands to deploy your module:

      php bin/magento setup:upgrade
      php bin/magento setup:di:compile
      php bin/magento setup:static-content:deploy -f
      php bin/magento cache:clean
      
    • Test the functionality by placing an order programmatically and verifying that the customer group pricing is not applied.

Explanation:

  • Plugin Declaration: The plugin is declared in di.xml to intercept the placeOrder method of CartManagementInterface.
  • Before Method: The beforePlaceOrder method is used to modify the quote items before the order is placed. It sets the custom price of each item to the regular product price, effectively disabling customer group pricing.
  • Setting Custom Price: By calling setCustomPrice and setOriginalCustomPrice, the item's price is overridden with the regular product price.
  • Super Mode: setIsSuperMode(true) is called to ensure that Magento does not overwrite the custom prices during the checkout process.

This approach allows you to programmatically disable customer group pricing for specific orders while using the placeOrder method.

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.