While adding gateways, we need to show the enabled currency on the site and give admin to select which currencies are supported by that gateway and show that gateway only while using those currency after price resolution.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sumanthkumarc created an issue. See original summary.

sumanthkumarc’s picture

Assigned: Unassigned » sumanthkumarc

i'm writing a condition plugin using new condition API for commerce_currency entity. Will put a patch shortly.

sumanthkumarc’s picture

Status: Active » Needs review
FileSize
4.2 KB

Added a new patch, this patch will only work, if the blocker issue https://www.drupal.org/node/2860102 gets fixed. Because the payment gateway is saved in order as default and even if the gateway is unset by conditions, the same gateway is taken as default option. This causes exception.

bojanz’s picture

Title: Limiting gateways shown on payment page by currency they support » Add a OrderCurrency condition
Component: Payment » Other
Assigned: sumanthkumarc » bojanz
Status: Needs review » Needs work

The attached patch is missing a schema and a test.

Other comments:

+    $active_currencies = $this->currencyStorage
+      ->loadByProperties([
+        'status' => TRUE,
+      ]);

This line is not formatted right, we don't break method calls to the next line like this.
Also, there is no need to load by properties, currencies don't use a status.

+    // If no currency is imported or active, return no form.
+    if (empty($active_currencies)) {
+      return $form;
+    }

We generally don't do this. Without a currency you can't create a store, and without a store you can't create most other entities, so we're protected.

+    foreach ($active_currencies as $currency_code => $currency) {
+      /* @var \Drupal\commerce_price\Entity\Currency $currency */
+      $currency_list[$currency_code] = $currency->getName();
+    }

You can use EntityHelper::extractLabels($currencies) instead.

+    $selected_currencies = $this->configuration['currencies'];

No point in introducing this variable when it's only used once.

+    $form['currencies'] = [
+      '#type' => 'select',
+      '#title' => $this->t('Currencies'),
+      '#multiple' => TRUE,
+      '#default_value' => $selected_currencies,
+      '#options' => $currency_list,
+      '#required' => TRUE,
+    ];

Let's use a commerce_entity_select element here, so that it uses checkboxes for up to 7 elements, and an autocomplete for more than that.
Less painful than a multiple select widget. Plus, it matches what we do in the OrderStore condition.

+    $this->configuration['currencies'] = $values['currencies'];

Without an array_filter() the currencies key will also contain unselected currencies.

+    return in_array($order_currency, $selected_currencies) ? TRUE : FALSE;

in_array already returns a boolean, the ternary is completely unneeded.

  • bojanz committed ab89b1c on 8.x-2.x
    Issue #2860111 by sumanthkumarc, bojanz: Add a OrderCurrency condition
    
bojanz’s picture

Status: Needs work » Fixed

Added test, schema, addressed my own feedback, moved the condition to commerce_order.

Thanks for getting the ball rolling, Sumanth!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.