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.

How to Make Purchase Order Number Optional in Magento 2

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

1. Copy the below file

vendor/magento/module-offline-payments/view/frontend/web/template/payment/purchaseorder-form.html

To

app/design/frontend/[Vendor]/[Theme]/Magento_OfflinePayments/web/template/payment/purchaseorder-form.html

If the purchaseorder-form.html file already exists, then edit that file.

2. Search the below code in the purchaseorder-form.html file.

<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>

3. Replace the above code with

<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>

4. Use the below code in your custom module’s plugin file di.xml at app/code/[Vendor]/[Module]/etc

<?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>

5. Create Purchaseorder.php file at app/code/[Vendor]/[Module]/Plugin/Model

<?php

namespace [Vendor]\[Module]\Plugin\Model;

class Purchaseorder
{
    public function aroundvalidate($subject, $proceed)
    {
        // check PoNumber is empty or not
        if (empty($subject->getInfoInstance()->getPoNumber())) {
            return $this;
        }
        return $this;
    }
}

After applying the above code, one can place an order without entering a purchase order.

How to Make Purchase Order Number Optional in Magento 2

That’s it.

Also read: How to Configure Magento 2 Custom Order Number Extension

Do consider sharing this post with Magento Community via social media.

Thank you.

Chandresh Chauhan

Article by

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.