Add drop down selection to shipping method
-
02-03-2021 - |
Question
I need to add an additional drop down field when a particular shipping method is selected, on the order information page during checkout. I only found how to add additional plugin option when adding a new shipping method through the UI. This is for a custom omniva parcel machine location implementation. Can anyone help me out where to get started with this? EDIT: im using drupal 8 and commerce 2. UPDATE: i tried creating a custom pane, thanks to the suggestion of Berry Dingle. I added the drop down and managed to pass the value to the summary page but i can't figure out how to get the value in the email receipt. I saved the selected value like this but how can i access it in the email template?
This is how i saved the value.
$form = $form_state->getValue($pane_form['#parents']);
$val = $pane_form['omniva_parcel_machines']['#options'][$form['omniva_parcel_machines']];
$this->order->setData('omniva_parcel_machines', $val);
This is how i built the summary pane.
public function buildPaneSummary()
{
if ($parcel_machine = $this->order->getData('omniva_parcel_machines')) {
return [
'#plain_text' => $parcel_machine,
];
}
return [];
}
Is it even possible to access the value in the email receipt twig template or i have to pass it somehow? I tried using
{{ order_entity.getData('omniva_parcel_machines') }}
as well as
{{ order_item.getData('omniva_parcel_machines') }}
but so far nothing.
Solution
I resolved the issue by adding a field to the order type and then instead of using Order interface
$this->order->setData('field_omniva_parcel_machines', $val);
i used FieldableEntityInterfaces' set
$this->order->set('field_omniva_parcel_machines', $val);
and i accessed the value in the commerce-order-receipt.html.twig by using
{% if order_entity.field_omniva_parcel_machines.value %}
{{ order_entity.field_omniva_parcel_machines.value }}
{% endif %}
Thanks again, Berry, for getting me started with this!