How to Make Purchase Order Number Optional in Magento 2
Purchase Order (PO) is one of the payment methods in Magento 2 that allow commercial customers to pay for authorized purchases by referencing the PO number.
This payment method is usually applied for wholesale orders. In order to configure the purchase order, we have to set “Yes” in the “enabled” option of the ‘Purchase Order’ section from Stores > Settings > Configuration > Sales.
After applying the configuration for the purchase order, it displays at the checkout page while completing the payment method as shown in the below image.
In most cases, agencies or any other business place wholesale orders in the store. If demanded order is available, the store owner can ship that product to the customer, but what if ordered products are not available? In that scenario, the customer registers their name, required stocks, and pay the total amount or decided half amount.
The customer gets a purchase order number, but what if the customer does not have a purchase order number and still wants to purchase the product?
In the default behaviour of Magento 2, it will not allow placing an order without a purchase order as it is a required field. Use the below code and make purchase order number optional in Magento 2.
Steps to Make Purchase Order Number Optional in Magento 2
- Copy the below file
1vendor/magento/module-offline-payments/view/frontend/web/template/payment/purchaseorder-form.html
1app/design/frontend/[Vendor]/[Theme]/Magento_OfflinePayments/web/template/payment/purchaseorder-form.html - Search the below code in the purchaseorder-form.html file.
123456789101112131415<div class="field field-number required"><label for="po_number" class="label"><span><!-- ko i18n: 'Purchase Order Number'--><!-- /ko --></span></label><div class="control"><input type="text"id="po_number"name="payment[po_number]"data-validate="{required:true}"data-bind='attr: {title: $t("Purchase Order Number")},value: purchaseOrderNumber'class="input-text"/></div></div> - Replace the above code with
1234567891011121314<div class="field field-number"><label for="po_number" class="label"><span><!-- ko i18n: 'Purchase Order Number'--><!-- /ko --></span></label><div class="control"><input type="text"id="po_number"name="payment[po_number]"data-bind='attr: {title: $t("Purchase Order Number")},value: purchaseOrderNumber'class="input-text"/></div></div> - Use the below code in your custom module’s plugin file di.xml at app/code/[Vendor]/[Module]/etc
12345678<?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\OfflinePayments\Model\Purchaseorder"><plugin name="purchase_order_validate" type="Vendor\Module\Plugin\Model\Purchaseorder"/></type></config> - Create Purchaseorder.php file at app/code/[Vendor]/[Module]/Plugin/Model
123456789101112131415<?phpnamespace [Vendor]\[Module]\Plugin\Model;class Purchaseorder{public function aroundvalidate($subject, $proceed){// check PoNumber is empty or notif (empty($subject->getInfoInstance()->getPoNumber())) {return $this;}return $this;}}
After applying the above code, one can place an order without entering a purchase order.
That’s it.
If you have queries regarding this blog, feel free to ask in the Comments section below.
Also read: How to Configure Magento 2 Custom Order Number Extension
I would be happy to answer your question.
Do consider sharing this post with Magento Community via social media.
Thank you.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
6 Comments
Hi,
I do not understand the step “Use the below code in your custom module’s plugin file di.xml”.
I do not have any custom module – Where do I have to create the di.xml?
thanks
Hello Christian,
You can use the above code in any existing module or else create your custom module.
For more details you can refer to our blog: https://meetanshi.com/blog/create-module-in-magento-2/
Thank You
In step 5 you have an “if” but in both cases $this is returned, It looks like a mistake and the “if” can be omitted.
Hello Oleh,
The “if” that you’re mentioning is not compulsory as we’ve added it to check whether the purchase number arrives as blank or not.
Thank You.
Hi,
Remark: Step 4 line 6 put \Vendor\Module\ in brackets so that its clearly visible that here has to be made some adjustment like in the other places where its needed.
Question: What is needed to get rid of the mandatory field in the admin order form?
Thank you!
Plastikschnitzer
Hello Plasticschnitzer,
Regarding your query about mandatory field, it depends on the field which you don’t want to keep mandatory.
Thank You.