4

Is there any way to make purchase order optional in checkout page??

1

I had to this very same thing today - you need to remove the validation from the input itself as described above by localising;

/m2root/app/design/frontend/NameSpace/Theme/Magento_OfflinePayments/web/template/payment/purchaseorder-form.html

Change;

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

To;

<input type="text"
                           id="po_number"
                           name="payment[po_number]"
                           data-validate="{required:false}"
                           data-bind='
                            attr: {title: $t("Purchase Order Number")},
                            value: purchaseOrderNumber'
                           class="input-text"/>

That gets you passed client-side javascript validation, but you additionally need to override the validation function in model class;

Magento\OfflinePayments\Model\Purchaseorder

Use a plugin. In your custom modules /etc/di.xml file add;

<type name="Magento\OfflinePayments\Model\Purchaseorder">
    <plugin name="purchase_order_validate" type="ModuleNamespace\ModuleName\Plugin\Model\Purchaseorder" />
</type>

Then create the file

ModuleNamespace\ModuleName\Plugin\Model\Purchaseorder.php

With this - note I've added a helper class in as in my instance I need to make it required based on a customer attribute - you can leave that bit out if you dont need it.

namespace ModuleNamespace\ModuleName\Plugin\Model;
use Magento\Framework\Exception\LocalizedException;

class Purchaseorder {


protected $cHelper;

public function __construct(
    \ModuleNamespace\ModuleName\Helper\Customer $cHelper
) {

    $this->cHelper = $cHelper;
}



public function aroundvalidate($subject,$proceed)
{

    // Use whatever function you want if you want to conditionally make PO number required 
    $needsValidation = $this->cHelper->getIsPoNumRequired();
    if(!$needsValidation) { 
        // Validation avoided here
        return $this;
    }

    // Still here?  You will get empty PO number validation then 
    if (empty($subject->getInfoInstance()->getPoNumber())) {
        throw new LocalizedException(__('Purchase order number is a required field.'));
    }

    return $this;
  }

}
|improve this answer|||||
  • it is not working – gajjala sandeep Oct 1 '19 at 12:36
  • you must have missed a step - this definitely works. please check again. – pixiemedia Oct 23 '19 at 21:55
  • @piximedia why you used helper class in the above code and didn't mention the helper – Jack Dec 2 '19 at 9:21
  • Hi Jack, as I say in the code, that is an example of how you could use a helper to use a condition to control whether the PO number is required or not - in our use case only certain customer types need to enter a PO – pixiemedia Dec 3 '19 at 12:15
  • Thanks for this works like a charm. Guys if you don't need a helper and just need the po number to not be checked at all then you can just change the whole content of the aroundvalidate function to: return $this; – Sanne Jan 13 at 15:13
0

You can edit below file to make Purchase Order Number optional

copy

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

to

app/design/frontend/{Package}/{theme}/Magento_OfflinePayments/web/template/payment/purchaseorder-form.html

in the above file, you need to remove data-validate="{required:true}" from input box.

After that you need to clear cache, Remove static content and if in production module, deploy content

|improve this answer|||||
  • I tried this but it's not working – gill May 14 '19 at 14:48
  • what is not working ? template overriding working ? can see your changes on front ? – Pawan May 14 '19 at 17:00
  • Overriding is working but purchase order still showing required. – gill May 14 '19 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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