1

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

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

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

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

}

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.