I am working on a custom module for Magento 2.4.5 that extends the Sales Rule Conditions functionality. My goal is to:
- Add a new condition to the “Conditions” tab in the Cart Price Rule section.
- When the new condition is selected, it should display a Customer Grid (not a dropdown/select box) to allow selecting multiple customers.
- Save the selected customer IDs as part of the condition’s data.
- Validate the coupon on the frontend to ensure it applies only if the logged-in customer matches one of the selected customer IDs.
What I’ve Done So Far:
- Created a custom module structure.
- Successfully added a custom condition to the Sales Rule Conditions section.
- Managed to add a customer selection, but currently, I can only display a dropdown/select input instead of a full customer grid.
The Problem:
- I want to render a customer grid instead of a dropdown/select box when my custom condition is selected.
- The customer grid should allow multiple selections, and the selected customer IDs should be saved.
- On the frontend, I need to validate the coupon for the logged-in customer against these selected customer IDs.
My Questions:
- How can I render a customer grid instead of a simple select box in the custom condition UI?
- How can I pass the selected customer IDs and save them properly in the Sales Rule condition data?
- How do I validate on the frontend (during cart rule checks) to ensure the coupon applies only if the customer matches one of the selected IDs?
Code So Far:
Here’s the code snippet where I’ve added the custom condition:
CustomCondition.php
namespace VendorModuleModelRuleCondition;
use MagentoRuleModelConditionAbstractCondition;
class CustomerCondition extends AbstractCondition
{
public function loadAttributeOptions()
{
$attributes = [
'selected_customers' => __('Specific Customers')
];
$this->setAttributeOption($attributes);
return $this;
}
public function getInputType()
{
return 'multiselect'; // This is where I want to render a grid instead
}
public function getValueElementType()
{
return 'select'; // This should trigger a grid instead of a dropdown
}
public function validate(MagentoFrameworkModelAbstractModel $model)
{
// Frontend validation logic
$customerId = $model->getCustomerId(); // Get the logged-in customer ID
$selectedCustomerIds = explode(',', $this->getValue()); // Saved customer IDs
return in_array($customerId, $selectedCustomerIds);
}
}
What I’m Stuck On:
- I don’t know how to replace the
select
input with a customer grid that allows multiple selections. - I need guidance on saving the customer IDs and validating them correctly.
Environment:
- Magento 2.4.5
- PHP 8.1
Any help, code examples, or suggestions on how to render the grid and save/validate the selected customer IDs would be greatly appreciated!